/
base_duration.py
620 lines (475 loc) · 21.7 KB
/
base_duration.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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
from abc import ABC, abstractmethod
from typing import List, Optional, Pattern, Dict, Match
from datetime import datetime
import regex
from recognizers_text.utilities import QueryProcessor
from recognizers_text.extractor import ExtractResult
from recognizers_number.number.extractors import BaseNumberExtractor
from recognizers_number.number.parsers import BaseNumberParser
from .constants import Constants, TimeTypeConstants
from .extractors import DateTimeExtractor
from .parsers import DateTimeParser, DateTimeParseResult
from .utilities import Token, merge_all_tokens, DateTimeResolutionResult, RegExpUtility,\
DateTimeOptionsConfiguration, DateTimeOptions, DurationParsingUtil, RegExpUtility
class DurationExtractorConfiguration(DateTimeOptionsConfiguration):
@property
@abstractmethod
def all_regex(self) -> Pattern:
raise NotImplementedError
@property
@abstractmethod
def half_regex(self) -> Pattern:
raise NotImplementedError
@property
@abstractmethod
def followed_unit(self) -> Pattern:
raise NotImplementedError
@property
@abstractmethod
def number_combined_with_unit(self) -> Pattern:
raise NotImplementedError
@property
@abstractmethod
def an_unit_regex(self) -> Pattern:
raise NotImplementedError
@property
@abstractmethod
def inexact_number_unit_regex(self) -> Pattern:
raise NotImplementedError
@property
@abstractmethod
def suffix_and_regex(self) -> Pattern:
raise NotImplementedError
@property
@abstractmethod
def relative_duration_unit_regex(self) -> Pattern:
raise NotImplementedError
@property
@abstractmethod
def cardinal_extractor(self) -> BaseNumberExtractor:
raise NotImplementedError
@property
@abstractmethod
def during_regex(self) -> Pattern:
raise NotImplementedError
@property
@abstractmethod
def unit_map(self) -> {}:
raise NotImplementedError
@property
@abstractmethod
def unit_value_map(self) -> {}:
raise NotImplementedError
@property
@abstractmethod
def duration_unit_regex(self) -> Pattern:
raise NotImplementedError
@property
@abstractmethod
def duration_connector_regex(self) -> Pattern:
raise NotImplementedError
@property
@abstractmethod
def more_than_regex(self) -> Pattern:
raise NotImplementedError
@property
@abstractmethod
def less_than_regex(self) -> Pattern:
raise NotImplementedError
@property
@abstractmethod
def check_both_before_after(self):
raise NotImplementedError
class BaseDurationExtractor(DateTimeExtractor):
@property
def extractor_type_name(self) -> str:
return Constants.SYS_DATETIME_DURATION
def __init__(self, config: DurationExtractorConfiguration, merge: bool = True):
self.config = config
self.merge = merge
def extract(self, source: str, reference: datetime = None) -> List[ExtractResult]:
if reference is None:
reference = datetime.now()
tokens = self.number_with_unit(source)
tokens.extend(self.number_with_unit_and_suffix(source, tokens))
tokens.extend(self.implicit_duration(source))
results = merge_all_tokens(tokens, source, self.extractor_type_name)
# First MergeMultipleDuration then ResolveMoreThanOrLessThanPrefix so cases like "more than
# 4 days and less than 1 week" will not be merged into one "multipleDuration"
if self.merge:
results = self.merge_multiple_duration(source, results)
results = self.tag_inequality_prefix(source, results)
return results
# Handle cases look like: {more than | less than} {duration}?
def tag_inequality_prefix(self, text: str, extract_results: [ExtractResult]):
for extract_result in extract_results:
before_string = text[0: extract_result.start]
after_string = text[extract_result.start: extract_result.length]
is_inequality_prefix_matched = False
is_match_after = False
match = RegExpUtility.match_end(self.config.more_than_regex, before_string, True)
# The second condition is necessary so for "1 week" in "more than 4 days and less than
# 1 week", it will not be tagged incorrectly as "more than"
if match and match.success:
extract_result.data = TimeTypeConstants.MORE_THAN_MOD
is_inequality_prefix_matched = True
if not is_inequality_prefix_matched:
match = RegExpUtility.match_end(self.config.less_than_regex, before_string, True)
if match and match.success:
extract_result.data = TimeTypeConstants.LESS_THAN_MOD
is_inequality_prefix_matched = True
if is_inequality_prefix_matched:
if not is_match_after:
extract_result.length += extract_result.start - text.index(match.group())
extract_result.start = text.index(match.group())
extract_result.text = text[extract_result.start: extract_result.start + extract_result.length]
else:
extract_result.length += match.index + match.length
extract_result.text = text[extract_result.start: extract_result.length]
return extract_results
def merge_multiple_duration(self, text: str, extractor_results: [ExtractResult]):
if len(extractor_results) <= 1:
return extractor_results
unit_map = self.config.unit_map
unit_value_map = self.config.unit_value_map
unit_regex = self.config.duration_unit_regex
result = []
first_extraction_index = 0
time_unit = 0
total_unit = 0
while first_extraction_index < len(extractor_results):
cur_unit = None
unit_match = unit_regex.search(extractor_results[first_extraction_index].text)
if unit_match and str(RegExpUtility.get_group(unit_match, Constants.UNIT)) in unit_map:
cur_unit = str(RegExpUtility.get_group(unit_match, Constants.UNIT))
total_unit += 1
if DurationParsingUtil.is_time_duration_unit(unit_map[cur_unit]):
time_unit += 1
if not cur_unit:
first_extraction_index += 1
continue
second_extraction_index = first_extraction_index + 1
while second_extraction_index < len(extractor_results):
valid = False
mid_str_begin = extractor_results[second_extraction_index - 1].start +\
extractor_results[second_extraction_index - 1].length if \
extractor_results[second_extraction_index - 1].length else 0
mid_str_end = extractor_results[second_extraction_index].start \
if extractor_results[second_extraction_index].start else 0
mid_str = text[mid_str_begin: mid_str_end]
match = self.config.duration_connector_regex.search(mid_str)
if match:
unit_match = unit_regex.search(extractor_results[second_extraction_index].text)
if unit_match and str(RegExpUtility.get_group(unit_match, 'unit')) in unit_map:
next_unit_str = str(RegExpUtility.get_group(unit_match, 'unit'))
if unit_value_map[next_unit_str] != unit_value_map[cur_unit]:
valid = True
if unit_value_map[next_unit_str] < unit_value_map[cur_unit]:
cur_unit = next_unit_str
total_unit += 1
if DurationParsingUtil.is_time_duration_unit(unit_map[next_unit_str]):
time_unit += 1
if not valid:
break
second_extraction_index += 1
if second_extraction_index - 1 > first_extraction_index:
node: ExtractResult = ExtractResult()
node.start = extractor_results[first_extraction_index].start
node.length = extractor_results[second_extraction_index - 1].start +\
extractor_results[second_extraction_index - 1].length -\
node.start
node.text = text[node.start or 0: node.length + node.start or 0]
node.type = extractor_results[first_extraction_index].type
if time_unit == total_unit:
duration_type = Constants.MULTIPLE_DURATION_TIME
elif time_unit == 0:
duration_type = Constants.MULTIPLE_DURATION_DATE
else:
duration_type = Constants.MULTIPLE_DURATION_DATE_TIME
node.data = duration_type
result.append(node)
time_unit = 0
total_unit = 0
else:
result.append(extractor_results[first_extraction_index])
first_extraction_index = second_extraction_index
return result
# simple cases of a number followed by unit
def number_with_unit(self, source: str) -> List[Token]:
extract_results: List[ExtractResult] = self.config.cardinal_extractor.extract(
source)
result: List[Token] = list(
filter(None, map(lambda x: self.__cardinal_to_token(x, source), extract_results)))
# handle "3hrs"
result.extend(self.get_tokens_from_regex(
self.config.number_combined_with_unit, source))
# handle "an hour"
result.extend(self.get_tokens_from_regex(
self.config.an_unit_regex, source))
# handle "few" related cases
result.extend(self.get_tokens_from_regex(
self.config.inexact_number_unit_regex, source))
return result
# handle cases look like: {number} {unit}? and {an|a} {half|quarter} {unit}?
# define the part "and {an|a} {half|quarter}" as Suffix
def number_with_unit_and_suffix(self, source: str, tokens: List[Token]) -> List[Token]:
result: List[Token] = list(
filter(None, map(lambda x: self.__base_to_token(x, source), tokens)))
return result
# handle cases that don't contain number
def implicit_duration(self, source: str) -> List[Token]:
# handle "all day", "all year"
result: List[Token] = self.get_tokens_from_regex(
self.config.all_regex, source)
# handle "half day", "half year"
result.extend(self.get_tokens_from_regex(
self.config.half_regex, source))
# handle "next day", "last year"
result.extend(self.get_tokens_from_regex(
self.config.relative_duration_unit_regex, source))
# handle "during/for the day/week/month/year"
if (self.config.options & DateTimeOptions.CALENDAR) != 0:
result.extend(self.get_tokens_from_regex(
self.config.during_regex, source)
)
return result
def __cardinal_to_token(self, cardinal: ExtractResult, source: str) -> Optional[Token]:
after = source[cardinal.start + cardinal.length:]
match = regex.match(self.config.followed_unit, after)
if match is not None:
return Token(cardinal.start, cardinal.start + cardinal.length + len(match.group()))
return None
def __base_to_token(self, token: Token, source: str) -> Optional[Token]:
after = source[token.start + token.length:]
match = regex.match(self.config.suffix_and_regex, after)
if match is not None:
return Token(token.start, token.start + token.length + len(match.group()))
return None
@staticmethod
def get_tokens_from_regex(pattern: Pattern, source: str) -> List[Token]:
return list(map(lambda x: Token(x.start(), x.end()), regex.finditer(pattern, source)))
class DurationParserConfiguration(ABC):
@property
@abstractmethod
def cardinal_extractor(self) -> BaseNumberExtractor:
raise NotImplementedError
@property
@abstractmethod
def number_parser(self) -> BaseNumberParser:
raise NotImplementedError
@property
@abstractmethod
def followed_unit(self) -> Pattern:
raise NotImplementedError
@property
@abstractmethod
def suffix_and_regex(self) -> Pattern:
raise NotImplementedError
@property
@abstractmethod
def number_combined_with_unit(self) -> Pattern:
raise NotImplementedError
@property
@abstractmethod
def an_unit_regex(self) -> Pattern:
raise NotImplementedError
@property
@abstractmethod
def all_date_unit_regex(self) -> Pattern:
raise NotImplementedError
@property
@abstractmethod
def half_date_unit_regex(self) -> Pattern:
raise NotImplementedError
@property
@abstractmethod
def inexact_number_unit_regex(self) -> Pattern:
raise NotImplementedError
@property
@abstractmethod
def unit_map(self) -> Dict[str, str]:
raise NotImplementedError
@property
@abstractmethod
def unit_value_map(self) -> Dict[str, int]:
raise NotImplementedError
@property
@abstractmethod
def double_numbers(self) -> Dict[str, float]:
raise NotImplementedError
class BaseDurationParser(DateTimeParser):
@property
def parser_type_name(self) -> str:
return Constants.SYS_DATETIME_DURATION
def __init__(self, config: DurationParserConfiguration):
self.config = config
def parse(self, source: ExtractResult, reference: datetime = None) -> Optional[DateTimeParseResult]:
if reference is None:
reference = datetime.now()
value = None
if source.type is self.parser_type_name:
source_text = source.text.lower()
inner_result = self.parse_number_with_unit(source_text, reference)
if not inner_result.success:
inner_result = self.parse_implicit_duration(
source_text, reference)
if inner_result.success:
inner_result.future_resolution[TimeTypeConstants.DURATION] = str(
inner_result.future_value)
inner_result.past_resolution[TimeTypeConstants.DURATION] = str(
inner_result.past_value)
value = inner_result
res = value
if res and source.data:
if source.data == TimeTypeConstants.MORE_THAN_MOD:
res.mod = TimeTypeConstants.MORE_THAN_MOD
elif source.data == TimeTypeConstants.LESS_THAN_MOD:
res.mod = TimeTypeConstants.LESS_THAN_MOD
result = DateTimeParseResult()
result.text = source.text
result.start = source.start
result.length = source.length
result.type = source.type
result.data = source.data
result.value = value
result.timex_str = '' if not value else value.timex
result.resolution_str = ''
return result
# simple cases made by a number followed an unit
def parse_number_with_unit(self, source: str, reference: datetime) -> DateTimeResolutionResult:
source = source.strip()
result = self.parse_number_space_unit(source)
if not result.success:
result = self.parse_number_combined_unit(source)
if not result.success:
result = self.parse_an_unit(source)
if not result.success:
result = self.parse_in_exact_number_unit(source)
return result
# handle cases that don't contain numbers
def parse_implicit_duration(self, source: str, reference: datetime) -> DateTimeResolutionResult:
source = source.strip()
result = self.get_result_from_regex(
self.config.all_date_unit_regex, source, 1)
if not result.success:
result = self.get_result_from_regex(
self.config.half_date_unit_regex, source, 0.5)
if not result.success:
result = self.get_result_from_regex(
self.config.followed_unit, source, 1)
return result
def get_result_from_regex(self, pattern: Pattern, source: str, num: float) -> DateTimeResolutionResult:
result = DateTimeResolutionResult()
match: Match = regex.search(pattern, source)
if match is None:
return result
source_unit: str = match.group(Constants.UNIT) or ''
if source_unit not in self.config.unit_map:
return result
num = QueryProcessor.float_or_int(num)
unit = self.config.unit_map[source_unit]
is_time = Constants.UNIT_T if self.is_less_than_day(unit) else ''
result.timex = f'P{is_time}{num}{unit[0]}'
result.future_value = QueryProcessor.float_or_int(
num * self.config.unit_value_map[source_unit])
result.past_value = result.future_value
result.success = True
return result
def parse_number_space_unit(self, source: str) -> DateTimeResolutionResult:
result = DateTimeResolutionResult()
# if there are spaces between number and unit
ers = self.config.cardinal_extractor.extract(source)
if len(ers) != 1:
return result
suffix = source
source_unit = ''
er = ers[0]
pr = self.config.number_parser.parse(er)
no_num = source[pr.start + pr.length:].strip().lower()
match = regex.search(self.config.followed_unit, no_num)
if match is not None:
suffix = RegExpUtility.get_group(match, Constants.SUFFIX_GROUP_NAME)
source_unit = RegExpUtility.get_group(match, Constants.UNIT)
if source_unit not in self.config.unit_map:
return result
num = float(pr.value) + self.parse_number_with_unit_and_suffix(suffix)
unit = self.config.unit_map[source_unit]
num = QueryProcessor.float_or_int(num)
is_time = 'T' if self.is_less_than_day(unit) else ''
result.timex = f'P{is_time}{num}{unit[0]}'
result.future_value = QueryProcessor.float_or_int(
num * self.config.unit_value_map[source_unit])
result.past_value = result.future_value
result.success = True
return result
def parse_number_with_unit_and_suffix(self, source: str) -> float:
match = regex.search(self.config.suffix_and_regex, source)
if match is not None:
num = match.group(Constants.SUFFIX_NUM_GROUP_NAME) or ''
return self.config.double_numbers.get(num, 0)
return 0
def parse_number_combined_unit(self, source: str) -> DateTimeResolutionResult:
result = DateTimeResolutionResult()
# if there are NO spaces between number and unit
match = regex.search(self.config.number_combined_with_unit, source)
if match is None:
return result
num = float(match.group(Constants.NUM)) + self.parse_number_with_unit_and_suffix(source)
source_unit = match.group(Constants.UNIT) or ''
if source_unit not in self.config.unit_map:
return result
unit = self.config.unit_map[source_unit]
if num > 1000 and unit in [Constants.UNIT_Y, Constants.UNIT_MON, Constants.UNIT_W]:
return result
num = QueryProcessor.float_or_int(num)
is_time = Constants.UNIT_T if self.is_less_than_day(unit) else ''
result.timex = f'P{is_time}{num}{unit[0]}'
result.future_value = QueryProcessor.float_or_int(
num * self.config.unit_value_map[source_unit])
result.past_value = result.future_value
result.success = True
return result
def parse_an_unit(self, source: str) -> DateTimeResolutionResult:
result = DateTimeResolutionResult()
match = regex.search(self.config.an_unit_regex, source)
if match is None:
match = regex.search(self.config.half_date_unit_regex, source)
if match is None:
return result
num = (0.5 if match.group(Constants.HALF) else 1) + self.parse_number_with_unit_and_suffix(source)
source_unit = match.group(Constants.UNIT) or ''
if source_unit not in self.config.unit_map:
return result
num = QueryProcessor.float_or_int(num)
unit = self.config.unit_map[source_unit]
is_time = Constants.UNIT_T if self.is_less_than_day(unit) else ''
result.timex = f'P{is_time}{num}{unit[0]}'
result.future_value = QueryProcessor.float_or_int(
num * self.config.unit_value_map[source_unit])
result.past_value = result.future_value
result.success = True
return result
def parse_in_exact_number_unit(self, source: str) -> DateTimeResolutionResult:
result = DateTimeResolutionResult()
match = regex.search(self.config.inexact_number_unit_regex, source)
if match is None:
return result
# set the inexact number "few", "some" to 3 for now
num = float(3)
source_unit = match.group(Constants.UNIT) or ''
if source_unit not in self.config.unit_map:
return result
unit = self.config.unit_map[source_unit]
if num > 1000 and unit in [Constants.UNIT_Y, Constants.UNIT_MON, Constants.UNIT_W]:
return result
num = QueryProcessor.float_or_int(num)
is_time = Constants.UNIT_T if self.is_less_than_day(unit) else ''
result.timex = f'P{is_time}{num}{unit[0]}'
result.future_value = QueryProcessor.float_or_int(
num * self.config.unit_value_map[source_unit])
result.past_value = result.future_value
result.success = True
return result
@staticmethod
def is_less_than_day(source: str) -> bool:
return source in [Constants.UNIT_H, Constants.UNIT_M, Constants.UNIT_S]