import pandas as pd

data = pd.read_questions('questions.txt')

# this function is used to get printable results
def getResults(questions, fn):
    def getResult(q):
        answer, score, prediction = fn(q)
        return [q, prediction, answer, score]

    return pd.DataFrame(list(map(getResult, questions)), columns=["Q", "Prediction", "A", "Score"])

test_data = [
" Ellen Ripley,Aliens"
"Jack Sparrow,Pirates of the Caribbean"
"Han Solo,Star Wars"
"Gandalf,Lord of the Rings"
"John McClane,Die Hard"
"Neo,Matrix"
"Maximus,Gladiator"
"Snake Plissken,Escape from New York"
"Dorothy Gale,The Wizard of Oz"
"Inigo Montoya,The Princess Bride"
"Sarah Connor,Terminator"
]
data

from Levenshtein import ratio

def getApproximateAnswer(q):
    max_score = 0
    answer = ""
    prediction = ""
    for idx, row in data.iterrows():
        score = ratio(row["Question"], q)
        if score >= 0.9:  # I'm sure, stop here
            return row["Answer"], score, row["Question"]
        elif score > max_score:  # I'm unsure, continue
            max_score = score
            answer = row["Answer"]
            prediction = row["Question"]
    if max_score > 0.8:
        return answer, max_score, prediction
    return "Sorry, I didn't get you.", max_score, prediction

getResults(test_data, getApproximateAnswer)
from Levenshtein import ratio

def getApproximateAnswer2(q):
    max_score = 0
    answer = ""
    prediction = ""
    for idx, row in data.iterrows():
        score = ratio(row["Question"], q)
        if score >= 0.9:  # I'm sure, stop here
            return row["Answer"], score, row["Question"]
        elif score > max_score:  # I'm unsure, continue
            max_score = score
            answer = row["Answer"]
            prediction = row["Question"]
    if max_score > 0.3:  # threshold is lowered
        return answer, max_score, prediction
    return "Sorry, I didn't get you.", max_score, prediction

getResults(test_data, getApproximateAnswer2)
