-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.py
64 lines (52 loc) · 1.76 KB
/
entry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Description : Entry point for Simple Question Answer Chatbot
# Program Argument :
# datasetName = "Name of dataset text file" eg. "USB.txt"
# Usage :
# $ python entry.py dataset/USB
print("Bot> Please wait, while I am loading my dependencies")
from Retrieval_TFIDF import Retrieval_TFIDF as DRM
from processed_question import ProcessedQuestion as PQ
import re
import sys
if len(sys.argv) == 1:
print("Bot> I need some reference to answer your question")
print("Bot> Please! Rerun me using following syntax")
print("\t\t$ python3 P2.py <datasetName>")
print("Bot> You can find dataset name in \"dataset\" folder")
print("Bot> Thanks! Bye")
exit()
datasetName = sys.argv[1]
# Loading Dataset
try:
datasetFile = open(datasetName,"r")
except FileNotFoundError:
print("Bot> Oops! I am unable to locate \"" + datasetName + "\"")
exit()
# Retrieving paragraphs : Assumption is that each paragraph in dataset is
# separated by new line character
paragraphs = []
for para in datasetFile.readlines():
if(len(para.strip()) > 0):
paragraphs.append(para.strip())
# Processing Paragraphs
drm = DRM(paragraphs,True,True)
print("Bot> Hey! I am ready. Ask me factoid based questions only :P")
print("Bot> You can say me Bye anytime you want")
# Greet Pattern
greetPattern = re.compile("^\ *((hi+)|((good\ )?morning|evening|afternoon)|(he((llo)|y+)))\ *$",re.IGNORECASE)
isActive = True
while isActive:
userQuery = input("You> ")
if(not len(userQuery)>0):
print("Bot> You need to ask something")
elif greetPattern.findall(userQuery):
response = "Hello!"
elif userQuery.strip().lower() == "bye":
response = "Bye Bye!"
isActive = False
else:
# Proocess Question
pq = PQ(userQuery,True,False,True)
# Get Response From Bot
response =drm.query(pq)
print("Bot>",response)