-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCorrect_Spelling_Graphml.py
53 lines (47 loc) · 2.15 KB
/
Correct_Spelling_Graphml.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
__author__ = 'williewonka'
import os
from fuzzywuzzy import fuzz
#open the streams
streamin = open("Crisis of the Confederation Map.gml", "r")
streamout = open("Crisis of the Confederation Map Corrected.gml", "w")
#read all the filenames in the province folder, use the one run for loop as a hack to get data from generator
# (no idea how those are supposed to work properly)
existing_provinces = {}
for info in os.walk('provinces'):
files = info[2]
for file in files:
name = file.split("-")[1].strip().split(".")[0]
id = int(file.split("-")[0].strip())
existing_provinces[name] = id
break
#iterate through file
cache = ""
for line in streamin:
if "label" in line or "text" in line:#if there is a name in the current line
name_ori = line.split('"')[1].strip("\n").strip()#isolate the name
#check if the name is already correctly in the province files, then skip this one
if name_ori in existing_provinces or "Sector" in name_ori:
streamout.writelines(line)
continue
#if not see if there is a similar name in the list, if so than correct the name to this
newline = line
for province in existing_provinces:
#caLculate fuzzy ratio
ratio = fuzz.ratio(province, name_ori)
if ratio > 75:
if cache == "":
print("original name '" + name_ori + "' is maybe '" + province + "' with id '" + str(existing_provinces[province]) + "'")
#ask for confirmation to change, ask only 1 per 2 changes because of the gml format everything is double (for label and text)
if cache == "":
choice = input("Accept? (y/n) ")
if choice == "y":
newline = line.replace(name_ori, province).strip("\n")
print("Changed line to: " + newline)
cache = choice
else:
if cache == "y":
newline = line.replace(name_ori, province).strip("\n")
cache = ""
streamout.writelines(newline)
else:
streamout.writelines(line)