forked from vishakha-lall/MapBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot.py
144 lines (136 loc) · 4.9 KB
/
chatbot.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import utilities
import databaseconnect
import googleMapsApiModule
from enum import Enum, auto
import logging
import logger_config
location_dict = {"origin": "null", "destination": "null"}
log = logging.getLogger(__name__)
log.info("Entered module: %s" % __name__)
class LearnResponse(Enum):
MESSAGE = auto()
TRAIN_ME = auto()
ORIGIN = auto()
DESTINATION = auto()
@logger_config.logger
def setup():
utilities.setup_nltk()
logging.debug("NLTK setup completed")
clf = utilities.classify_model_adv(model="rf")
logging.debug("Classification model ready")
databaseconnect.setup_database()
logging.debug("Database setup completed, database connected")
learn_response = LearnResponse.MESSAGE.name
return clf, learn_response
@logger_config.logger
def message_to_bot(H, clf, learn_response):
if learn_response == LearnResponse.ORIGIN.name:
location_dict["origin"] = H
B = "Can you help me with the destination location?"
learn_response = LearnResponse.DESTINATION.name
return B, learn_response
if learn_response == LearnResponse.DESTINATION.name:
location_dict["destination"] = H
origin, destination = (
location_dict["origin"],
location_dict["destination"],
)
googleMapsApiModule.direction(origin, destination)
B = "I will certainly help you with that."
learn_response = LearnResponse.MESSAGE.name
return B, learn_response
if "bye" in H.lower().split(" "): # check in words within H
B = "Bye! I'll miss you!"
return B, learn_response # exit loop
if not H:
B = "Please say something!"
return B, learn_response # empty input
# grammar parsing
subj = set()
obj = set()
verb = set()
triples, root = utilities.parse_sentence(H)
triples = list(triples)
for t in triples:
if t[0][1][:2] == "VB":
verb.add(t[0][0])
relation = t[1]
if relation[-4:] == "subj":
subj.add(t[2][0])
if relation[-3:] == "obj":
obj.add(t[2][0])
logging.debug(
"\t"
+ "Subject: "
+ str(subj)
+ "\n"
+ "\t"
+ "Object: "
+ str(obj)
+ "\n"
+ "\t"
+ "Topic: "
+ str(root)
+ "\n"
+ "\t"
+ "Verb: "
+ str(verb)
)
subj = list(subj)
obj = list(obj)
verb = list(verb)
proper_nouns = set()
for t in triples:
if t[0][1] == "NNP":
proper_nouns.add(t[0][0])
if t[2][1] == "NNP":
proper_nouns.add(t[2][0])
proper_nouns == list(proper_nouns)
logging.debug("\t" + "Proper Nouns: " + str(proper_nouns))
# classification
classification = utilities.classify_sentence(clf, H)
# logging.debug(classification)
if learn_response == LearnResponse.MESSAGE.name:
databaseconnect.add_to_database(classification, subj, root, verb, H)
if classification == "C":
B = databaseconnect.get_chat_response()
elif classification == "Q":
B, learn_response = databaseconnect.get_question_response(subj, root, verb)
if learn_response == LearnResponse.TRAIN_ME.name and (
len(proper_nouns) == 0
or (len(proper_nouns) == 1 and H.split(" ", 1)[0] != "Where")
):
databaseconnect.add_learnt_statement_to_database(subj, root, verb)
if learn_response == LearnResponse.TRAIN_ME.name and (
len(proper_nouns) >= 2
or (len(proper_nouns) == 1 and H.split(" ", 1)[0] == "Where")
):
learn_response = LearnResponse.MESSAGE.name
B = "I will certainly help you with that."
else:
B = "Oops! I'm not trained for this yet."
else:
B, learn_response = databaseconnect.learn_question_response(H)
if (
len(proper_nouns) >= 2
or (len(proper_nouns) >= 1 and H.split(" ", 1)[0] == "Where")
) and len(subj) != 0:
if subj[0] == "distance":
if len(proper_nouns) == 2:
location_dict["origin"] = proper_nouns.pop()
location_dict["destination"] = proper_nouns.pop()
origin, destination = (
location_dict["origin"],
location_dict["destination"],
)
googleMapsApiModule.direction(origin, destination)
else:
B = "I didn't get that. Can you please give me the origin location?"
learn_response = LearnResponse.ORIGIN.name
if len(proper_nouns) == 1:
location = proper_nouns.pop()
if subj[0] == "geocoding" or subj[0] == location:
googleMapsApiModule.geocoding(location)
learn_response = LearnResponse.MESSAGE.name
B = "I will certainly help you with that."
return B, learn_response