forked from carltonnorthern/nicknames
-
Notifications
You must be signed in to change notification settings - Fork 0
/
python-parser.py
31 lines (28 loc) · 889 Bytes
/
python-parser.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
import collections
import csv
import operator
import functools
class NameDenormalizer(object):
def __init__(self, filename=None):
filename = filename or '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 = functools.reduce(operator.or_, self.lookup[name])
if name in names:
names.remove(name)
return names
def get(self, name, default=None):
try:
return self[name]
except KeyError:
return default