forked from osm-fr/osmose-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTagFix_DuplicateValue.py
211 lines (189 loc) · 9.48 KB
/
TagFix_DuplicateValue.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
#-*- coding: utf-8 -*-
###########################################################################
## ##
## Copyrights Etienne Chové <[email protected]> 2009 ##
## Copyrights Frédéric Rodrigo 2011-2015 ##
## ##
## 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/>. ##
## ##
###########################################################################
import re
import itertools
from modules.Stablehash import stablehash64
from modules.OsmoseTranslation import T_
from plugins.Plugin import Plugin
class TagFix_DuplicateValue(Plugin):
def init(self, logger):
Plugin.init(self, logger)
doc = dict(
detail = T_(
'''The tag contains two values (separated by ';') which are very
similar.'''),
fix = T_(
'''Delete one value.'''),
trap = T_(
'''In some cases all values may be required.
Ensure the interpretation of the tag does not change when you delete one item.'''))
self.errors[3060] = self.def_class(item = 3060, level = 3, tags = ['value', 'fix:chair'],
title = T_('Duplicated values'),
**doc)
self.errors[30601] = self.def_class(item = 3060, level = 3, tags = ['value', 'fix:chair'],
title = T_('Similar values'),
resource = 'https://en.wikipedia.org/wiki/Levenshtein_distance',
**doc)
self.WhitelistSimilarEqual = set(( # Keys that can have similar and even equal values
'CLC:id', 'GNS:id', 'tmc', 'tiger:cfcc', 'statscan:rbuid', 'nysgissam:nysaddresspointid',
'ref',
'source:date',
'source:geometry:date', # Belgium, Flanders
'technology',
'cables',
'position',
'couplings:diameters',
'voltage',
'addr:flats', 'addr:housenumber', 'addr:unit', 'addr:floor', 'addr:block', 'addr:door',
'note', 'description', 'inscription', # Text; not ;-separated values
))
self.WhitelistSimilar = set(( # Keys that can have similar, but not equal values
'source:geometry:ref', # Belgium, Flanders
'gnis:id', 'gnis:feature_id', # USA
'network:guid',
'created_by',
'is_in',
'service_times', 'collection_times',
'phone', 'fax', 'emergency:phone',
'email',
'url', 'website',
'destination',
'passenger',
'healthcare:speciality',
'traffic_sign', 'traffic_sign:forward', 'traffic_sign:backward',
'sport',
'communication:mobile_phone',
'cuisine',
'operator',
'delivery',
'changing_table:location',
'furniture',
'seamark:berth:category',
'seamark:restricted_area:restriction',
'historic:period',
'charge',
'social_facility:for',
))
self.WhitelistSimilarEqualRegex = set(( # Regexes for keys that can have similar and even equal values
re.compile('seamark:.+:colour'),
re.compile('.+_ref'), re.compile('ref:.+'),
re.compile('destination:.+'),
re.compile('AND_.+'), re.compile('AND:.+'), # Netherlands
re.compile('[Nn][Hh][Dd]:.+'),
re.compile('massgis:.+'),
re.compile('lacounty:.+'),
re.compile('turn:lanes.*'),
re.compile('.+:conditional'), # More thorough check in ConditionalRestrictions plugin
))
self.WhitelistSimilarRegex = set(( # Regexes for keys that can have similar, but not equal values
re.compile('.+_name'),
re.compile('.+:date'),
re.compile('.+:colour'),
re.compile('opening_hours(:.+)?'),
re.compile('(.+:)?wikidata'),
re.compile('railway:signal:.+'),
re.compile('contact:.+'),
))
# http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#Python
def levenshtein(self, s1, s2):
if len(s1) < len(s2): # pragma: no cover
return self.levenshtein(s2, s1)
if not s1: # pragma: no cover
return len(s2)
previous_row = range(len(s2) + 1)
for i, c1 in enumerate(s1):
current_row = [i + 1]
for j, c2 in enumerate(s2):
insertions = previous_row[j + 1] + 1 # j+1 instead of j since previous_row and current_row are one character longer
deletions = current_row[j] + 1 # than s2
substitutions = previous_row[j] + (c1 != c2)
current_row.append(min(insertions, deletions, substitutions))
previous_row = current_row
return previous_row[-1]
def anyRegexMatch(self, set_of_regexes, key):
for r in set_of_regexes:
if r.match(key):
return True
return False
def node(self, data, tags):
err = []
keys = tags.keys()
for k in keys:
if k in self.WhitelistSimilarEqual:
continue # Key may have equal or similar values
v = tags[k]
if not ';' in v:
continue
if self.anyRegexMatch(self.WhitelistSimilarEqualRegex, k):
continue # Key may have equal or similar values
if k == 'source':
v = v.replace('Cadastre ; mise', 'Cadastre, mise') # France
v = v.replace('GSImaps/ort', 'GSImaps/std') # Japan
vs = list(filter(lambda w: len(w) > 0, map(lambda w: w.strip(), v.split(';'))))
if len(vs) != len(set(vs)):
err.append({"class": 3060, "subclass": stablehash64(k),
"text": T_("Duplicated values {key}={val}", key = k, val = tags[k]),
"fix": {k: ";".join(set(vs))} })
else:
if k in self.WhitelistSimilar:
continue # Key may have similar values
if self.anyRegexMatch(self.WhitelistSimilarRegex, k):
continue # Key may have similar values
vs_long = filter(lambda w: len(w) > 6, vs)
for v1,v2 in itertools.combinations(vs_long, 2):
if abs(len(v1)-len(v2)) < 4 and self.levenshtein(v1, v2) < 4:
err.append({"class": 30601, "subclass": stablehash64(k),
"text": T_("Similar values {v1} and {v2} in {key}={val}", v1 = v1, v2 = v2, key = k, val = tags[k])})
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 = TagFix_DuplicateValue(None)
a.init(None)
for t in [{"oneway":"yes;yes"},
{"oneway":"yes;yes;no"},
{"oneway":"yes;yes;yes;yes;-1;-1;no;no"},
{"passenger":"national;national"},
{"opening_hours": "Mo 14:00-19:00; Tu-Fr 10:00-14:00,15:00-19:00; Mo 14:00-19:00"},
{"source":u"cadastre-dgi-fr source : Direction Générale des Impôts - Cadastre ; mise à jour : 2013;cadastre-dgi-fr source : Direction Générale des Impôts - Cadastre ; mise à jour : 2013"},
{"source":u"cadastre-dgi-fr source : Direction Générale des Impôts - Cadastre ; mise à jour : 2010;cadastre-dgi-fr source : Direction Générale des Impôts - Cadastre ; mise à jour : 2013"},
{"source":"GSImaps/ort;GSImaps/std"},
]:
self.check_err(a.node(None, t), t)
self.check_err(a.way(None, t, None), t)
self.check_err(a.relation(None, t, None), t)
for t in [{"ref":"E 05; E 70; E 05;E 70; E 05;E 70; E 05;E 70; E 05;E 70"},
{"seamark:buoy_lateral:colour":"red;white;red;white"},
{"ref:mhs":"IA00070520; IA00070492"},
{"opening_hours": "Mo 14:00-19:00; Tu-Fr 10:00-14:00,15:00-19:00; Sa 10:00-19:00"},
{"oneway":"yes;no"},
{"passenger":"national;international;regional"},
{"AND_toto":"121;121;121"},
{"NHD:ComID":"141725410;141725411"},
{"massgis:OS_ID":"305-735;305-764"},
]:
assert not a.node(None, t), t