-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_search.py
52 lines (44 loc) · 2.19 KB
/
simple_search.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
import random
from fuzzywuzzy import process, fuzz
from text_normalization import clean
continuity = ["what else", "oh wow", "really?", "and?", "go on", "yeah", "yea", "ok", "yep", "great"]
class SimpleSearch(object):
def __init__(self, db_search, db_learn):
self._db_search = db_search
self._db_learn = db_learn
def get_response(self, text, past_intent=None, current_context=None):
next_i = []
if past_intent:
next_i = self._db_learn[past_intent]['context_set']
# check if 'what else'
match, rate = self.fuzzy_matching(text, continuity, 70)
if match != '':
if next_i != []:
if next_i[0] in self._db_learn:
response = random.choice(self._db_learn[next_i[0]]["responses"])
return response, next_i[0], self._db_learn[next_i[0]]['context'] # aargh!
# check exact match
match, confidence = self.fuzzy_matching(text, self._db_search.keys(), 65)
#check if match found
if match != '':
intent = random.choice(self._db_search[match]["intents"])
response = random.choice(self._db_learn[intent]["responses"])
new_context = self._db_learn[intent]["context"]
return response, intent, new_context
else:
if current_context:
i = 'fallback_' + str(current_context)
if i in self._db_learn:
return random.choice(self._db_learn[i]["responses"]), i, current_context
# select global fallback
return random.choice(self._db_learn['fallback_global']['responses']), 'fallback_global', '' # returning random from fallbacks, no intent/topic match
def fuzzy_matching(self, sentence, choices, confidence):
query = clean(sentence)
# TODO : more stuff for multiple matching sentences
# Get a list of matches ordered by score, default limit to 5
# process.extract(query, choices)
match = process.extractOne(query, choices, scorer=fuzz.token_set_ratio)
if match[1] > confidence:
return match[0], match[1]
else:
return '', 0