diff --git a/asked_questions_log.txt b/asked_questions_log.txt index fa1dccb..e69de29 100644 --- a/asked_questions_log.txt +++ b/asked_questions_log.txt @@ -1,728 +0,0 @@ -hi /nboo /n - -test -test2 -test123 -Testing exit code -boo -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -test -testing -joe -fred -fras -/usr/local/bin/python3 "/Volumes/GoogleDrive-100976413726208790908/My Drive/UNI/INFS 2048 - System Design and Realisation/Assessment 2B/main.py" -h -test -hello -/usr/local/bin/python3 "/Volumes/GoogleDrive-100976413726208790908/My Drive/UNI/INFS 2048 - System Design and Realisation/Assessment 2B/main.py" -/usr/local/bin/python3 "/Volumes/GoogleDrive-100976413726208790908/My Drive/UNI/INFS 2048 - System Design and Realisation/Assessment 2B/main.py" -/usr/local/bin/python3 "/Volumes/GoogleDrive-100976413726208790908/My Drive/UNI/INFS 2048 - System Design and Realisation/Assessment 2B/main.py" -/usr/local/bin/python3 "/Volumes/GoogleDrive-100976413726208790908/My Drive/UNI/INFS 2048 - System Design and Realisation/Assessment 2B/main.py" -/usr/local/bin/python3 "/Volumes/GoogleDrive-100976413726208790908/My Drive/UNI/INFS 2048 - System Design and Realisation/Assessment 2B/main.py" -/usr/local/bin/python3 "/Volumes/GoogleDrive-100976413726208790908/My Drive/UNI/INFS 2048 - System Design and Realisation/Assessment 2B/main.py" -/usr/local/bin/python3 "/Volumes/GoogleDrive-100976413726208790908/My Drive/UNI/INFS 2048 - System Design and Realisation/Assessment 2B/main.py" -hello -is -me -you -/usr/local/bin/python3 "/Volumes/GoogleDrive-100976413726208790908/My Drive/UNI/INFS 2048 - System Design and Realisation/Assessment 2B/main.py" -test 2 -test 3 - - - - - - -test 2 -/usr/local/bin/python3 "/Volumes/GoogleDrive-100976413726208790908/My Drive/UNI/INFS 2048 - System Design and Realisation/Assessment 2B/main.py" -test -test 1 -test 2 -test 3 -test 1 -test -test 2 -test 3 -test 4 -hello -/usr/local/bin/python3 "/Volumes/GoogleDrive-100976413726208790908/My Drive/UNI/INFS 2048 - System Design and Realisation/Assessment 2B/main.py" -test 1 -test 2 -test 3 -what is the weather like today -how are you -what is your name \ No newline at end of file diff --git a/faq.json b/faq.json index 6111985..7768faa 100644 --- a/faq.json +++ b/faq.json @@ -6,5 +6,9 @@ { "question": "What is the weather like today?", "answer": "Same as yesterday." + }, + { + "question": "What is your birthday?", + "answer": "April." } ] \ No newline at end of file diff --git a/main.py b/main.py index 1d8fb4a..9921d8a 100644 --- a/main.py +++ b/main.py @@ -1,24 +1,32 @@ import json -print("Hello, I am a question answering bot.\n") -#question = str(input("Please enter a question, and press the ENTER key:\n")) -#if question.isalpha(): -# print ("I think that you asked " + (question)) -# # Open the file in append & read mode ('a+') -# with open("asked_questions_log.txt", "a+") as file_object: -# # Move read cursor to the start of file. -# file_object.seek(0) -# # If file is not empty then append '\n' -# data = file_object.read(100) -# if len(data) > 0 : -# file_object.write("\n") -# # Append text at the end of file -# file_object.write(question) -userQuestion = '' -while True: - #question = input('') - userQuestion = input('Please enter a question, and press the ENTER key: ').strip() + +def jaccard_similarity(userQuestion, questionBank): + s1 = set(userQuestion) + s2 = set(questionBank) + return float(len(s1.intersection(s2)) / len(s1.union(s2))) + +def most_likely(userQuestion): + likelihoodScore = [] + fileObject = open("faq.json", "r") + jsonContent = fileObject.read() + aList = json.loads(jsonContent) + for json_object in aList: + likelihoodScore.append(jaccard_similarity(userQuestion, json_object['question'])) + #print(likelihoodScore) + + mostLikelyIndex = likelihoodScore.index(max(likelihoodScore)) + mostLikely = aList[mostLikelyIndex] + + return mostLikely + +def main(): + print("Hello, I am a question answering bot.\n") + userQuestion = '' + while True: + userQuestion = input('Please enter a question, and press the ENTER key: \n').strip() if userQuestion: - print ("I think that you asked " + (userQuestion)) + likelyQuestion = most_likely(userQuestion) + print ("I think that you asked " + "'" + (likelyQuestion['question']) + "'" + " and conclude that the answer is " + "'" + (likelyQuestion['answer']) + "'.\n") # Open the file in append & read mode ('a+') with open("asked_questions_log.txt", "a+") as file_object: file_object.seek(0) # Move read cursor to the start of file. @@ -27,26 +35,11 @@ while True: # Append text at the end of file file_object.write("\n") file_object.write(userQuestion) + + else : print ("Goodbye!") exit() -#else: -# sys.exit() - - - -#def jaccard_similarity(list1, list2): - # s1 = set(list1) - # s2 = set(list2) - # return float(len(s1.intersection(s2)) / len(s1.union(s2))) -#list1 = ['dog', 'cat', 'cat', 'rat'] -#list2 = ['dog', 'cat', 'mouse'] -#jaccard_similarity(list1, list2) - - -#with open('faq.json', 'r') as f: -# questionBank = json.load(f) - -# Output json file for testing -#print(questionBank) \ No newline at end of file +if __name__ == "__main__": + main() \ No newline at end of file