-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot.py
250 lines (207 loc) · 6.99 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import spacy
from spacy.matcher import Matcher
from rdflib import Graph
# question1 = "What is COMP 474 about?"
# question2 = "Which courses did Lucas take?"
# question3 = "Which courses cover Expert Systems?"
# Which courses cover Natural Language Processing?
# Who is familiar with Recurrent neural networks
# question4 = "Who is familiar with financial reporting?"
# question5 = "What does Lucas Wang know?"
g = Graph()
g.parse('course_topics_student.ttl', format="ttl")
nlp = spacy.load('en_core_web_sm')
matcher = Matcher(nlp.vocab)
pattern_course_id = [{"TEXT": {"REGEX": "[A-Z]{4}"}},
{"TEXT": {"REGEX": "[0-9]{1,4}"}}]
matcher.add("course_id", None, pattern_course_id)
def get_course_id(doc):
course_id = ''
matchers = matcher(doc)
for match_id, start, end in matchers:
course_id = doc[start:end].text
return course_id
def nlp_question(question):
lemma_list = []
doc = nlp(question)
for token in doc:
lemma_list.append(token.lemma_)
# student_name = ''
# topic_name = ''
# for ent in doc.ents:
# if ent.label_ == 'PERSON':
# student_name = ent.text
# elif ent.label_ == 'ORG':
# topic_name = ent.text
# print("student_name:"+student_name)
# print("topic_name:" + topic_name)
entity = ""
last_one_compound = False
for token in doc:
if token.dep_ == 'compound':
entity = entity + " " + token.text
last_one_compound = True
elif token.dep_ != 'compound' and last_one_compound == True:
entity = entity + " " + token.text
last_one_compound = False
else:
last_one_compound = False
if entity == "":
for i in range(0, len(doc)):
if doc[i].tag_ == 'NN':
entity = doc[i].text
if i - 1 >= 0 and doc[i - 1].tag_ == 'JJ':
entity = doc[i - 1].text + " " + entity
if entity == "":
for i in range(0, len(doc)):
if doc[i].tag_ == 'NNP':
entity = doc[i].text
# print("entity ==== "+entity)
entity = entity.strip()
course_about_vocab_list = ['what', 'about']
course_take_vocab_list = ['which', 'course', 'take']
course_cover_vocab_list = ['which', 'course', 'cover']
topic_familiar_vocab_list = ['who', 'familiar']
student_know_vocab_list = ['what', 'know']
course_id = get_course_id(doc)
if(all(x in lemma_list for x in course_about_vocab_list) and course_id != ''):
question1(course_id,g)
elif(all(x in lemma_list for x in course_take_vocab_list) and entity != ''):
question2(entity, g)
elif(all(x in lemma_list for x in course_cover_vocab_list) and entity != ''):
question3(entity, g)
elif(all(x in lemma_list for x in topic_familiar_vocab_list) and entity != ''):
question4(entity, g)
elif (all(x in lemma_list for x in student_know_vocab_list) and entity != ''):
question5(entity, g)
else:
print("Sorry, I have no idea.")
# question1 = "What is COMP 474 about?"
def question1(course_id,g):
if len(course_id.split(' '))<2:
print("No this course.")
return
subject = course_id.split(' ')[0]
identifier = course_id.split(' ')[1]
qres = g.query(
"""
select
?description
where{
?course dc:description ?description.
?course dc:subject '%s'.
?course dc:identifier '%s'.
}
"""%(subject,identifier)
)
for row in qres:
print(row.description)
if len(qres) == 0 :
print("No course %s"%course_id)
# question2 = "Which courses did Joan Kennedy take?"
def question2(student_name, g):
if len(student_name.split(' '))<2:
print("No this student.")
return
givenName = student_name.split(' ')[0]
familyNanme = student_name.split(' ')[1]
qres = g.query(
"""
select
?course_name
where{
?student foaf:givenName '%s'.
?student foaf:familyNanme '%s'.
?student focu:hasCompletedCourse ?hasCompletedCourse.
?hasCompletedCourse focu:completedCourseID ?course.
?course foaf:name ?course_name
}
""" % (givenName, familyNanme)
)
for row in qres:
print(row.course_name)
if len(qres) == 0 :
print("%s did not take any courses."%student_name)
# question3 = "Which courses cover Expert Systems?"
def question3(topic, g):
if len(topic)==0:
print("No this topic.")
return
qres = g.query(
"""
select
?course_name
where{
?course foaf:name ?course_name.
?course focu:hasTopic ?topic.
?topic rdfs:label '%s'
}
""" % (topic.lower())
)
for row in qres:
print(row.course_name)
if len(qres) == 0 :
print("No course covers it")
# question4 = "Who is familiar with Natural Language Processing?"
def question4(topic, g):
if len(topic)==0:
print("No this topic.")
return
qres = g.query(
"""
select
?givenName ?familyNanme
where{
?course focu:hasTopic ?topic.
?topic rdfs:label "%s".
?student foaf:givenName ?givenName.
?student foaf:familyNanme ?familyNanme.
?student focu:hasCompletedCourse ?completedCourse.
?completedCourse focu:completedCourseID ?course.
?completedCourse focu:completedCourseGrade ?grade.
Filter (?grade != 'F')
}
""" %(topic.lower())
)
for row in qres:
print(row.givenName+" "+row.familyNanme)
if len(qres) == 0 :
print("No one familiars with %s."%topic)
# question5 = "What does Lucas know?"
def question5(student_name, g):
if len(student_name.split(' '))<2:
print("No this student.")
return
givenName = student_name.split(' ')[0]
familyNanme = student_name.split(' ')[1]
qres = g.query(
"""
select
distinct ?topicLabel
where{
?student foaf:givenName '%s'.
?student foaf:familyNanme '%s'.
?student focu:hasCompletedCourse ?completedCourse.
?completedCourse focu:completedCourseID ?course.
?completedCourse focu:completedCourseGrade ?grade.
?course focu:hasTopic ?topic.
?topic rdfs:label ?topicLabel.
Filter (?grade != 'F')
}
""" %(givenName,familyNanme)
)
for row in qres:
print(row.topicLabel)
if len(qres) == 0 :
print("%s knows nothing."%student_name)
def start_chatbot():
g = Graph()
g.parse('course_topics_student.ttl', format="ttl")
print("Please enter your Question, or enter Q to quit.")
while True:
question = input(">")
if question == 'Q':
break
else:
nlp_question(question)
start_chatbot()