-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintelligence.py
116 lines (92 loc) · 3.51 KB
/
intelligence.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
from pymongo import MongoClient
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics.pairwise import euclidean_distances
import random
from nltk.stem import PorterStemmer
ps=PorterStemmer()
dummy=''
def fetchData():
MONGODB_URI = "mongodb://Debangshu:[email protected]:63694/brilu"
client = MongoClient(MONGODB_URI, connectTimeoutMS=30000)
db = client.get_database("brilu")
col = db["Knowledgebase"]
cursor = col.find()
# p.pprint(cursor[0])
document = cursor[0]
#p.pprint(document.keys())
#p.pprint(document["chitchat"])
return(document)
def findBest(query,document,topic):
for mood in document[topic].keys():
for question in document[topic][mood]:
if(stem(query)==stem(question)):
return(topic,mood,random.choice(document[topic][mood]))
return ('not','not','not')
def answerBest(topic,mood,document):
if topic=='chitchat':
return(topic,mood,random.choice(document[topic][mood]))
if topic=='comments':
return(topic+'ans',mood,random.choice(document[topic+'ans'][mood]))
def findBestQuery(query,document,que):
questionarr = []
intentarr=[]
answerarr=[]
for topics in document.keys():
if topics==que:
for questionlist in document[topics].keys():
for question in document[topics][questionlist]:
questionarr.append(question)
intentarr.append(questionlist)
#print(questionlist)
query = stem(query)
for i in range(0, len(questionarr)):
questionarr[i] = stem(questionarr[i])
questionarr.append(query)
#print(questionarr)
vectorizer = CountVectorizer()
ques = vectorizer.fit_transform(questionarr).todense()
match = []
for f in range(0, len(ques) - 2):
p = euclidean_distances(ques[len(ques) - 1], ques[f])
match.append(p[0][0])
m = match.index(min(match))
#print(match)
if match == [] or min(match) > 1.5:
return 'sorry i dont know how to reply'
probableQuestion=questionarr[m]
#print('probable question=',intentarr[m])
#print('query=',query)
return (que,intentarr[m],probableQuestion)
def findBestAnswer(probableQuestion,document):
myanswers=[]
for answer in document['questionans'][probableQuestion]:
#print(answer)
myanswers.append(answer)
return(random.choice(myanswers))
def stem(mystring):
mystring=mystring.lower()
mystring=mystring.split()
my=''
for word in range(0,len(mystring)):
my=my+ ps.stem(mystring[word])+' '
return my
def tryToHandleByQuickReply(probableQuestion,document):
if document['quickreplymapping'].get(probableQuestion):
return True,probableQuestion
return False,"dummy"
def BRAIN(query):
document = fetchData()
topic,mood,question=findBest(query,document,'chitchat')
if mood !='not':
return (answerBest(topic,mood,document))
topic, mood,question=findBest(query,document,'comments')
if mood !='not':
return (answerBest(topic,mood,document))
topic,mood,question = findBestQuery(query,document,'question')
if mood =='callrepresentative':
return ('question', 'call', document['journeys']['callrepresentativeans']['button'])
isquickreply,answer=tryToHandleByQuickReply(mood,document)
if isquickreply==True:
return ("dummy", "dummy",answer)
answer = findBestAnswer(mood, document)
return (topic, mood,answer)