forked from HAKSOAT/place_to_live
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhere.py
454 lines (406 loc) · 14 KB
/
where.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
import pandas as pd
from utils import text_color
from utils import text_type
maxDiffCharTax = 1 / 2.325
df = pd.read_csv("city/output/list_of_countries.csv")
df_copy = df
def isSimilarTo(reference, model):
reference = reference.replace(' ', '')
model = model.replace(' ', '')
distance = damerau_levenshtein_distance(model, reference) / len(reference)
return distance <= maxDiffCharTax
"""
Compute the Damerau-Levenshtein distance between two given
strings (s1 and s2)
"""
def damerau_levenshtein_distance(s1, s2):
d = {}
lenstr1 = len(s1)
lenstr2 = len(s2)
for i in range(-1,lenstr1+1):
d[(i,-1)] = i+1
for j in range(-1,lenstr2+1):
d[(-1,j)] = j+1
for i in range(lenstr1):
for j in range(lenstr2):
if s1[i] == s2[j]:
cost = 0
else:
cost = 1
d[(i,j)] = min(
d[(i-1,j)] + 1, # deletion
d[(i,j-1)] + 1, # insertion
d[(i-1,j-1)] + cost, # substitution
)
if i and j and s1[i]==s2[j-1] and s1[i-1] == s2[j]:
d[(i,j)] = min (d[(i,j)], d[i-2,j-2] + cost) # transposition
return d[lenstr1-1,lenstr2-1]
def run_country_checker():
"""Checks for a valid country by checking df"""
while True:
try:
country = input(
text_color(
"What is your country? ", text_type.QUESTION,
),
)
country = country.lower()
country = country.title()
float(df[df.country == country]["purchasing_power_index"])
except TypeError:
similarCountries=[]
for i in range(len(df)):
if(isSimilarTo(df['country'][i], country)):
similarCountries.append(df['country'][i])
if(len(similarCountries) == 0):
message = f"'{country} is an invalid country. Please try again."
else:
message = f"{country} is an invalid country. Did you mean "
for i in range(len(similarCountries)):
message += f"{similarCountries[i]}"
if(i != len(similarCountries) - 1):
message += " or "
else:
message += "?"
print(text_color(message, text_type.WARNING))
else:
return country
YOUR_COUNTRY = run_country_checker()
def max_min_index(name_index):
"""Return maximum and minimum value with country of a column from df."""
country_and_name = df_copy[['country', name_index]]
counrties_in_name_index = country_and_name.sort_values(name_index).dropna()
min_value = [
list(counrties_in_name_index[name_index])[0],
list(counrties_in_name_index['country'])[0],
]
max_value = [
list(counrties_in_name_index[name_index])[-1],
list(counrties_in_name_index['country'])[-1],
]
return max_value, min_value
def print_question(name_index, max_min_value, mood):
"""
Return a question that asks the user about the status of a factor
in his country, also displays the maximum and minimum value of this factor
in the world.
"""
if mood == 'higher is better':
first_value = max_min_value[0]
second_value = max_min_value[1]
elif mood == 'lower is better':
first_value = max_min_value[1]
second_value = max_min_value[0]
message = text_color(
f"What is your desirable {name_index} ({mood})? "
f"The best score in the world is "
f"{first_value[0]} "
f"({first_value[1]}), "
f"the worst is {second_value[0]} "
f"({second_value[1]}) ", text_type.QUESTION,
)
return message
def _value_checker(index_input):
"""Helper function to input check the main index functions"""
if index_input == '': # empty string, default index
return "default"
try:
return float(index_input)
except ValueError:
return False
def purchase_power_func():
"""finds your purchase power index"""
country_purchasing_power_index = float(
df[df.country == YOUR_COUNTRY]["purchasing_power_index"],
)
print(
text_color(
f"In your country purchasing power "
f"index is {country_purchasing_power_index}", text_type.ANSWER,
),
)
while True:
index_input = (
input(
print_question(
'purchasing power index', max_min_purchasing,
'higher is better',
),
)
)
if isinstance(_value_checker(index_input), float):
return _value_checker(index_input)
elif _value_checker(index_input) == "default":
return country_purchasing_power_index
print(
text_color(
f"'{index_input}' is an invalid index. Please try again.",
text_type.WARNING,
),
)
def safety_func():
"""finds your safety index"""
country_safety_index = float(
df[df.country == YOUR_COUNTRY]["safety_index"],
)
print(
text_color(
f"In your country safety index is {country_safety_index}",
text_type.ANSWER,
),
)
while True:
index_input = (
input(
print_question('safety index', max_min_safety, 'higher is better'),
)
)
if isinstance(_value_checker(index_input), float):
return _value_checker(index_input)
elif _value_checker(index_input) == "default":
return country_safety_index
print(
text_color(
f"'{index_input}' is an invalid index. Please try again.",
text_type.WARNING,
),
)
def health_care_func():
"""finds your health care index"""
country_health_care_index = float(
df[df.country == YOUR_COUNTRY]["health_care_index"],
)
print(
text_color(
f"In your country health care index is {country_health_care_index}",
text_type.ANSWER,
),
)
while True:
index_input = (
input(
print_question(
'health care index', max_min_health,
mood='higher is better',
),
)
)
if isinstance(_value_checker(index_input), float):
return _value_checker(index_input)
elif _value_checker(index_input) == "default":
return country_health_care_index
print(
text_color(
f"'{index_input}' is an invalid index. Please try again.",
),
text_type.WARNING,
)
def climate_func():
"""finds your climate index"""
country_climate_index = float(
df[df.country == YOUR_COUNTRY]["climate_index"],
)
print(
text_color(
f"In your country climate index is {country_climate_index}",
text_type.ANSWER,
),
)
while True:
index_input = (
input(
print_question(
'climate index', max_min_climate,
'higher is better',
),
)
)
if isinstance(_value_checker(index_input), float):
return _value_checker(index_input)
elif _value_checker(index_input) == "default":
return country_climate_index
print(
text_color(
f"'{index_input}' is an invalid index. Please try again.",
text_type.WARNING,
),
)
def cost_of_living_func():
"""finds your cost of living index"""
country_cost_of_living_index = float(
df[df.country == YOUR_COUNTRY]["cost_of_living_index"],
)
print(
text_color(
f"In your country cost of living "
f"index is {country_cost_of_living_index}",
text_type.ANSWER,
),
)
while True:
index_input = (
input(
print_question(
'cost of living index', max_min_cost,
'lower is better',
),
)
)
if isinstance(_value_checker(index_input), float):
return _value_checker(index_input)
elif _value_checker(index_input) == "default":
return country_cost_of_living_index
print(
text_color(
f"'{index_input}' is an invalid index. Please try again.",
text_type.WARNING,
),
)
def property_price_to_income_ratio_func():
"""finds your property price to income ratio index"""
country_property_price_to_income_ratio = float(
df[df.country == YOUR_COUNTRY]["property_price_to_income_ratio"],
)
print(
text_color(
f"In your country house price to "
f"income ratio index is {country_property_price_to_income_ratio}",
text_type.ANSWER,
),
)
while True:
index_input = (
input(
print_question(
'property price to income ratio', max_min_property,
'lower is better',
),
)
)
if isinstance(_value_checker(index_input), float):
return _value_checker(index_input)
elif _value_checker(index_input) == "default":
return country_property_price_to_income_ratio
print(
text_color(
f"'{index_input}' is an invalid index. Please try again.",
text_type.WARNING,
),
)
def traffic_commute_time_func():
"""finds your traffic commute time index"""
country_traffic_commute_time_index = float(
df[df.country == YOUR_COUNTRY]["traffic_commute_time_index"],
)
print(
text_color(
f"In your country traffic commute time "
f"index is {country_traffic_commute_time_index}", text_type.ANSWER,
),
)
while True:
index_input = (
input(
print_question(
'traffic commute time index', max_min_traffic,
'lower is better',
),
)
)
if isinstance(_value_checker(index_input), float):
return _value_checker(index_input)
elif _value_checker(index_input) == "default":
return country_traffic_commute_time_index
print(
text_color(
f"'{index_input}' is an invalid index. Please try again.",
text_type.WARNING,
),
)
def pollution_func():
"""finds your pollution index"""
country_pollution_index = float(
df[df.country == YOUR_COUNTRY]["pollution_index"],
)
print(
text_color(
f"In your country pollution index is {country_pollution_index}",
text_type.ANSWER,
),
)
while True:
index_input = (
input(
print_question(
'pollution index', max_min_pollution,
'lower is better',
),
)
)
if isinstance(_value_checker(index_input), float):
return _value_checker(index_input)
elif _value_checker(index_input) == "default":
return country_pollution_index
print(
text_color(
f"'{index_input}' is an invalid index. Please try again.",
text_type.WARNING,
),
)
values = {
"purchasing_power_index": 200,
"safety_index": 200,
"health_care_index": 200,
"cost_of_living_index": 0,
"property_price_to_income_ratio": 0,
"traffic_commute_time_index": 0,
"pollution_index": 0,
"climate_index": 200,
}
df = df.fillna(value=values)
if __name__ == "__main__":
max_min_purchasing = max_min_index('purchasing_power_index')
max_min_safety = max_min_index('safety_index')
max_min_health = max_min_index('health_care_index')
max_min_cost = max_min_index('cost_of_living_index')
max_min_property = max_min_index('property_price_to_income_ratio')
max_min_traffic = max_min_index('traffic_commute_time_index')
max_min_pollution = max_min_index('pollution_index')
max_min_climate = max_min_index('climate_index')
your_purchasing_power_index = float(purchase_power_func())
your_safety_index = float(safety_func())
your_health_care_index = float(health_care_func())
your_climate_index = float(climate_func())
your_cost_of_living_index = float(cost_of_living_func())
your_property_price_to_income_ratio = float(
property_price_to_income_ratio_func(),
)
your_traffic_commute_time_index = float(traffic_commute_time_func())
your_pollution_index = float(pollution_func())
out_df = df[(df.purchasing_power_index > your_purchasing_power_index) &
(df.safety_index > your_safety_index) &
(df.health_care_index > your_health_care_index) &
(df.cost_of_living_index < your_cost_of_living_index) &
(
df.property_price_to_income_ratio <
your_property_price_to_income_ratio
) &
(
df.traffic_commute_time_index <
your_traffic_commute_time_index
) &
(df.pollution_index < your_pollution_index) &
(df.climate_index > your_climate_index)]
print_out_df = out_df[
["country", "freedomhouse_score", "quality_of_life_index"]
].dropna().sort_values(by=['freedomhouse_score'], ascending=False)
if print_out_df.empty:
print(
text_color(
f"There is no country better than {YOUR_COUNTRY}.",
text_type.ANSWER,
),
)
else:
with pd.option_context("display.max_rows", None, "display.max_columns", None):
print(text_color(print_out_df, text_type.ANSWER))