-
Notifications
You must be signed in to change notification settings - Fork 0
/
recognize.py
103 lines (90 loc) · 3.46 KB
/
recognize.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
import re
from utils import clear, print_
class Recognize():
"""
Named Entity and Intent Detector
"""
def __init__(self, entity_ptrn, intent_ptrn, name_ptrn, city_ptrn, product_ptrn, context_words):
self.entities_ptrn = entity_ptrn
self.intents_ptrn = intent_ptrn
self.name_ptrn = name_ptrn
self.city_ptrn = city_ptrn.replace('-', ' ')
self.product_ptrn = product_ptrn
self.context_words = context_words
self.entities = []
self.intents = []
self.name = ""
self.digit = []
def recognition_utterance(self, responses):
try:
for response in responses:
for result in response.results:
for alternative in result.recognition_result.alternatives:
print(alternative.transcript)
self.utterance = alternative.transcript
print("Phrase start:", result.recognition_result.start_time.ToTimedelta())
print("Phrase end: ", result.recognition_result.end_time.ToTimedelta())
except Exception as e:
print("Got exception in recognition_utterance", e)
raise
def search_entities(self):
self.entities = []
for pattern_name in self.entities_ptrn:
if re.findall(self.entities_ptrn[pattern_name], self.utterance.lower()):
self.entities.append(pattern_name)
print_("\nEntities", self.entities)
def search_intents(self):
self.intents = []
for pattern_name in self.intents_ptrn:
if re.findall(self.intents_ptrn[pattern_name], self.utterance.lower()):
self.intents.append(pattern_name)
print_("\nIntents", self.intents)
def search_products(self):
self.products = []
for pattern_name in self.product_ptrn:
if re.findall(self.product_ptrn[pattern_name], self.utterance.lower()):
self.products.append(pattern_name)
print_("\nProducts", self.products)
def search_name(self):
self.name = ""
for word in self.utterance.lower().split():
if re.findall(self.name_ptrn, word):
self.name = (clear(word)).title()
print_("\nName", self.name)
return
def search_city(self):
utterance = clear(self.utterance).split()
self.city = ""
tmp = []
while len(utterance) >= 2:
word1 = utterance.pop(0).title()
word2 = utterance[0].title()
tmp.append(word1)
word = word1 + ' ' + word2
if re.fullmatch(self.city_ptrn, word):
self.city = word
print_("\nCity", self.city)
return
if utterance:
tmp.append(utterance[0].title())
for word in tmp:
if re.findall(self.city_ptrn, word):
self.city = word
print_("\nCity", self.city)
return
def search_digit(self):
self.digit = []
self.digit = re.findall('\d+', self.utterance)
print_("\nDigit", " ".join(self.digit))
def entity(self, *entity):
for item in entity:
if item in self.entities:
return True
def intent(self, *intent):
for item in intent:
if item in self.intents:
return True
def product(self, *product):
for item in product:
if item in self.products:
return True