-
Notifications
You must be signed in to change notification settings - Fork 0
/
nicknames.py
45 lines (41 loc) · 1.16 KB
/
nicknames.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
###
##
#
# carltonnorthern/nickname-and-diminutive-names-lookup is licensed under the
#
# https://github.com/carltonnorthern/nickname-and-diminutive-names-lookup
#
# Apache License 2.0
# http://www.apache.org/licenses/
#
##
###
import logging
import collections
import csv
class NameDenormalizer(object):
def __init__(self, filename=None):
filename = filename or 'static_data/names.csv'
lookup = collections.defaultdict(list)
with open(filename) as f:
reader = csv.reader(f)
for line in reader:
matches = set(line)
for match in matches:
lookup[match].append(matches)
self.lookup = lookup
def __getitem__(self, name):
name = name.lower()
if name not in self.lookup:
raise KeyError(name)
names = set().union(*self.lookup[name])
logging.debug(f'getitem({name}): {names}')
if name in names:
names.remove(name)
return names
def get(self, name):
try:
logging.debug(f'getnick: self[{name}] = self[name]')
return self[name]
except KeyError:
return []