forked from osm-fr/osmose-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Name_Script.py
344 lines (291 loc) · 16.2 KB
/
Name_Script.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
#-*- coding: utf-8 -*-
###########################################################################
## ##
## Copyrights Frédéric Rodrigo 2016 ##
## ##
## This program is free software: you can redistribute it and/or modify ##
## it under the terms of the GNU General Public License as published by ##
## the Free Software Foundation, either version 3 of the License, or ##
## (at your option) any later version. ##
## ##
## This program is distributed in the hope that it will be useful, ##
## but WITHOUT ANY WARRANTY; without even the implied warranty of ##
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ##
## GNU General Public License for more details. ##
## ##
## You should have received a copy of the GNU General Public License ##
## along with this program. If not, see <http://www.gnu.org/licenses/>. ##
## ##
###########################################################################
from plugins.Plugin import Plugin
import regex
import unicodedata
from modules.languages import language2scripts, gen_regex
from modules import confusables
from modules.py3 import ilen
from modules.Stablehash import stablehash64
class Name_Script(Plugin):
def init(self, logger):
Plugin.init(self, logger)
self.errors[50701] = self.def_class(item = 5070, level = 2, tags = ['name', 'fix:chair'],
title = T_('Some value chars does not match the language charset'),
detail = T_(
'''Words are not written in the appropriate alphabet of the
language.'''),
fix = T_(
'''Usually, a wrong language has been chosen. Sometimes the word has been
transliterated, and needs to be changed back to the original alphabet.
`name:ar=Salaam` should be either `name:en=Salaam` (if known by
untranslated name) or `name:en=Peace` (translated) or `name:ar=سلام`
(original).'''))
self.errors[50702] = self.def_class(item = 5070, level = 2, tags = ['name', 'fix:chair'],
title = T_('Non printable char'),
detail = T_(
'''A non-printable character such as linefeed (0x000a) has been
used.'''),
fix = T_(
'''Remove the character.'''))
self.errors[50703] = self.def_class(item = 5070, level = 2, tags = ['name', 'fix:chair'],
title = T_('Unexpected symbol in name'),
detail = T_(
'''A symbol is used instead of a letter from the appropriate
alphabet.'''),
fix = T_(
'''Change the character into a punctuation mark or something else more
appropriate.'''),
resource = 'http://unicode.org/cldr/utility/list-unicodeset.jsp?a=[:General_Category=Other_Symbol:]')
country = self.father.config.options.get("country")
self.non_printable = regex.compile(u"[\p{Line_Separator}\p{Paragraph_Separator}\p{Control}\p{Private_Use}\p{Surrogate}\p{Unassigned}]", flags=regex.V1)
# http://unicode.org/cldr/utility/list-unicodeset.jsp?a=[:General_Category=Other_Symbol:]
self.other_symbol = regex.compile(u"[[\p{General_Category=Other_Symbol}]--[\p{Block=Latin 1 Supplement}\p{Block=Braille Patterns}\p{Block=CJK Radicals Supplement}\p{Block=Kangxi Radicals}\p{Block=CJK Strokes}]--[↔→◄►№]]", flags=regex.V1)
self.non_letter = regex.compile(u"[^\p{Letter}\p{Mark}\p{Separator}]", flags=regex.V1)
non_look_like_latin = u"\p{Hangul}\p{Bengali}\p{Bopomofo}\p{Braille}\p{Canadian_Aboriginal}\p{Devanagari}\p{Ethiopic}\p{Gujarati}\p{Gurmukhi}\p{Han}\p{Hangul}\p{Hanunoo}\p{Hebrew}\p{Hiragana}\p{Inherited}\p{Kannada}\p{Katakana}\p{Khmer}\p{Lao}\p{Malayalam}\p{Oriya}\p{Runic}\p{Sinhala}\p{Syriac}\p{TaiLe}\p{Tamil}\p{Thaana}\p{Thai}\p{Tibetan}"
ammend = ""
if country and country.startswith("BG"):
ammend = "|TT" # Bulgarian survey point
self.alone_char = regex.compile(u"(^| |[%s])(?:[A-Z]%s)(?= |[%s]|$)" % (non_look_like_latin, ammend, non_look_like_latin), flags=regex.V1)
self.roman_number = regex.compile(u"(^| )(?:[IVXLDCM]+)(?= |$)", flags=regex.V1)
self.scripts = language2scripts
self.uniq_scripts = {}
for k, s in self.scripts.items():
if s and ilen(filter(lambda ss: ss[0] != "[", s)) == 1:
self.uniq_scripts[k] = s[0]
else:
self.uniq_scripts[k] = None
self.lang = {}
for (k, s) in self.scripts.items():
self.lang[k] = gen_regex(s)
self.default = None
languages = self.father.config.options.get("language")
if languages:
if not isinstance(languages, list):
languages = [languages]
# Assert the languages are mapped to scripts
for language in languages:
if language not in self.lang:
raise Exception("No script setup for language '%s'" % language)
# Disable default scripts if one language is not mapped to scripts
for language in languages:
if not self.lang[language]:
languages = None
# Build default regex
if languages:
self.default = regex.compile(r"[\p{Common}%s]" % "".join(map(lambda l: self.lang[l], languages)), flags=regex.V1)
self.uniq_script = self.uniq_scripts.get(languages[0]) if languages and len(languages) == 1 else None
for l, s in list(self.lang.items()):
if s is None:
del(self.lang[l])
else:
self.lang[l] = regex.compile(r"[\p{Common}%s]" % s, flags=regex.V1)
self.names = [u"name", u"name_1", u"name_2", u"alt_name", u"loc_name", u"old_name", u"official_name", u"short_name"]
def node(self, data, tags):
err = []
for key, value in tags.items():
m = self.non_printable.search(key)
if m:
err.append({"class": 50702, "subclass": 0 + stablehash64(key), "text": T_f(u"\"{0}\" unexpected non printable char ({1}, 0x{2:04x}) in key at position {3}", key, unicodedata.name(m.group(0), ''), ord(m.group(0)), m.start() + 1)})
break
m = self.non_printable.search(value)
if m:
err.append({"class": 50702, "subclass": 1 + stablehash64(key), "text": T_f(u"\"{0}\"=\"{1}\" unexpected non printable char ({2}, 0x{3:04x}) in value at position {4}", key, value, unicodedata.name(m.group(0), ''), ord(m.group(0)), m.start() + 1)})
break
m = self.other_symbol.search(key)
if m:
err.append({"class": 50703, "subclass": 0 + stablehash64(key), "text": T_f(u"\"{0}\" unexpected symbol char ({1}, 0x{2:04x}) in key at position {3}", key, unicodedata.name(m.group(0), ''), ord(m.group(0)), m.start() + 1)})
break
m = self.other_symbol.search(value)
if m:
err.append({"class": 50703, "subclass": 1 + stablehash64(key), "text": T_f(u"\"{0}\"=\"{1}\" unexpected symbol char ({2}, 0x{3:04x}) in value at position {4}", key, value, unicodedata.name(m.group(0), ''), ord(m.group(0)), m.start() + 1)})
break
err_size = len(err)
# https://en.wikipedia.org/wiki/Bi-directional_text#Table_of_possible_BiDi-types
for c in u"\u200E\u200F\u061C\u202A\u202D\u202B\u202E\u202C\u2066\u2067\u2068\u2069":
m = key.find(c)
if m > 0:
err.append({"class": 50702, "subclass": 2 + stablehash64(key), "text": T_f(u"\"{0}\" unexpected non printable char ({1}, 0x{2:04x}) in key at position {3}", key, unicodedata.name(c, ''), ord(c), m + 1)})
break
m = value.find(c)
if m > 0:
err.append({"class": 50702, "subclass": 3 + stablehash64(key), "text": T_f(u"\"{0}\"=\"{1}\" unexpected non printable char ({2}, 0x{3:04x}) in value at position {4}", key, value, unicodedata.name(c, ''), ord(c), m + 1)})
break
if err_size != len(err):
break
if self.default:
if key in self.names:
s = self.non_letter.sub(u" ", value)
s = self.alone_char.sub(u"", s)
s = self.roman_number.sub(u"", s)
s = self.default.sub(u"", s)
if len(s) > 0 and not(len(value) == 2 and len(s) == 1) and len(s) <= len(value) / 10 + 1:
if len(s) == 1:
c = s[0]
u = self.uniq_script and confusables.unconfuse(c, self.uniq_script)
if u:
err.append({"class": 50701, "subclass": 0 + stablehash64(key),
"text": T_f(u"\"{0}\"=\"{1}\" unexpected char \"{2}\" ({3}, 0x{4:04x}). Means \"{5}\" ({6}, 0x{7:04x})?", key, value, s, unicodedata.name(c, ''), ord(c), u, unicodedata.name(u, ''), ord(u)),
"fix": {key: value.replace(c, u)}
})
else:
err.append({"class": 50701, "subclass": 0 + stablehash64(key),
"text": T_f(u"\"{0}\"=\"{1}\" unexpected char \"{2}\" ({3}, 0x{4:04x})", key, value, s, unicodedata.name(c, ''), ord(c))
})
else:
err.append({"class": 50701, "subclass": 0 + stablehash64(key), "text": T_f(u"\"{0}\"=\"{1}\" unexpected \"{2}\"", key, value, s)})
break
l = key.split(':')
if len(l) > 1 and l[0] in self.names and l[1] in self.lang:
s = self.non_letter.sub(u" ", value)
s = self.alone_char.sub(u"\\1", s)
s = self.roman_number.sub(u"\\1", s)
s = self.lang[l[1]].sub(u"", s)
if len(s) > 0:
if len(s) == 1:
c = s[0]
u = self.uniq_scripts.get(l[1]) and confusables.unconfuse(c, self.uniq_scripts.get(l[1]))
if u:
err.append({"class": 50701, "subclass": 1 + stablehash64(key),
"text": T_f(u"\"{0}\"=\"{1}\" unexpected char \"{2}\" ({3}, 0x{4:04x}). Means \"{5}\" ({6}, 0x{7:04x})?", key, value, s, unicodedata.name(c, ''), ord(c), u, unicodedata.name(u, ''), ord(u)),
"fix": {key: value.replace(c, u)}
})
else:
err.append({"class": 50701, "subclass": 1 + stablehash64(key),
"text": T_f(u"\"{0}\"=\"{1}\" unexpected char \"{2}\" ({3}, 0x{4:04x})", key, value, s, unicodedata.name(c, ''), ord(c))
})
else:
err.append({"class": 50701, "subclass": 1 + stablehash64(key), "text": T_f(u"\"{0}\"=\"{1}\" unexpected \"{2}\"", key, value, s)})
break
return err
def way(self, data, tags, nds):
return self.node(data, tags)
def relation(self, data, tags, members):
return self.node(data, tags)
###########################################################################
from plugins.Plugin import TestPluginCommon
class Test(TestPluginCommon):
def test_(self):
a = Name_Script(None)
class _config:
options = {"country": "FR"}
class father:
config = _config()
a.father = father()
a.init(None)
assert not a.node(None, {u"name": u"test ь"})
assert not a.node(None, {u"name": u"Sacré-Cœur"})
self.check_err(a.node(None, {u"name:uk": u"Sacré-Cœur"}))
def test_fr(self):
a = Name_Script(None)
class _config:
options = {"language": "fr"}
class father:
config = _config()
a.father = father()
a.init(None)
assert not a.node(None, {u"seamark:light:information": u"R. 340° -095° , W.-111° , G.-160°"})
assert not a.node(None, {u"source": u"©IGN 2010"})
assert not a.node(None, {u"name": u"Karditsa → Larisa"})
assert not a.node(None, {u"name": u"To Embonas ►"})
self.check_err(a.node(None, {u"name": u"test ь"}))
self.check_err(a.node(None, {u"name": u"\u1F1EE\u1F1F6\u1F3E0"}))
assert not a.node(None, {u"name": u"test кодувань"})
assert not a.node(None, {u"name": u"кодувань"})
assert not a.node(None, {u"name": u"Sophie II"})
assert not a.node(None, {u"name": u"Sacré-Cœur"})
assert not a.node(None, {u"name": u"дA"})
assert not a.node(None, {u"name:uk": u"кодувань"})
assert not a.node(None, {u"name:tg": u"Париж"})
self.check_err(a.node(None, {u"name:uk": u"Sacré-Cœur"}))
assert not a.node(None, {u"name:uk": u"кодувань A"})
assert not a.node(None, {u"name:uk": u"кодувань A33"})
assert not a.node(None, {u"name:uk": u"B2"})
assert not a.node(None, {u"name:el": u"Διαδρομος 15R/33L"})
self.check_err(a.node(None, {u"name:el": u"ροMμος"}))
assert not a.node(None, {u"name:fa": u"شیب دِراز"})
assert not a.node(None, {u"name:th": u"P T L"})
self.check_err(a.node(None, {u"name:ru": u"Кари́бские Нидерла́нды"}))
assert not a.node(None, {u"name:ar": u"مسكّن عدي"})
assert not a.node(None, {u"name:ko": u"유스페이스2 B동"})
self.check_err(a.node(None, {u"name:el": u"Aιαδρομος"})) # A (Latin) to Α (Greek)
def test_fr_nl(self):
a = Name_Script(None)
class _config:
options = {"language": ["fr", "nl"]}
class father:
config = _config()
a.father = father()
a.init(None)
self.check_err(a.node(None, {u"name": u"test ь"}))
assert not a.node(None, {u"name": u"test кодувань"})
assert not a.node(None, {u"name:uk": u"кодувань"})
self.check_err(a.node(None, {u"name:uk": u"Sacré-Cœur"}))
def test_zh(self):
a = Name_Script(None)
class _config:
options = {"language": "zh"}
class father:
config = _config()
a.father = father()
a.init(None)
assert not a.node(None, {u"name": u"test ь"})
assert not a.node(None, {u"name": u"test кодувань"})
assert not a.node(None, {u"name:uk": u"кодувань"})
self.check_err(a.node(None, {u"name:uk": u"Sacré-Cœur"}))
def test_uk(self):
a = Name_Script(None)
class _config:
options = {"language": "uk"}
class father:
config = _config()
a.father = father()
a.init(None)
assert not a.node(None, {u"name": u"Бу́рти"})
assert not a.node(None, {u"name": u"Шкарпи́"})
def test_BG(self):
a = Name_Script(None)
class _config:
options = {"language": "bg", "country": "BG"}
class father:
config = _config()
a.father = father()
a.init(None)
assert not a.node(None, {u"name": u"TT190/XXVI/"}) # Bulgarian survey point
def test_non_printable(self):
a = Name_Script(None)
class _config:
options = {}
class father:
config = _config()
a.father = father()
a.init(None)
self.check_err(a.node(None, {u"name\u0001": u"test"}))
self.check_err(a.node(None, {u"name": u"test \u0000"}))
self.check_err(a.node(None, {u"name": u"test \u202B"}))
def test_non_my(self):
a = Name_Script(None)
class _config:
options = {"language": "my", "country": "ZZ"}
class father:
config = _config()
a.father = father()
a.init(None)
assert not a.node(None, {u"name:my": u"кодувань"})