forked from Tiiiger/templm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field_transformation.py
229 lines (199 loc) · 7.65 KB
/
field_transformation.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
import calendar
import re
_e2e_field_transformations_complex = {
("food", "Fast food"): ["Fast food", "fast food"],
("customer rating", "1 out of 5"): ["1 out of 5", "low"],
("customer rating", "3 out of 5"): ["3 out of 5", "average"],
("customer rating", "5 out of 5"): ["5 out of 5", "high"],
("priceRange", "less than £20"): ["less than £20", "cheap"],
("priceRange", "£20-25"): ["£20-25", "moderate"],
("priceRange", "more than £30"): ["more than £30", "high"],
("familyFriendly", "yes"): [
"family friendly",
"kid friendly",
"children friendly",
"family-friendly",
"child friendly",
],
("familyFriendly", "no"): [
"not family friendly",
"not kid friendly",
"not children friendly",
"not family-friendly",
"not child friendly",
],
}
_e2e_field_transformations_simple = {
("food", "Fast food"): ["Fast food", "fast food"],
("familyFriendly", "yes"): ["family friendly", "family-friendly",],
("familyFriendly", "no"): ["not family friendly", "not family-friendly",],
}
def e2e_field_transformation(data_table, complex=True):
if complex:
_e2e_transform = _e2e_field_transformations_complex
else:
_e2e_transform = _e2e_field_transformations_simple
new_data_table = dict()
for field_name, field_content in data_table.items():
if (field_name, field_content) in _e2e_transform:
field_content = _e2e_transform[(field_name, field_content)]
else:
field_content = [field_content]
new_data_table[field_name] = field_content
new_data_table["article"] = ["a", "an"]
return new_data_table
class SearchDate:
regex_exps = [
re.compile(f"^(\d{{1,2}})[,\s]+([a-zA-Z]+)[,\s]+(\d+)$"), # day month year
re.compile(f"^([a-zA-Z]+)[,\s]+(\d{{3,4}})$"), # month year
re.compile(f"^([a-zA-Z]+)[,\s]+(\d{{1,2}})[,\s]+(\d+)$"), # month day year
re.compile(f"^(\d+)[,\s]+([a-zA-Z]+)[,\s]+(\d{{1,2}})$"), # year month day
re.compile(f"^(\d{{3,4}})\s+(\d{{1,2}})\s+(\d{{1,2}})$"), # year month day
re.compile(f"^(\d{{1,2}})\s+(\d{{1,2}})\s+(\d{{3,4}})$"), # month day year
re.compile(f"^(\d{{3,4}})$"), # year
re.compile(f"^(\d{{3,4}})\s+(\d{{1,2}})$"),
] # year month
regex_date_assignments = [
[3, 2, 1], # index into year, month, day pattern
[2, 1, 0],
[3, 1, 2],
[1, 2, 3],
[1, 2, 3],
[3, 1, 2],
[1, 0, 0],
[1, 2, 0],
] # 0 means absent
def __init__(self):
self.year, self.month, self.day = 0, 0, 0
self.target_date_strings = set()
def find_date_in_input(self, input_string):
input_string = input_string.replace("yes", "")
input_string = input_string.replace("ca", "")
input_string = input_string.replace("a.d", "")
input_string = input_string.replace("c.", "")
input_string = input_string.replace("?", "")
input_string = input_string.replace("-", " ")
input_string = input_string.replace(".", "")
input_string = input_string.replace("around", "")
input_string = re.sub(r"\(.*\)", "", input_string)
input_string = input_string.strip()
for idx, month_abre in enumerate(list(calendar.month_abbr)):
input_string = input_string.replace(
f"{month_abre.lower()} ", f"{calendar.month_name[idx].lower()} "
)
found, found_idx, search_ojb = False, -1, None
for idx, exp in enumerate(self.regex_exps):
search_ojb = re.search(exp, input_string)
if search_ojb is not None:
found, found_idx = True, idx
break
if found:
self.year, self.month, self.day = [
search_ojb.group(item) if item != 0 else "0"
for item in self.regex_date_assignments[found_idx]
]
if not self.month.isnumeric():
self.month = (
list(calendar.month_name).index(self.month.capitalize())
if self.month.capitalize() in list(calendar.month_name)
else None
)
if self.month is None:
return False
self.year, self.month, self.day = (
int(self.year),
int(self.month),
int(self.day),
)
if self.month > 12:
self.month, self.day = self.day, self.month
return found
def generate_equiv(self, input_string):
date_strings = self.target_date_strings
date_strings.add(input_string)
found = self.find_date_in_input(input_string)
day_string, month_string, year_string = "", "", ""
if found:
if self.day != 0:
day_string = {f"{self.day}", f"{self.day:02}"}
if self.month != 0:
month_string = {
f"{self.month}",
f"{self.month:02}",
f"{calendar.month_abbr[self.month].lower()}",
f"{calendar.month_name[self.month].lower()}",
}
if self.year != 0:
year_string = [f"{self.year}"]
return day_string, month_string, year_string
def sb_field_transformation(input_table):
data_table = input_table.copy()
if "birth_date" in data_table:
search_date = SearchDate()
day_str, mon_str, year_str = search_date.generate_equiv(
data_table["birth_date"]
)
if day_str != "":
data_table["birth_date_day"] = day_str
if mon_str != "":
data_table["birth_date_month"] = mon_str
if year_str != "":
data_table["birth_date_year"] = year_str
del data_table["birth_date"]
if "death_date" in data_table:
search_date = SearchDate()
day_str, mon_str, year_str = search_date.generate_equiv(
data_table["death_date"]
)
if day_str != "":
data_table["death_date_day"] = day_str
if mon_str != "":
data_table["death_date_month"] = mon_str
if year_str != "":
data_table["death_date_year"] = year_str
del data_table["death_date"]
if "gender" in data_table:
gender = data_table["gender"]
if gender == "male":
data_table["relation"] = ["son"]
data_table["pronoun_a"] = ["he"]
data_table["pronoun_b"] = ["him"]
data_table["pronoun_c"] = ["his"]
data_table["pronoun_d"] = ["man"]
if gender == "female":
data_table["relation"] = ["daughter"]
data_table["pronoun_a"] = ["she"]
data_table["pronoun_b"] = ["her"]
data_table["pronoun_c"] = ["her"]
data_table["pronoun_d"] = ["woman"]
if gender == "non-binary":
data_table["pronoun_a"] = ["they"]
data_table["pronoun_b"] = ["them"]
data_table["pronoun_c"] = ["their"]
to_delete = []
for k, v in data_table.items():
if v in ["none", "", "no info", "unknown"]:
to_delete.append(k)
continue
if isinstance(v, str):
if v[-1] in [",", "."]:
v = v[:-1]
data_table[k] = [v]
for k in to_delete:
del data_table[k]
data_table["article"] = ["a", "an"]
data_table["be"] = ["is", "are", "was", "were"]
data_table["number"] = [
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
]
del data_table["notable_type"]
return data_table