-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverify-TR-data.py
executable file
·105 lines (85 loc) · 3.23 KB
/
verify-TR-data.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
import pandas as pd
import json
def turkish_lower(s):
if not isinstance(s, str):
return s
return s.replace("I", "ı").replace("İ", "i").lower()
# Read the TSV file
df = pd.read_csv("generated_data/db.tsv", sep="\t")
# Filter for Turkey and get city names
tsv_cities = set(
df[df["country_code"].str.lower() == "tr"]["name"].apply(turkish_lower)
)
# Read the JSON file
with open("turkey_map.json", "r", encoding="utf-8") as f:
turkey_map = json.load(f)
# Flatten the JSON data into a set of city names
ts_cities = set(
turkish_lower(city) for cities in turkey_map.values() for city in cities
)
ts_states = set(turkish_lower(state) for state in turkey_map.keys())
# JSON is a bit wrong, sometimes it doesn't contain city centre in the cities list
ts_cities = ts_cities | ts_states
# Compare the sets
missing_in_ts = tsv_cities - ts_cities
missing_in_tsv = ts_cities - tsv_cities
print("Cities in TSV but not in JSON:")
for city in missing_in_ts:
print(f"- {city}")
print("\nCities in JSON but not in TSV:")
for city in missing_in_tsv:
print(f"- {city}")
print(f"\nTotal cities in TSV: {len(tsv_cities)}")
print(f"Total cities in JSON: {len(ts_cities)}")
# Additional analysis
if len(tsv_cities) == len(ts_cities) and len(missing_in_ts) == 0:
print("\nThe datasets match perfectly!")
else:
print("\nThere are discrepancies between the datasets.")
# Check for potential case mismatches or close matches
for tsv_city in missing_in_ts:
close_matches = [
ts_city
for ts_city in ts_cities
if turkish_lower(ts_city) == turkish_lower(tsv_city)
]
if close_matches:
print(
f"Possible case mismatch: '{tsv_city}' in TSV, '{close_matches[0]}' in JSON"
)
# State-level comparison
tsv_states = set(
df[df["country_code"].str.lower() == "tr"]["state_name"].apply(turkish_lower)
)
missing_states_in_ts = tsv_states - ts_states
missing_states_in_tsv = ts_states - tsv_states
print("\nStates in TSV but not in JSON:")
for state in missing_states_in_ts:
print(f"- {state}")
print("\nStates in JSON but not in TSV:")
for state in missing_states_in_tsv:
print(f"- {state}")
# Detailed state-city comparison
print("\nDetailed state-city comparison:")
for state, cities in turkey_map.items():
state_lower = turkish_lower(state)
if state_lower in tsv_states:
tsv_state_cities = set(
df[
(df["country_code"].str.lower() == "tr")
& (df["state_name"].apply(turkish_lower) == state_lower)
]["name"].apply(turkish_lower)
)
json_state_cities = set(turkish_lower(city) for city in cities) | set([state])
missing_in_json = tsv_state_cities - json_state_cities
missing_in_tsv = json_state_cities - tsv_state_cities
if missing_in_json or missing_in_tsv:
print(f"\nState: {state}")
if missing_in_json:
print(" Cities in TSV but not in JSON:")
for city in missing_in_json:
print(f" - {city}")
if missing_in_tsv:
print(" Cities in JSON but not in TSV:")
for city in missing_in_tsv:
print(f" - {city}")