forked from raana23/FaNumberFixer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FaNumberFixer.py
159 lines (140 loc) · 5.52 KB
/
FaNumberFixer.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# fanumberfixer a gmail d com
# Converting English digits [1234567890] and also Arabic digits [٤٥٦] into their equivalents in Perisan encoding [۴۵۶].
import re,fileinput,sys,os,datetime
import xml.etree.ElementTree as ET
sys.path.append(os.path.dirname(os.path.realpath(__file__))+"/modules")
import persian
import pytz
debugging = True
os.system('cls')
def log(text):
print(text)
text = str(text)
if debugging == True:
now = datetime.datetime.now(pytz.timezone('Asia/Tehran'))
with open(os.path.dirname(os.path.realpath(__file__))+"/log.txt", 'ab') as file:
text = now.strftime("%Y-%m-%d %H:%M:%S")+": "+text+"\n"
text = text.encode('utf8')
file.write(text)
log("* Loading File")
tree = ET.parse('input.osm') #Source input file name
root = tree.getroot()
log("* File loaded, Fixing.")
counter = 0
issuecounter = 0
ar_numbers = 0
en_numbers = 0
# if the name has one of the following accepted_chars (allowed chars) apply the fixes. doing so to avoid editing names which are not farsi.
accepted_chars = ['ا','آ','ب','پ','ت','ث','ج','چ','ح','خ','د','ذ','ر','ز','ژ','س','ش','ص','ض','ط','ظ','ع','غ','ف','ق','ک','گ','ل','م','ن','و','ه','ی','1','2','3','4','5','6','7','8','9','۰','۱','۲','۳','۴','۵','۶','۷','۸','۹','٤','٥','٦']
# do not edit names which have one of this characters in them.
ignore_list = ['ك','ي','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',]
log("Working on Nodes:")
for node in root.findall('node'):
for tag in node.findall('tag'):
k = tag.attrib['k']
if k=='name':
name = tag.attrib['v']
if any(char in name for char in ignore_list):
log(" id:"+node.attrib['id']+" name:'"+name+"' matched in ignore_list , it didn't get touched")
continue
if any(char in name for char in accepted_chars):
temp = tag.attrib['v']
v_fixed=persian.convert_ar_numbers(name)
if temp != v_fixed:
ar_numbers = ar_numbers+1
issuecounter = issuecounter+1
temp = v_fixed
v_fixed=persian.convert_en_numbers(v_fixed)
if temp != v_fixed:
en_numbers = en_numbers+1
issuecounter = issuecounter+1
temp = v_fixed
if v_fixed != name:
counter=counter+1
tag.attrib['v'] = v_fixed
node.set('action', 'modify')
else:
log (" Warning: id:"+node.attrib['id']+" name:'"+name+"' did not matched in accepted_chars , it didn't get touched")
log (str(counter) + " node With "+str(issuecounter)+" Issue Fixed. ("+str(ar_numbers)+" Arabic Numbers - "+str(en_numbers)+" English Numbers)")
counter = 0
issuecounter = 0
ar_numbers = 0
en_numbers = 0
log("Working on Ways:")
for way in root.findall('way'):
for tag in way.findall('tag'):
k = tag.attrib['k']
if k=='name':
name = tag.attrib['v']
if any(char in name for char in ignore_list):
log(" id:"+node.attrib['id']+" name:'"+name+"' matched in ignore_list , it didn't get touched")
continue
if any(char in name for char in accepted_chars):
temp = tag.attrib['v']
v_fixed=persian.convert_ar_numbers(name)
if temp != v_fixed:
ar_numbers = ar_numbers+1
issuecounter = issuecounter+1
temp = v_fixed
v_fixed=persian.convert_en_numbers(v_fixed)
if temp != v_fixed:
en_numbers = en_numbers+1
issuecounter = issuecounter+1
temp = v_fixed
if v_fixed != name:
counter=counter+1
tag.attrib['v'] = v_fixed
way.set('action', 'modify')
else:
log (" Warning: id:"+node.attrib['id']+" name:'"+name+"' did not matched in accepted_chars , it didn't get touched")
log (str(counter) + " way With "+str(issuecounter)+" Issue Fixed. ("+str(ar_numbers)+" Arabic Numbers - "+str(en_numbers)+" English Numbers)")
# counter = 0
# issuecounter = 0
# ar_numbers = 0
# en_numbers = 0
# log("Working on Relations:")
# for relation in root.findall('relation'):
# for tag in relation.findall('tag'):
# k = tag.attrib['k']
# if k=='name':
# name = tag.attrib['v']
# if any(char in name for char in ignore_list):
# log(" id:"+node.attrib['id']+" name:'"+name+"' matched in ignore_list , it didn't get touched")
# continue
# if any(char in name for char in accepted_chars):
# temp = tag.attrib['v']
# v_fixed=persian.convert_ar_numbers(name)
# if temp != v_fixed:
# ar_numbers = ar_numbers+1
# issuecounter = issuecounter+1
# temp = v_fixed
# v_fixed=persian.convert_en_numbers(v_fixed)
# if temp != v_fixed:
# en_numbers = en_numbers+1
# issuecounter = issuecounter+1
# temp = v_fixed
# if v_fixed != name:
# counter=counter+1
# tag.attrib['v'] = v_fixed
# relation.set('action', 'modify')
# else:
# log (" Warning: id:"+node.attrib['id']+" name:'"+name+"' did not matched in accepted_chars , it didn't get touched")
# log (str(counter) + " Relation With "+str(issuecounter)+" Issue Fixed. ("+str(ar_numbers)+" Arabic Numbers - "+str(en_numbers)+" English Numbers)")
log ("")
log ("* Writing to output file")
tree.write('output.osm',encoding="UTF-8")
log ("* Done.")
log("------------------------------------------------------------------------------------------")
#comment:
'''
[out:xml][timeout:180];
{{geocodeArea:iran}}->.searchArea;
(
node["name"!~"[a-z]+"]["name"!~"[A-Z]+"]["name"~"[0-9]|٦|٥|٤|ي|ك"](area.searchArea);
way["name"!~"[a-z]+"]["name"!~"[A-Z]+"]["name"~"[0-9]|٦|٥|٤|ي|ك"](area.searchArea);
);
(._;>;);
out meta;
'''