-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex_datetime.py
323 lines (274 loc) · 14.1 KB
/
regex_datetime.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
import csv
import io
import os
import re
import warnings
import dateutil.parser
REGEX_PARTS = {
'Y': r"(?:19[4-9]\d|20[0-3]\d)", # 1940 to 2039
'y': r"(?:\d\d)", # 00 to 99
'm': r"(?:1[012]|0?[1-9])", # 0?1 to 12
'mz': r"(?:1[012]|0[1-9])", # 01 to 12
'B': r"(?:"
r"D?JAN(?:UAR[IY])?|"
r"[FP]EB(?:RUAR[IY])?|"
r"MAC|MAR(?:CH|ET)?|MRT|"
r"APR(?:IL)?|"
r"M[EA]I|MAY|"
r"JUNE?|D?JUNI?|"
r"JUL(?:Y|AI)?|D?JULI?|"
r"OG(?:OS)?|AUG(?:UST)?|AGT?(?:USTUS)?|"
r"SEP(?:T(?:EMBER)?)?|"
r"O[KC]T(?:OBER)?|"
r"NO[VP](?:EMBER)?|"
r"D[EI][SC](?:EMBER)?"
r")",
'd': r"(?:3[01]|[12]\d|0?[1-9])", # 0?1 to 31
'd_range': r"(?:3[01]|[12]\d|0?[1-9])(?: ?[-] ?(?:3[01]|[12]\d|0?[1-9]))?", # 14-15
'dz': r"(?:3[01]|[12]\d|0[1-9])", # 01 to 31
'j': r"(?:36[0-6]|3[0-5]\d|[12]\d\d|0?[1-9]\d|0?0?[1-9])", # 0?0?1 to 366
'H': r"(?:2[0-4]|[01]?\d)", # 0?0 to 24
'HZ': r"(?:2[0-4]|[01]\d)", # 0?0 to 24
'I': r"(?:1[012]|0?[1-9])", # 0?1 to 12
'M': r"(?:[1-5]\d|0\d)", # 00 to 59
'S': r"(?:6[01]|[0-5]\d)", # 00 to 61 (leap second)
'p': r'(?:MIDNI(?:GHT|TE)|AFTERNOON|MORNING|NOON|[MN]N|H(?:(?:OU)?RS?)?|[AP]\.? ?M\.?)',
'p2': r'(?:MIDNI(?:GHT|TE)|NOON|[AP]\.? ?M\.?)',
'Z': r"(?:A(?:C(?:DT|ST|T|WST)|DT|E(?:DT|ST)|FT|K(?:DT|ST)|M(?:ST|T)|RT|ST|WST"
r"|Z(?:O(?:ST|T)|T))|B(?:DT|I(?:OT|T)|OT|R(?:ST|T)|ST|TT)|C(?:AT|CT|DT|E("
r"?:ST|T)|H(?:A(?:DT|ST)|O(?:ST|T)|ST|UT)|I(?:ST|T)|KT|L(?:ST|T)|O(?:ST|T"
r")|ST|T|VT|WST|XT)|D(?:AVT|DUT|FT)|E(?:A(?:S(?:ST|T)|T)|CT|DT|E(?:ST|T)|"
r"G(?:ST|T)|IT|ST)|F(?:ET|JT|K(?:ST|T)|NT)|G(?:A(?:LT|MT)|ET|FT|I(?:LT|T)"
r"|MT|ST|YT)|H(?:AEC|DT|KT|MT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dd_mm_YYYY_5': r"(?:{d}{m}{Y})", # too many phone numbers
'dd_mm_YYYY_6': r"(?:{d} ?{m} ?{Y})",
'dd_mm_YYYY_7': r"(?:{dz}{mz}{Y})",
# 14/8/91
'dd_mm_yy_1': r"(?:{d}/{m}/{y})",
'dd_mm_yy_2': r"(?:{d}\\{m}\\{y})",
'dd_mm_yy_3': r"(?:{d}[-]{m}[-]{y})",
'dd_mm_yy_4': r"(?:{d}\.{m}\.{y})",
# 'dd_mm_yy_5': r"(?:{dz}{mz}{y})", # too many phone numbers
# 14 Aug, 1991
'dd_mmm_YYYY_1': r"(?:{d}{th}? ?/ ?{B} ?/ ?{Y})",
'dd_mmm_YYYY_2': r"(?:{d}{th}? ?\\ ?{B} ?\\ ?{Y})",
'dd_mmm_YYYY_3': r"(?:{d}{th}? ?[-] ?{B} ?[ -] ?{Y})",
'dd_mmm_YYYY_4': r"(?:{d}{th}? ?[ -]? ?{B} ?,? ?{Y})",
'dd_mmm_YYYY_5': r"(?:{d}{th}? ?\. ?{B} ?\. ?{Y})",
# 14 Aug '91
'dd_mmm_yy_1': r"(?:{d}{th}? ?/ ?{B} ?/ ?'?{y})",
'dd_mmm_yy_2': r"(?:{d}{th}? ?\\ ?{B} ?\\ ?'?{y})",
'dd_mmm_yy_3': r"(?:{d}{th}? ?[-] ?{B} ?[-] ?'?{y})",
'dd_mmm_yy_4': r"(?:{d}{th}? ?[ -]? ?{B} ?,? ?'?{y})",
'dd_mmm_yy_5': r"(?:{d}{th}? ?\. ?{B} ?\. ?'?{y})",
# 14th Aug
'dd_mmm': r"(?:{d}{th}? ?[/\\. -] ?{B})",
# 08/14/1991 # WARNING! dateutil set to day first
'mm_dd_YYYY_1': r"(?:{m}/{d}/{Y})",
'mm_dd_YYYY_2': r"(?:{m}\\{d}\\{Y})",
'mm_dd_YYYY_3': r"(?:{m}[-]{d}[-]{Y})",
'mm_dd_YYYY_4': r"(?:{m} {d} {Y})",
'mm_dd_YYYY_5': r"(?:{m}\.{d}\.{Y})",
'mm_dd_YYYY_6': r"(?:{mz}{dz}{Y})",
# 8/14/91 # WARNING! dateutil set to day first
'mm_dd_yy_1': r"(?:{m}/{d}/{y})",
'mm_dd_yy_2': r"(?:{m}\\{d}\\{y})",
'mm_dd_yy_3': r"(?:{m}[-]{d}[-]{y})",
'mm_dd_yy_4': r"(?:{m}\.{d}\.{y})",
# 'mm_dd_yy_5': r"(?:{mz}{dz}{y})", # too many phone numbers
# Aug 14th, 1991
'mmm_dd_YYYY_1': r"(?:{B} ?/ ?{d}{th}? ?/ ?{Y})",
'mmm_dd_YYYY_2': r"(?:{B} ?\\ ?{d}{th}? ?\\ ?{Y})",
'mmm_dd_YYYY_3': r"(?:{B} ?[-] ?{d}{th}? ?[ -] ?{Y})",
'mmm_dd_YYYY_4': r"(?:{B} ?[ -]? ?{d}{th}? ?, ?{Y})",
'mmm_dd_YYYY_5': r"(?:{B} ?\. ?{d}{th}? ?\. ?{Y})",
# Aug-14 '91
'mmm_dd_yy_1': r"(?:{B} ?/ ?{d}{th}? ?/ ?'?{y})",
'mmm_dd_yy_2': r"(?:{B} ?\\ ?{d}{th}? ?\\ ?'?{y})",
'mmm_dd_yy_3': r"(?:{B} ?[-] ?{d}{th}? ?[-] ?'?{y})",
'mmm_dd_yy_4': r"(?:{B} ?[. -]? ?{d}{th}?, '?{y})",
'mmm_dd_yy_5': r"(?:{B} ?\. ?{d}{th}? ?\. ?'?{y})",
# Aug-14 # WARNING! dateutil assumes current year
'mmm_dd': r"(?:{B} ?[/\\. -] ?{d}{th}?)",
# # Aug-91
# 'mmm_yy': r"(?:{B} ?[/\\. -] ?'{y})", # too many false positives
# August 1991
'mmm_YYYY': r"(?:{B} ?[/\\. -] ?{Y})", # many non-useful dates
# 1991-8-14
'YYYY_mm_dd_1': r'(?:{Y}/{m}/{d})',
'YYYY_mm_dd_2': r'(?:{Y}\\{m}\\{d})',
'YYYY_mm_dd_3': r'(?:{Y}[-]{m}[-]{d})',
'YYYY_mm_dd_4': r'(?:{Y} {m} {d})',
'YYYY_mm_dd_5': r'(?:{Y}\.{m}\.{d})',
'YYYY_mm_dd_6': r'(?:{Y}{mz}{dz})',
# 910814 (ISO 8601)
# 'yy_mm_dd_1': r"(?:{y} {m} {d})", # too many random numbers
'yy_mm_dd_2': r"(?:{y}/{m}/{d})",
'yy_mm_dd_3': r"(?:{y}\\{m}\\{d})",
'yy_mm_dd_4': r"(?:{y}[-]{m}[-]{d})",
'yy_mm_dd_5': r"(?:{y}\.{m}\.{d})",
# 'yy_mm_dd_6': r"(?:{y}{mz}{dz})", # too many phone numbers
# 1991-Aug-14
'YYYY_mmm_dd_1': r"(?:{Y} ?/ ?{B} ?/ ?{d})",
'YYYY_mmm_dd_2': r"(?:{Y} ?\\ ?{B} ?\\ ?{d})",
'YYYY_mmm_dd_3': r"(?:{Y} ?[-] ?{B} ?[-] ?{d})",
'YYYY_mmm_dd_4': r"(?:{Y} ?{B} ?[ -]? ?{d}{th}?)",
# 91-Aug-14
'yy_mmm_dd_1': r"(?:'?{y} ?/ ?{B} ?/ ?{d})",
'yy_mmm_dd_2': r"(?:'?{y} ?\\ ?{B} ?\\ ?{d})",
'yy_mmm_dd_3': r"(?:'?{y} ?[-] ?{B} ?[-] ?{d})",
'yy_mmm_dd_4': r"(?:'?{y} ?{B} ?[ -]? ?{d}{th}?)",
# # 1991.226 (Aug 14 = day 226 in 1991) # dateutil fails
# 'YYYY_ddd_1': r"(?:{Y}\.{j})", # too many random numbers
# 'YYYY_ddd_2': r"(?:{Y}[-]{j})", # too many random numbers
# time
'HH_MM_SS': r"(?:{H}:{M}:{S}(?: ?{p})?(?: ?(?:Z|{Z}|{z}))?)",
'HH_MZ_pp_1': r"(?:{H}:{M}(?: ?{p})?(?: ?(?:Z|{Z}|{z}))?)",
'HH_MZ_pp_1b': r"(?:{H}[:. ]{M}(?: ?{p})(?: ?(?:Z|{Z}|{z}))?)",
'HH_MZ_pp_2': r"(?:(?<!\.){HZ}[. ]?{M}(?: ?{p})(?: ?(?:Z|{Z}|{z}))?)",
'HH_pp': r"(?:(?<!\.){H} ?{p2}(?: ?(?:Z|{Z}|{z}))?)",
# # 910814094500 (9:45am)
# 'yy_mm_dd_HH_MM_SS': r"(?:{y}{mz}{dz}{H}{M}{S})", # too many phone numbers
# 1991-08-14T09:45:00Z
'YYYY_mm_dd_HH_MM': r"(?:{Y}[-]{m}[-]{d}[T ]{H}:{M}(?: ?(?:Z|{Z}|{z}))?)",
'YYYY_mm_dd_HH_MM_SS_1': r"(?:{Y}[-]{m}[-]{d}[T ]{H}:{M}:{S}(?: ?(?:Z|{Z}|{z}))?)",
'YYYY_mm_dd_HH_MM_SS_2': r"(?:{Y}{mz}{d}T?{H}{M}{S}(?: ?(?:Z|{Z}|{z}))?)",
# # standalone
# 'day': r"{A}", # too many false positives
# 'month': r"{B}", # too many false positives
# 'year': r"{Y}", # too many random numbers
# 'timezone': r"(?:Z|{Z}|{z})", # too many malay words
}
# unicode fixes
REGEX_FORMATTED = {label: '\\b' +
pattern
.format(**REGEX_PARTS) # fill in the chunks
.replace("-]", "\u2009\u2010\u2011\u2012\u2013\u2014-]") # unicode dashes
.replace("'?", "['\u2018\u2019]?") # unicode quotes
+ '\\b'
for label, pattern in REGEX_PATTERNS_PARSERS.items()}
# match emails and urls to avoid returning chunks of them
REGEX_FORMATTED['eml'] = r'''[a-zA-Z0-9][^\s`!@%$^={}\[\]/\\"',()<>:;]+(?:@|%40|\s+at\s+|\s*<\s*at\s*>\s*)[a-zA-Z0-9][-_a-zA-Z0-9~.]+\.[a-zA-Z]{2,15}'''
REGEX_FORMATTED['url'] = r'\b(?:(?:https?|ftp|file)://|www\d?\.|ftp\.)[-A-Z0-9+&@#/%=~_|$?!:,.]*[A-Z0-9+&@#/%=~_|$]'
REGEX_FORMATTED['dot'] = r'(?:\d+\.){3,}\d+'
# compile all the regex patterns
REGEX_COMPILED = {label: re.compile(pattern, flags=re.I | re.U) for label, pattern in REGEX_FORMATTED.items()}
HEADERS = ['PATH',
'FILE_ID',
'REGEX_LABEL',
'LINE_NUM',
'MATCH',
'START',
'END',
'MATCH_LEN',
'NORM_LINE_LEN',
'CONTEXT',
'PARSED',
]
def parse_txt(path):
with io.open(path, mode='r', encoding='utf8') as f:
return os.path.basename(path), f.readlines()
def regex_text(text, longest=True, context_max_len=999, dayfirst=True):
# join multiple spaces, convert tabs, strip leading/trailing whitespace
text = ' '.join(text.split())
matches = []
for regex_label, regex_obj in REGEX_COMPILED.items():
for m in regex_obj.finditer(text):
context_start = max(0, (m.start() + m.end() - context_max_len) // 2)
context_end = min(len(text), context_start + context_max_len)
context_str = text[context_start:context_end]
if context_start != 0:
context_str = '\u2026' + context_str[1:]
if context_end != len(text):
context_str = context_str[:-1] + '\u2026' # this is the `...` character
parsed_date = None
try:
with warnings.catch_warnings():
warnings.simplefilter('ignore', category=dateutil.parser.UnknownTimezoneWarning)
if 'HH' in regex_label:
if 'dd' in regex_label or 'YYYY' in regex_label:
matched_text = re.sub(r'[\\]', '/', m.group())
parsed_date = dateutil.parser.parse(matched_text, dayfirst=dayfirst)
else:
matched_text = re.sub(r'H(?:(?:OU)?RS?)?', '', m.group(), flags=re.I)
matched_text = re.sub(r'MN', r'AM', matched_text, flags=re.I)
matched_text = re.sub(r'NN', r'PM', matched_text, flags=re.I)
matched_text = re.sub(r'(\d)[. ](\d)', r'\1:\2', matched_text)
matched_text = f'2001-01-01 {matched_text}'
parsed_date = dateutil.parser.parse(matched_text, dayfirst=dayfirst).time()
elif 'dd' in regex_label or 'YYYY' in regex_label:
matched_text = re.sub(r'[\\]', '/', m.group())
parsed_date = dateutil.parser.parse(matched_text, dayfirst=dayfirst).date()
except ValueError:
pass
matches.append({'REGEX_LABEL': regex_label,
'MATCH': m.group(),
'START': m.start(),
'END': m.end(),
'MATCH_LEN': m.end() - m.start(),
'NORM_TEXT_LEN': len(text),
'CONTEXT': context_str,
'PARSED': parsed_date,
})
# narrow to longest match
for match in matches:
if not longest or all((other['START'] >= match['START'] and other['END'] <= match['END']) or
other['START'] > match['END'] or
other['END'] < match['START']
for other in matches):
# don't return emails or urls
if match['REGEX_LABEL'] not in {u'eml', 'url', 'dot'}:
yield match
def regex_file(path, parser=parse_txt):
path = os.path.abspath(path)
file_name, file_lines = parser(path) #
for line_num, line in enumerate(file_lines):
for match_info in regex_text(line):
yield [path,
file_name,
match_info['REGEX_LABEL'],
line_num,
match_info['MATCH'],
match_info['START'],
match_info['END'],
match_info['MATCH_LEN'],
match_info['NORM_TEXT_LEN'],
match_info['CONTEXT'],
match_info['PARSED'],
]
if __name__ == '__main__':
SOURCE_FILES = ['regex_datetime_test.txt', 'README.md']
OUTPUT_CSV = 'found.csv'
with io.open(OUTPUT_CSV, mode='w', encoding='utf8', newline='') as f:
c = csv.writer(f)
c.writerow(HEADERS)
for path in SOURCE_FILES:
print('READING FROM:', os.path.abspath(path))
for row in regex_file(path):
c.writerow(row)
print('OUTPUT CSV: ', os.path.abspath(OUTPUT_CSV))