-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
227 lines (184 loc) · 7.67 KB
/
utils.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
from collections import Counter
from Config import MISSINGVALUE, MISSINGDATE, ALL_CA_PROVINCES, province_dic, \
CA_REGIONS,PLACE_PREPOSITIONS, STOP_WORDS
import re
from datetime import datetime
import string
import mysql.connector
class utils(object):
def extract_int(record, field_name):
value = record[field_name]
if len(value) == 0:
return MISSINGVALUE
try:
return int(value)
except ValueError:
return MISSINGVALUE
def extract_float(record, field_name):
value = record[field_name]
if len(value) == 0:
return MISSINGVALUE
try:
return float(value)
except ValueError:
return MISSINGVALUE
def extract_str(record, field_name):
value = record[field_name]
if len(value) == 0:
return 'unknown'
else:
return value
def extract_date(record, field_name):
date_format = '%m/%d/%Y'
value = record[field_name]
if len(value) == 0:
return MISSINGDATE
try:
return datetime.strptime(value, date_format)
except ValueError:
return MISSINGDATE
def get_location(place):
# version 2
locations = []
place = place.replace("(", "").replace(")", "")
while len(place) > 0:
found = False
# Search in provinces and territories
for province_abbr, province in ALL_CA_PROVINCES:
if province_abbr in place:
idx1 = place.index(province_abbr)
idx2 = idx1 + len(province_abbr)
elif province in place:
idx1 = place.index(province)
idx2 = idx1 + len(province)
else:
continue
cities_info = place[0:idx1].strip()
if cities_info.startswith("and "):
cities_info = cities_info[4:]
place = place[idx2:].strip()
cities = []
for city in cities_info.replace(" and ", ",").replace(";", ",").split(","):
city = city.strip()
if len(city) > 0 and city[0] in string.ascii_uppercase:
cities.append(city)
if len(cities) > 0:
for city in cities:
locations.append([city, province, 'Canada', 'y'])
else:
maincity = 'unknown'
if province in province_dic.keys():
maincity = province_dic[province]
locations.append([maincity, province, 'Canada', 'y'])
found = True
break
if found:
continue
# Search in region list
for region in CA_REGIONS:
if region in place:
idx1 = place.index(region)
idx2 = idx1 + len(region)
place = place[idx2:]
provinces = CA_REGIONS[region]
if provinces is not None:
for province in provinces:
# parse regions to provinces and their main cities
maincity = 'unknown'
if province in province_dic.keys():
maincity = province_dic[province]
locations.append([maincity, province, 'Canada', 'y'])
else:
locations.append([region, region, 'Canada', 'y'])
found = True
break
if found:
continue
# Assume the place is in from City, State, Country or City,Country or Country
for prep in PLACE_PREPOSITIONS:
place = place.replace(prep, ",")
cities_info = []
for t in place.split(","):
t = t.strip()
if len(t) > 0 and t[0].isupper():
cities_info.append(t)
if 0 < len(cities_info) <= 3:
country = cities_info[-1]
valid_country = True
for term in country.split():
if not term[0].isupper():
valid_country = False
break
if valid_country:
place = ""
found = True
if len(cities_info) == 3:
is_canada = ''
if country == 'Canada':
is_canada = 'y'
else:
is_canada = 'n'
if country == 'City': country = 'unknown'
locations.append([cities_info[0], cities_info[1], country, is_canada])
elif len(cities_info) == 2:
is_canada = ''
if country == 'Canada':
is_canada = 'y'
else:
is_canada = 'n'
if country == 'City': country = 'unknown'
locations.append([cities_info[0], 'unknown', country, is_canada])
elif len(cities_info) == 1:
is_canada = ''
if country == 'Canada':
is_canada = 'y'
else:
is_canada = 'n'
if country == 'City': country = 'unknown'
locations.append(['unknown', 'unknown', country, is_canada])
if not found:
break
# final filtering
for location in locations:
for key in province_dic.keys():
# if city information cannot be parsed from input string, use its main city
if key.lower() in location[0].lower():
if 'and' in location[0].lower():
result = [province_dic[location[1]], location[1], 'Canada', 'y']
locations.append(result)
location[0] = province_dic[key]
location[1] = key
if location[1] == 'Québec': location[1] = 'Quebec'
return locations
def extract_keywords(text, num_keywords=3):
counter = Counter(w for w in re.split('\W+', text.lower())
if len(w) > 2 and w not in STOP_WORDS)
return counter.most_common(num_keywords)
def sql_connection(pwd):
db_connection = mysql.connector.connect(
host="localhost",
user="root",
passwd=""
)
return db_connection
def check_balanced(df, column):
'''
check if the data set is balanced using the given column as label.
Return each label's distribution percentage to see if the dataset is balanced.
:return:
(Dict): dictionary has key=label_name, value=label percentage
'''
# vc is pandas series
vc = df[column].value_counts()
# row counts of df
row_count = df.shape[0]
result_dict = {}
for i, v in vc.items():
result_dict[i] = v, v/row_count
return result_dict
def print_info(text=None):
print("================================================")
print("| |")
print("|", text, " "*(len("================================================")-len(text)-5) , "|")
print("| |")
print("================================================")