This repository has been archived by the owner on Jun 3, 2019. It is now read-only.
forked from xyzsam/isca2017
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstitutions.py
81 lines (66 loc) · 2.01 KB
/
institutions.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
# Reads the institution name database.
from bisect import bisect_left
import re
from fuzzywuzzy import fuzz
import base
class Institution(base.BaseObj):
_id = 0
def __init__(self, aliases):
super(Institution, self).__init__()
self.aliases = []
for alias in aliases:
self.aliases.append(alias.strip())
self.name = self.aliases[0]
self.id = Institution._id
Institution._id += 1
def __eq__(self, other):
if isinstance(other, Institution):
return other.id == self.id
elif isinstance(other, int):
return other == self.id
return False
def __unicode__(self):
return u"{0}".format(self.name)
def __str__(self):
return unicode(self).encode("utf-8")
def __repr__(self):
return str(self)
class InstDB(base.BaseDB):
def __init__(self, inst_list):
super(InstDB, self).__init__(inst_list)
# Add all of the aliases for each institution.
for inst in inst_list:
for name in inst.aliases:
self.pairs_.append((name, inst.id))
self.names_.append(name)
self.pairs_.sort(key=lambda tup: tup[0])
self.names_.sort()
def find_exact_or_closest(self, instname, scorer=fuzz.token_set_ratio):
""" Find either an exact match or a very close match. """
if len(instname) == 0:
return -1
# Try to find an exact match.
instid = self.getid(instname)
if instid != -1:
return instid
# Find closest match to this institution.
match = self.find_closest(instname, scorer=scorer)
if not match:
return -1
score = match["score"]
if score >= 90:
return match["id"]
return -1
def read_instdb(fname):
institutions = []
with open(fname, "rb") as f:
for line in f:
line = unicode(line, encoding="utf-8")
split = line.split(",")
# Remove "University" and "College"
processed = []
for s in split:
s = re.sub("UNIVERSITY|COLLEGE", "", s.upper())
processed.append(s)
institutions.append(Institution(processed))
return InstDB(institutions)