-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnormalizer.py
358 lines (310 loc) · 9.63 KB
/
normalizer.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
"""
Thierry Bertin-Mahieux (2011) Columbia University
This code contains functions to normalize an artist name,
and possibly a song title.
This is intended to do metadata matching.
It is mostly an elaborate hack, I never did an extensive search of
all problematic name matches.
Code developed using Python 2.6 on a Ubuntu machine, using UTF-8
This is part of the Million Song Dataset project from
LabROSA (Columbia University) and The Echo Nest.
Copyright 2011, Thierry Bertin-Mahieux
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 os
import re
import sys
import unicodedata
import itertools
import Levenshtein # http://pypi.python.org/pypi/python-Levenshtein/
# ROTATION SYMBOLS (A and B => B and A)
rotation_symbols = ['\|', '/', '&', ',', '\+', ';', '_']#, '\-']
rotation_words = ['and', 'y', 'et', 'vs', 'vs.', 'v', 'with', 'feat',
'feat.', 'featuring', 'presents', 'ft.', 'pres.']
# SYMBOLS TO REMOVE AT THE BEGINNING
stub_to_remove = ['dj', 'dj.', 'mc', 'm.c.', 'mc.', 'the', 'los', 'les']
# SYMBOLS TO REMOVE AT THE END
end_to_remove1 = ['big band', 'trio', 'quartet', 'ensemble', 'orchestra']
end_to_remove2 = ['band']
# COMPILED REGULAR EXPRESSION
# white spaces
re_space = re.compile(r'\s')
# non alphanumeric
re_nonalphanum = re.compile(r'\W')
# rotation symbols
re_rotsymbols = re.compile('\s*?' + '|'.join(rotation_symbols) + '\s*?')
# rotation words
re_rotwords = re.compile(r'\s(' + '|'.join(rotation_words) + ')\s')
# stub to remove
re_remstub = re.compile('(' + '|'.join(stub_to_remove) + ')\s(.*)')
# ending to remove
re_remending1 = re.compile('(.*)\s(' + '|'.join(end_to_remove1) + ')')
re_remending2 = re.compile('(.*)\s(' + '|'.join(end_to_remove2) + ')')
# quotes to remove
re_remquotes = re.compile('(.+)\s(".+?")\s(.+)')
# parenthesis to remove
re_remparenthesis = re.compile('(.+)\s(\(.+?\))\s*(.*)')
# brackets to remove
re_rembrackets = re.compile('(.+)\s(\[.+?\])\s*(.*)')
def char_is_ascii(c):
"""
Check if a unicode character, e.g. u'A', u'1' or u'\u0301' is ASCII
"""
#return ord(c) < 128
# the following should be faster, according to:
#http://stackoverflow.com/questions/196345/how-to-check-if-a-string-in-python-is-in-ascii
return c < u"\x7F"
def remove_non_ascii(s):
"""
Normalize characters in unicode string 's' that are not ASCII,
try to transform accented characters to non accented version.
Otherwise, remove non-ascii chars
"""
decomposition = unicodedata.normalize('NFKD', s)
return filter(lambda x: char_is_ascii(x), decomposition)
def to_lower_case(s):
"""
transform a unicode string 's' to lowercase
ok, this one is trivial, I know
"""
return s.lower()
def remove_spaces(s):
"""
Remove all possible spaces in the unicode string s
"""
return re_space.sub('', s)
def replace_rotation_symbols(s):
"""
Mostly, replace '&' by 'and'
"""
return re_rotsymbols.sub(' and ', s)
def remove_stub(s):
"""
Remove a questionable beginning, e.g. dj
otherwise return string at is
"""
m = re_remstub.match(s)
if not m:
return s
return m.groups()[1]
def remove_endings(s):
"""
Remove questionable endings, e.g. 'band'
"""
m = re_remending1.match(s)
if m:
s = m.groups()[0]
m = re_remending2.match(s)
if m:
s = m.groups()[0]
return s
def remove_quotes(s):
"""
Remove the quote, like Thierry "The Awesomest" BM
"""
m = re_remquotes.match(s)
if not m:
return s
parts = m.groups()
assert len(parts) == 3
return parts[0] + ' ' + parts[2]
def remove_parenthesis(s):
"""
Remove parenthesis, like Thierry (Coolest guy)
"""
m = re_remparenthesis.match(s)
if not m:
return s
parts = m.groups()
assert len(parts) >= 2
if len(parts) == 2:
return parts[0]
return parts[0] + ' ' + parts[2]
def remove_brackets(s):
"""
Remove brackets, like Thierry [Coolest guy]
"""
m = re_rembrackets.match(s)
if not m:
return s
parts = m.groups()
assert len(parts) >= 2
if len(parts) == 2:
return parts[0]
return parts[0] + ' ' + parts[2]
def normalize_no_rotation(s):
"""
We normalize a name that is supposed to contain no
rotation term ('and', 'y', ...)
"""
# remove beginning
s = remove_stub(s)
# remove ends
s = remove_endings(s)
# remove ()
s = remove_parenthesis(s)
# remove ""
s = remove_quotes(s)
return s
def split_rotation_words(s):
"""
Split a name using the rotation words: 'and', 'vs', 'y', 'et', ...
then create all possible permutations
"""
parts = re_rotwords.split(s)
parts = filter(lambda p: not p in rotation_words, parts)[:5]
results = set()
# keep only the individual elems (risky?)
for p in parts:
results.add(p)
# create all permutations
permutations = itertools.permutations(parts)
#maxperm = 30
#count_perm = 0
for perm in permutations:
#count_perm += 1
#if count_perm > maxperm:
# break
results.add(' '.join(perm))
# redo the same but remove the stub first for all parts
parts = map(lambda p: normalize_no_rotation(p), parts)
for p in parts:
results.add(p)
permutations = itertools.permutations(parts)
for perm in permutations:
results.add(' '.join(perm))
# done
return results
def remove_nonalphanumeric(s):
"""
Remove usual punctuation signs: ! , ? : ; . ' etc
Also, we transform long spaces into normal ones
"""
# split around non-alphanum chars
parts = re_nonalphanum.split(s)
# remove empty spots
parts = filter(lambda p: p, parts)
# rejoin with regular space ' '
return ' '.join(parts)
def normalize_artist(s):
"""
Return a set of normalized versions of that artist name
"""
# normalized versions
results = set()
# lower case
s = to_lower_case(s)
results.add(s)
# remove non-ascii chars (try to replace them)
s = remove_non_ascii(s)
results.add(s)
# try removing parenthesis before, in case there's an & in it
s2 = remove_parenthesis(s)
results.add(s2)
# replace rotation symbols
s = replace_rotation_symbols(s)
# split and permute according to rotation words
permutations = split_rotation_words(s)
results.update(permutations)
# remove non-alphanumeric and normalize spaces
results = map(lambda s: remove_nonalphanumeric(s), results)
# remove all spaces
results = map(lambda s: remove_spaces(s), results)
# done (and remove dupes)
return set(results)
def normalize_title(s):
"""
Return a set of normalized versions of that title
"""
# normalized versions
results = set()
# lower case
s = to_lower_case(s)
results.add(s)
# remove non-ascii chars (try to replace them)
s = remove_non_ascii(s)
results.add(s)
# try removing parenthesis
s = remove_parenthesis(s)
results.add(s)
# try removing brackets
s = remove_brackets(s)
results.add(s)
# remove non-alphanumeric and normalize spaces
results = map(lambda s: remove_nonalphanumeric(s), results)
# remove all spaces
results = map(lambda s: remove_spaces(s), results)
# done (and remove dupes)
return set(results)
def same_artist(name1, name2):
"""
Compare two artists:
- edit distance
- if one name is contained in the other
- by normalizing the names
Return True if it's the same artist, False otherwise
"""
# trivial
n1 = to_lower_case(name1)
n2 = to_lower_case(name2)
if n1 == n2:
return True
# edit distance
if len(n1) >= 10 or len(n2) >= 10:
if Levenshtein.distance(n1, n2) <= 2:
return True
# n1 contains n2? or the other way around
if len(n1) >= 10 and len(n2) >= 10:
if len(n1) > len(n2):
if n1.find(n2) >= 0:
return True
else:
if n2.find(n1) >= 0:
return True
# compare by normalizing names
normalized1 = normalize_artist(n1)
normalized2 = normalize_artist(n2)
if len(normalized1.intersection(normalized2)) > 0:
return True
return False
def same_title(title1, title2):
"""
Compare two titles:
- edit distance
- if one name is contained in the other
- by normalizing the title
Return True if it's the same title, False otherwise
"""
# trivial
t1 = to_lower_case(title1)
t2 = to_lower_case(title2)
if t1 == t2:
return True
# edit distance
if len(t1) >= 10 or len(t2) >= 10:
if Levenshtein.distance(t1, t2) <= 2:
return True
# n1 contains n2? or the other way around
if len(t1) >= 10 and len(t2) >= 10:
if len(t1) > len(t2):
if t1.find(t2) >= 0:
return True
else:
if t2.find(t1) >= 0:
return True
# compare by normalizing names
normalized1 = normalize_title(t1)
normalized2 = normalize_title(t2)
if len(normalized1.intersection(normalized2)) > 0:
return True
return False