-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_all_regex.py
177 lines (157 loc) · 5.77 KB
/
test_all_regex.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
import re
from unidecode import unidecode
from total_regex import naive_score_total, regex_suffix, regex_prefix
from date_regex import (
naive_date_regex,
naive_score_date,
regex_date_suffix_time,
regex_date_prefix_time,
regex_date_suffix_time_en_format,
regex_date_prefix_time_en_format,
parse_datetime,
)
def test_regex(text: str, verbose=False):
text = unidecode(text).lower()
date_matches = (
[(match, i) for i, match in enumerate(re.finditer(naive_date_regex, text))]
+ [
(match, i)
for i, match in enumerate(re.finditer(regex_date_prefix_time, text))
]
+ [
(match, i)
for i, match in enumerate(re.finditer(regex_date_suffix_time, text))
]
+ [
(match, i)
for i, match in enumerate(
re.finditer(regex_date_prefix_time_en_format, text)
)
]
+ [
(match, i)
for i, match in enumerate(
re.finditer(regex_date_suffix_time_en_format, text)
)
]
)
possible_dates = []
for match, i in date_matches:
try:
(
parsed_date,
is_year_two_digit,
is_day_one_digit,
is_month_one_digit,
is_month_name,
has_time,
) = parse_datetime(match)
if "sep_day" in match.groupdict():
if match.group("sep_day") is not None:
sep_day = match.group("sep_day").strip()
else:
sep_day = match.group("sep_day_inner")
if "sep_month" in match.groupdict():
if match.group("sep_month") is not None:
sep_month = match.group("sep_month").strip()
else:
sep_month = match.group("sep_month_inner")
sep_hour = (
match.group("sep_hour").strip() if match.group("sep_hour") else None
)
sep_minute = (
match.group("sep_minute").strip() if match.group("sep_minute") else None
)
if "sep_date_inner" in match.groupdict() and match.group("sep_date_inner"):
sep_date = match.group("sep_date_inner").strip()
elif "sep_date_outer" in match.groupdict() and match.group(
"sep_date_outer"
):
sep_date = match.group("sep_date_outer").strip()
else:
sep_date = None
if "sep_time_inner" in match.groupdict() and match.group("sep_time_inner"):
sep_time = match.group("sep_time_inner").strip()
elif "sep_time_outer" in match.groupdict() and match.group(
"sep_time_outer"
):
sep_time = match.group("sep_time_outer").strip()
else:
sep_time = None
possible_dates.append(
(
naive_score_date(
{
"date": parsed_date,
"is_day_one_digit": is_day_one_digit,
"is_month_one_digit": is_month_one_digit,
"is_year_two_digit": is_year_two_digit,
"sep_day": sep_day,
"sep_month": sep_month,
"sep_hour": sep_hour,
"sep_minute": sep_minute,
"sep_time": sep_time,
"sep_date": sep_date,
"is_month_name": is_month_name,
"has_time": has_time,
"index": i,
},
len(date_matches),
),
parsed_date,
)
)
except Exception as e:
print(e)
pass # Impossible date found
possible_total = []
get_decimal_part = lambda match: int(match) * (10 if len(match) == 1 else 1) / 100
total_matches = [
(match, i) for i, match in enumerate(re.finditer(regex_prefix, text))
] + [(match, i) for i, match in enumerate(re.finditer(regex_suffix, text))]
for match, i in total_matches:
total_amount = round(
int(match.group("integer_part"))
+ (
get_decimal_part(match.group("decimal_part"))
if match.group("decimal_part")
else 0
),
2,
)
sep_decimal = (
match.group("sep_decimal").strip() if match.group("sep_decimal") else None
)
has_adjacent = match.group("adjacent") is not None
has_currency = (match.group("currency_prefix") is not None) or (
match.group("currency_suffix") is not None
)
possible_total.append(
(
naive_score_total(
{
"total": total_amount,
"has_adjacent": has_adjacent,
"has_currency": has_currency,
"sep_decimal": sep_decimal,
"index": i,
},
len(total_matches),
),
total_amount,
)
)
date_scores = sorted(possible_dates, key=lambda x: x[0], reverse=True) + [(0, None)]
total_scores = sorted(possible_total, key=lambda x: x[0], reverse=True) + [
(0, None)
]
if verbose:
print("Date matches:")
print(date_scores)
print("Total matches:")
print(total_scores)
date = date_scores[0]
total = total_scores[0]
if date[0] < 0.35:
return [1 - date[0], None], total
return date, total