-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemEntities.py
More file actions
297 lines (275 loc) · 12.7 KB
/
SystemEntities.py
File metadata and controls
297 lines (275 loc) · 12.7 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env python
# coding: utf-8
"""
File defining Entity Detector Function.
"""
import spacy
from spacy.tokenizer import Tokenizer
nlp = spacy.load("en_core_web_md")
tokenizer = Tokenizer(nlp.vocab)
from spacy.matcher import Matcher
import dateparser
def entity_detector(query, entities_to_detect = []):
"""
Input parameters:
- query::string
A string containing the query from which the entities are to be detected.
- entities_to_detect::list
A list containing the entities that are to be detected. The entities that can be detected and inputs
to be provided to the list are 'dates', 'times', 'ages', 'quantities', 'amounts', 'numbers', 'emails'.
Note: If only the query is passed as input, all of the entities are detected by default.
Output parameters:
- results::dict
Dict format:
{'entity_result': [{'entity': 'entity1', 'entity': 'entity2'}],
'entity_result': [{'entity': 'entity1', 'entity': 'entity2'}]}
Sample input:
entity_detector('Can you deliver thirteen apples and seventeen mangoes on 18th October?', ['dates', 'numbers'])
Sample output:
{'number_result': [{'number': 13}, {'number': 17}], 'date_result': [{'date': '2020-10-18 00:00:00'}]}
"""
doc = nlp(query)
results = {}
entities_all = False
if len(entities_to_detect) == 0:
entities_all = True
if entities_all:
matcher = Matcher(nlp.vocab)
num_pattern = [{"TEXT": {"REGEX": "^\d+$"}}]
alphanum_pattern = [{'POS': {'IN': ['NUM']}}]
matcher.add("Number", None, num_pattern, alphanum_pattern)
results.update({'number_result': []})
numbers = []
email = [{"TEXT": {"REGEX": "^[^\s@]+@[^\s@]+\.[^\s@]+$"}}]
matcher.add("Email", None, email)
results.update({'email_result': []})
dates = []
results.update({'date_result': []})
results.update({'age_result': []})
times = []
results.update({'time_result': []})
quantities = []
results.update({'quantity_result': []})
amounts = []
results.update({'money_result': []})
else:
if 'numbers' or 'emails' in entities_to_detect:
matcher = Matcher(nlp.vocab)
if 'numbers' in entities_to_detect:
num_pattern = [{"TEXT": {"REGEX": "^\d+$"}}]
alphanum_pattern = [{'POS': {'IN': ['NUM']}}]
matcher.add("Number", None, num_pattern, alphanum_pattern)
results.update({'number_result': []})
numbers = []
if 'emails' in entities_to_detect:
email = [{"TEXT": {"REGEX": "^[^\s@]+@[^\s@]+\.[^\s@]+$"}}]
matcher.add("Email", None, email)
results.update({'email_result': []})
for entity_to_detect in entities_to_detect:
if entity_to_detect == 'dates' or entity_to_detect == 'ages':
dates = []
if entities_all or entity_to_detect == 'ages':
results.update({'age_result': []})
if entities_all or entity_to_detect == 'dates':
results.update({'date_result': []})
elif entity_to_detect == 'times':
times = []
results.update({'time_result': []})
elif entity_to_detect == 'quantities':
quantities = []
results.update({'quantity_result': []})
elif entity_to_detect == 'amounts':
amounts = []
results.update({'money_result': []})
for ent in doc.ents:
if ent.label_ == 'DATE':
if entities_all or 'dates' in entities_to_detect or 'ages' in entities_to_detect:
dates.append([ent, ent.start, ent.end])
if dateparser.parse(str(ent)):
date = dateparser.parse(str(ent))
results['date_result'].append({'date': str(date)})
else:
if 'years' or 'age' in ent:
results['age_result'].append({'age': str(ent)})
else:
results['date_result'].append({'date': str(ent)})
if ent.label_ == 'TIME':
if entities_all or 'times' in entities_to_detect:
times.append([ent, ent.start, ent.end])
if dateparser.parse(str(ent)):
time = dateparser.parse(str(ent))
results['time_result'].append({'time': str(time)})
else:
results['time_result'].append({'time': str(ent)})
if ent.label_ == 'QUANTITY':
if entities_all or 'quantities' in entities_to_detect:
quantities.append([ent, ent.start, ent.end])
quantity_result = assign_unit(ent.text)
results['quantity_result'].append(quantity_result)
if ent.label_ == 'MONEY':
if entities_all or 'amounts' in entities_to_detect:
amounts.append([ent, ent.start, ent.end])
money_result = assign_unit(ent.text)
results['money_result'].append(money_result)
if 'numbers' or 'emails' in entities_to_detect:
for match_id, start, end in matcher(doc):
if nlp.vocab.strings[match_id] == 'Email':
if entities_all or 'emails' in entities_to_detect:
results['email_result'].append({'email': doc[start:end]})
if nlp.vocab.strings[match_id] == 'Number':
if entities_all or 'numbers' in entities_to_detect:
flag = False
if entities_all or 'dates' in entities_to_detect or 'ages' in entities_to_detect:
for entity in dates:
if doc[start:end].text in entity[0].text:
if start >= entity[1] and end <= entity[2]:
flag = True
if entities_all or 'times' in entities_to_detect:
for entity in times:
if doc[start:end].text in entity[0].text:
if start >= entity[1] and end <= entity[2]:
flag = True
if entities_all or 'quantities' in entities_to_detect:
for entity in quantities:
if doc[start:end].text in entity[0].text:
if start >= entity[1] and end <= entity[2]:
flag = True
if entities_all or 'amounts' in entities_to_detect:
for entity in amounts:
if doc[start:end].text in entity[0].text:
if start >= entity[1] and end <= entity[2]:
flag = True
if not flag:
numbers.append([doc[start:end], start, end])
if entities_all or 'numbers' in entities_to_detect:
textnum = []
text_nums = []
num_added = []
for j in range(0, len(numbers)):
if j == len(numbers) - 1:
if str(numbers[j][0]).isdigit():
results['number_result'].append({'number': numbers[j][0]})
else:
num = text2int(str(numbers[j][0]))
if num != 'Illegal word':
results['number_result'].append({'number': num})
else:
if (numbers[j][2] == numbers[j+1][1]) or (str(doc[numbers[j][2]]) == 'and' and numbers[j][2] + 1 == numbers[j+1][1]):
if textnum and j != 0:
if numbers[j-1][2] != numbers[j][1] and str(doc[numbers[j-1][2]]) != 'and':
text_nums.append(textnum)
textnum = []
if len(textnum) > 0:
if textnum[-1] != numbers[j][0]:
textnum.append(numbers[j][0])
num_added.append(numbers[j][1])
else:
textnum.append(numbers[j][0])
textnum.append(numbers[j+1][0])
num_added.append(numbers[j+1][1])
else:
if numbers[j][1] not in num_added:
if str(numbers[j][0]).isdigit():
results['number_result'].append({'number': numbers[j][0]})
else:
num = text2int(str(numbers[j][0]))
if num != 'Illegal word':
results['number_result'].append({'number': num})
if textnum:
text_nums.append(textnum)
for text_num in text_nums:
text = ''
for num in text_num:
text = text + ' ' + str(num)
num = text2int(text)
if num != 'Illegal word':
results['number_result'].append({'number': num})
return results
def text2int(textnum, numwords={}):
scalar = ["hundred", "thousand", "million", "billion", "trillion", "lakh", "crore"]
words = tokenizer(textnum)
if str(words[0]) in scalar:
textnum = 'one ' + textnum
flag = False
if 'lakh' in textnum or 'crore' in textnum:
flag = True
if not numwords:
units = [
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
"nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen",
]
tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
if flag:
scales = ["hundred", "thousand", "lakh", "crore"]
else:
scales = ["hundred", "thousand", "million", "billion", "trillion"]
numwords["and"] = (1, 0)
for idx, word in enumerate(units):
numwords[word] = (1, idx)
for idx, word in enumerate(tens):
numwords[word] = (1, idx * 10)
for idx, word in enumerate(scales):
if flag == True:
if idx <= 1:
numwords[word] = (10 ** (idx * 3 or 2), 0)
else:
numwords[word] = (10 ** (idx * 2 + 1), 0)
else:
numwords[word] = (10 ** (idx * 3 or 2), 0)
current = result = 0
for word in textnum.split():
if word not in numwords:
return 'Illegal word'
scale, increment = numwords[word]
current = current * scale + increment
if scale > 100:
result += current
current = 0
return result + current
def assign_unit(text):
words = tokenizer(text)
nums = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety", "hundred", "thousand", "lakh", "crore", "hundred", "thousand",
"lakh", "crore"]
flag = False
for word in words:
if str(word) in nums:
flag = True
break
if flag:
result = has_num(words)
else:
result = has_special_char(words)
return result
def has_special_char(words):
unit = ''
number = ''
if len(words) == 1:
for c in str(words[0]):
if not c.isdigit() and not c.isspace():
unit = unit + c
elif c.isdigit():
number = number + c
else:
for word in words:
if str(word).isdigit():
number = number + ' ' + str(word)
else:
unit = unit + ' ' + str(word)
return [{'number': number}, {'unit': unit}]
def has_num(words):
nums = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety", "hundred", "thousand", "lakh", "crore", "hundred", "thousand",
"lakh", "crore"]
unit = ''
number = ''
for word in words:
if str(word) not in nums:
unit = unit + ' ' + str(word)
else:
number = number + ' ' + str(word)
number = text2int(number)
return [{'number': number}, {'unit': unit}]