# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# !pip install fuzzywuzzy

from fuzzywuzzy import fuzz 
from fuzzywuzzy import process 
import pandas as pd
import numpy as np
import string

#set up df and target sentence for string matching
triggers = pd.read_csv('weather app sample conversations - more explicit triggers.csv', names = ['Sentences'])
triggers.dropna(inplace=True)
target_sent = 'What is the weather'

#classify based on fuzzy string ratio
triggers['fuzzydist'] = triggers['Sentences'].apply(lambda x: fuzz.ratio(x, target_sent))
triggers['label'] = triggers['fuzzydist'].apply(lambda x: x > 50)

triggers

sum(triggers['label'])/len(triggers)

#clean sentences first and pass into fuzzywuzzy
triggers['sent_cleaned'] = triggers['Sentences'].apply(lambda x: x.translate(str.maketrans('', '', string.punctuation)))
triggers['label_cleaned'] = triggers['sent_cleaned'].apply(lambda x: fuzz.ratio(x, target_sent) > 50)

sum(triggers['label_cleaned'])/len(triggers)

# +
#test using multiple trigger sents
target_sents = ['what is the weather', 'will it rain today', 'is it hot today', 'what is todays forecast', 'is it cold outside']
def classify_sent(sent): 
    scores = [fuzz.ratio(sent, target) for target in target_sents]
    print(scores)
    return max(scores) > 50

triggers['label_mult'] = triggers['sent_cleaned'].apply(lambda x: classify_sent(x))
sum(triggers['label_mult'])/len(triggers)
# -

#test on negative examples
f = open('samples.txt', 'r')

labels_cont = [classify_sent(sent) for sent in f]    

sum(labels_cont)

classify_sent("Im headed to New York this summer since my internship is remote anyway. Oh, that sounds cool! I've heard NY gets really humid at that time of the year though? I don't mind a little humidity, I think it'll be fine! Sweet! Let me know how that goes - I might have to go to Florida for mine, so well be on the same time zone. Oh, awesome")

# ## Testing framework

import test


# +
def is_trigger(sent, trig): 
    return fuzz.ratio(sent, trig) > 70 

test.compute_train_test_split(is_trigger)

# +
from tqdm import tqdm
import random

with open("samples.txt") as f:
    sentences = f.readlines()

with open("triggers.txt") as f:
    triggers = f.readlines()

with open("neg_distractors.txt") as f:
    distractors = f.readlines()

def compute_accuracy(sents, trigs, is_trigger):
    identified_trigger = 0
    random.shuffle(sents)
    for sent in tqdm(sents):
        for trig in trigs:
            if is_trigger(sent, trig):
                identified_trigger += 1
                break
    return identified_trigger, identified_trigger/len(sents)

def compute_train_test_split(is_trigger):
    print("Computing false positives:")
    random.shuffle(triggers)
    train_triggers = triggers[:int(len(triggers)*.8)]
    test_triggers = triggers[int(len(triggers)*.8): len(triggers)]
    print(compute_accuracy(sentences[:50], train_triggers, is_trigger))

    print("Compute true positives")
    print(compute_accuracy(test_triggers, train_triggers, is_trigger))

    print("Compute neg distractors rate")
    print(compute_accuracy(distractors, train_triggers, is_trigger))


# -

compute_train_test_split(is_trigger)



StarcoderdataJupyterScriptsDedupFiltered