-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
ranges.py
51 lines (42 loc) · 1.79 KB
/
ranges.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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
from recognizers_date_time import recognize_datetime
from recognizers_text import Culture
class Ranges:
"""
TIMEX expressions can represent date and time ranges. Here are a couple of examples.
"""
@staticmethod
def date_range():
# Run the recognizer.
results = recognize_datetime(
"Some time in the next two weeks.", Culture.English
)
# We should find a single result in this example.
for result in results:
# The resolution includes a single value because there is no ambiguity.
# We are interested in the distinct set of TIMEX expressions.
distinct_timex_expressions = {
value["timex"]
for value in result.resolution["values"]
if "timex" in value
}
# The TIMEX expression can also capture the notion of range.
print(f"{result.text} ({','.join(distinct_timex_expressions)})")
@staticmethod
def time_range():
# Run the recognizer.
results = recognize_datetime(
"Some time between 6pm and 6:30pm.", Culture.English
)
# We should find a single result in this example.
for result in results:
# The resolution includes a single value because there is no ambiguity.
# We are interested in the distinct set of TIMEX expressions.
distinct_timex_expressions = {
value["timex"]
for value in result.resolution["values"]
if "timex" in value
}
# The TIMEX expression can also capture the notion of range.
print(f"{result.text} ({','.join(distinct_timex_expressions)})")