ValadisCERTH commited on
Commit
470f592
·
1 Parent(s): 6e8d873

Create helper.py

Browse files
Files changed (1) hide show
  1. helper.py +156 -0
helper.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spacy
2
+ import re
3
+
4
+ from datetime import datetime
5
+
6
+
7
+ # load the spacy model
8
+ spacy.cli.download("en_core_web_lg")
9
+ nlp = spacy.load("en_core_web_lg")
10
+
11
+
12
+ # Define a function to extract dates from text
13
+ def extract_dates(text):
14
+ """
15
+ Identify dates both in numeric and free-text from text, using date regex patterns and NER tag
16
+ """
17
+
18
+ # Define regex patterns for common date formats
19
+ # Regular expressions that include the \b word boundary character to ensure that the date pattern only matches if it is not part of a longer pattern that has already been matched
20
+ date_patterns = [
21
+ r'\b\d{1,2}[-/]\d{1,2}[-/]\d{2,4}\b', # Matches dates like "01/01/22" or "1-1-2022"
22
+ r'\b\d{1,2}[-/]\d{1,2}\b(?!\d)', # Matches dates like "01/01" or "1-1"
23
+ r'\b[A-Z][a-z]{2,8} \d{1,2},? \d{2,4}\b', # Matches dates like "January 1, 2022" or "Feb 28, 22"
24
+ r'\b\d{1,2} [A-Z][a-z]{2,8} \d{2,4}\b', # Matches dates like "1 January 2022" or "28 Feb 22"
25
+ r'\b[A-Z][a-z]{2,8} \d{2,4}\b', # Matches dates like "January 2022" or "Feb 22"
26
+
27
+ ]
28
+
29
+ # Find all matches for date patterns in the text
30
+ matches = []
31
+ for pattern in date_patterns:
32
+ for match in re.findall(pattern, text):
33
+
34
+ # Check if the match is part of a longer date pattern that has already been matched
35
+ if all(match not in m for m in matches):
36
+ matches.append(match)
37
+
38
+ # Use SpaCy to extract additional dates
39
+ doc = nlp(text)
40
+
41
+ for ent in doc.ents:
42
+ if ent.label_ == 'DATE':
43
+ date_str = ent.text
44
+
45
+ # Checks each SpaCy date reference against the matches list to ensure that it is not already included
46
+ if all(date_str not in m for m in matches):
47
+ matches.append(date_str)
48
+
49
+ # Remove duplicates and return the matches
50
+ return list(set(matches))
51
+
52
+
53
+ def convert_dates(date_list):
54
+ """
55
+ Assign to the identified formatted dates the proper date format and then, on the formatted dates, assign the relevant date tags (e.g. specify which is the day, the month, etc)
56
+ """
57
+
58
+ DATE_FORMATS = {
59
+ '%B %d, %Y': 'day:{dt.day}, month:{dt.month}, year:{dt.year}',
60
+ '%-m-%d-%Y': 'day:{dt.day}, month:{dt.month}, year:{dt.year}',
61
+ '%m-%d-%y': 'day:{dt.day}, month:{dt.month}, year:{dt.year}',
62
+ '%d/%m': 'day:{dt.day}, month:{dt.month}',
63
+ '%B %d': 'day:{dt.day}, month:{dt.month}',
64
+ '%b %d': 'day:{dt.day}, month:{dt.month}',
65
+ '%B %Y': 'month:{dt.month}, year:{dt.year}',
66
+ '%Y': 'year:{dt.year}',
67
+ '%d/%m/%y': 'day:{dt.day}, month:{dt.month}, year:{dt.year}',
68
+ '%B %d, %y': 'day:{dt.day}, month:{dt.month}, year:{dt.year}',
69
+ '%b %d, %y': 'day:{dt.day}, month:{dt.month}, year:{dt.year}',
70
+ '%d-%m-%Y': 'day:{dt.day}, month:{dt.month}, year:{dt.year}',
71
+ '%d/%m/%Y': 'day:{dt.day}, month:{dt.month}, year:{dt.year}',
72
+ '%d-%m-%y': 'day:{dt.day}, month:{dt.month}, year:{dt.year}',
73
+ '%m/%d/%y': 'day:{dt.day}, month:{dt.month}, year:{dt.year}',
74
+ '%m/%d/%Y': 'day:{dt.day}, month:{dt.month}, year:{dt.year}',
75
+ '%m-%d-%Y': 'day:{dt.day}, month:{dt.month}, year:{dt.year}',
76
+ '%m-%d-%y': 'day:{dt.day}, month:{dt.month}, year:{dt.year}',
77
+ '%d/%m/%Y %H:%M:%S': 'day:{dt.day}, month:{dt.month}, year:{dt.year}, time:{dt.strftime("%H:%M:%S")}',
78
+ '%d/%m/%y %H:%M:%S': 'day:{dt.day}, month:{dt.month}, year:{dt.year}, time:{dt.strftime("%H:%M:%S")}',
79
+ '%m/%d/%Y %H:%M:%S': 'day:{dt.day}, month:{dt.month}, year:{dt.year}, time:{dt.strftime("%H:%M:%S")}',
80
+ '%m/%d/%y %H:%M:%S': 'day:{dt.day}, month:{dt.month}, year:{dt.year}, time:{dt.strftime("%H:%M:%S")}',
81
+ '%Y-%m-%d': 'day:{dt.day}, month:{dt.month}, year:{dt.year}',
82
+ '%y-%m-%d': 'day:{dt.day}, month:{dt.month}, year:{dt.year}',
83
+ '%m-%d-%Y %H:%M:%S': 'day:{dt.day}, month:{dt.month}, year:{dt.year}, time:{dt.strftime("%H:%M:%S")}',
84
+ '%m-%d-%y %H:%M:%S': 'day:{dt.day}, month:{dt.month}, year:{dt.year}, time:{dt.strftime("%H:%M:%S")}',
85
+ '%m-%d': 'month:{dt.month}, day:{dt.day}',
86
+ '%-m-%-d': 'month:{dt.month}, day:{dt.day}',
87
+ '%d %b %y': 'day:{dt.day}, month:{dt.month}, year:{dt.year}',
88
+ '%d %B %Y': 'day:{dt.day}, month:{dt.month}, year:{dt.year}',
89
+ '%b %Y': 'month:{dt.month}, year:{dt.year}',
90
+ '%b %d, %Y': 'day:{dt.day}, month:{dt.month}, year:{dt.year}',
91
+ '%d %B %y': 'day:{dt.day}, month:{dt.month}, year:{dt.year}'
92
+ }
93
+
94
+
95
+ output_list = []
96
+ for date_str in date_list:
97
+ valid_format = False
98
+ for fmt, out_fmt in DATE_FORMATS.items():
99
+ try:
100
+ dt = datetime.strptime(date_str, fmt)
101
+ output_list.append(out_fmt.format(dt=dt))
102
+ valid_format = True
103
+ break
104
+ except ValueError:
105
+ pass
106
+ if not valid_format:
107
+ # Attempt to parse using a custom format
108
+ try:
109
+ if '-' in date_str:
110
+ dt = datetime.strptime(date_str, '%m-%d-%y')
111
+ else:
112
+ dt = datetime.strptime(date_str, '%d/%m/%y')
113
+ output_list.append(f'day:{dt.day}, month:{dt.month}, year:{dt.year}')
114
+ except ValueError:
115
+ output_list.append(f'INVALID FORMAT: {date_str}')
116
+ return output_list
117
+
118
+
119
+
120
+ def dates_binding(text):
121
+ '''
122
+ This is a function that binds together all the subcomponents of the dates identification, while also controlling for multiple, or zero date references
123
+ '''
124
+
125
+ try:
126
+
127
+ # capture the referred dates
128
+ identified_dates = extract_dates(text)
129
+
130
+ # we only accept for one date reference
131
+ if len(identified_dates) == 1:
132
+
133
+ formatted_dates = convert_dates(identified_dates)
134
+
135
+
136
+ # in case there is a wrong date format then return the appropriate code to prompt back the proper message
137
+ if 'INVALID FORMAT' in formatted_dates[0]:
138
+ return (0,'DATES','wrong_date_format')
139
+
140
+ else:
141
+ return formatted_dates
142
+
143
+ # in case of zero references return the appropriate code (to aid returning the correct prompt)
144
+ elif len(identified_dates) == 0:
145
+ return (0,'DATES','no_date')
146
+
147
+ # in case of more than one references return the appropriate code (to aid returning the correct prompt)
148
+ elif len(identified_dates) > 1:
149
+ return (0,'DATES','more_dates')
150
+
151
+ # in case of unexpected error return the appropriate code (to aid returning the correct prompt)
152
+ else:
153
+ return (0,'DATES','unknown_error')
154
+
155
+ except:
156
+ return (0,'DATES','unknown_error')