q&a working

This commit is contained in:
David Lawler 2022-03-02 13:22:31 +10:00
parent 4623fe2884
commit 09a4b427c2
3 changed files with 35 additions and 766 deletions

View file

@ -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

View file

@ -6,5 +6,9 @@
{ {
"question": "What is the weather like today?", "question": "What is the weather like today?",
"answer": "Same as yesterday." "answer": "Same as yesterday."
},
{
"question": "What is your birthday?",
"answer": "April."
} }
] ]

63
main.py
View file

@ -1,24 +1,32 @@
import json import json
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") 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 = '' userQuestion = ''
while True: while True:
#question = input('') userQuestion = input('Please enter a question, and press the ENTER key: \n').strip()
userQuestion = input('Please enter a question, and press the ENTER key: ').strip()
if userQuestion: 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+') # Open the file in append & read mode ('a+')
with open("asked_questions_log.txt", "a+") as file_object: with open("asked_questions_log.txt", "a+") as file_object:
file_object.seek(0) # Move read cursor to the start of file. 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 # Append text at the end of file
file_object.write("\n") file_object.write("\n")
file_object.write(userQuestion) file_object.write(userQuestion)
else : else :
print ("Goodbye!") print ("Goodbye!")
exit() exit()
#else: if __name__ == "__main__":
# sys.exit() main()
#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)