-
Notifications
You must be signed in to change notification settings - Fork 19
/
hooks.py
228 lines (203 loc) · 7.23 KB
/
hooks.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
#!/usr/bin/env python
#coding=utf-8
'''
Defines the hooks for the different type of named entities.
@author: Marco Damonte (m.damonte@sms.ed.ac.uk)
@since: 03-10-16
'''
import re
from node import Node
from collections import defaultdict
from resources import Resources
try:
from counter import Counter
except:
from collections import Counter
wikis = {}
for item in open("resources/countries.txt").read().splitlines():
wikis[item.split(",")[0].strip()] = item.split(",")[1].strip()
countries = wikis.keys()
nationalities = {}
for item in open("resources/nationalities.txt").read().splitlines():
c = ""
for i in item.split(" => ")[0].strip()[1:-1].split():
i = i[0].upper() + i[1:]
c += i + " "
c = c.strip()
nationalities[item.split(" => ")[1].strip()[1:-2].lower()] = c
for item in open("resources/nationalities2.txt").read().splitlines():
if item.split("\t")[0] not in nationalities:
nationalities[item.split("\t")[1].lower()] = item.split("\t")[0]
states = [item.strip() for item in open("resources/states.txt").read().splitlines()]
cities = [item.strip() for item in open("resources/cities.txt").read().splitlines()]
def names(name_type, cat, token, var, variables):
nodes = []
relations = []
v1 = variables.nextVar()
v2 = variables.nextVar()
ntop = Node(token, v1, cat, False)
nodes.append(ntop)
nname = Node(token, v2, "name", False)
nodes.append(nname)
relations.append((ntop, nname , ":name"))
if var in wikis and wikis[var] != "":
nwiki = Node(token, '"' + wikis[var] + '"', name_type, True)
if wikis[var] != var:
nodes.append(nwiki)
else:
nwiki = Node(token, '"' + var + '"', name_type, True)
relations.append((ntop, nwiki , ":wiki"))
for i, word in enumerate(var.split("_")):
nop = Node(token, '"' + word + '"', name_type, True)
nodes.append(nop)
relations.append((nname, nop , ":op" + str(i + 1)))
return (nodes, relations)
def isState(name):
name = name.strip()
name.replace("'","")
name.replace('"','')
if name in states:
return name
return None
def isCity(name):
name = name.strip()
name.replace("'","")
name.replace('"','')
if name in cities:
return name
return None
def isOrg(name):
name = name.strip()
name.replace("'","")
name.replace('"','')
if name in Resources.organizations:
return Resources.organizations[name]
return None
def isCountry(name):
name = name.strip()
name = name.replace("'","")
name = name.replace('"','')
if name.startswith("_"):
name = name[1:]
if name.endswith("_"):
name = name[:-1]
name = name.replace("__","_")
if name in countries:
return name
name = name.replace(".","")
for c in countries:
if "".join([d[0].lower() for d in c.split("_")]) == name.lower():
return c
for c in countries:
if name.lower() in [d.lower() for d in c.split("_")]:
return c
if name.startswith("the_") or name.startswith("The_"):
name = name[4:]
return isCountry(name)
return None
def stripzeros(num):
return num.lstrip("0")
def run(token, var, label, variables):
if label == "DATE" and re.match("^(\d{4}|XXXX)(-\d{2})?(-\d{2})?$",token.word):
nodes = []
relations = []
v1 = variables.nextVar()
ntop = Node(token, v1, "date-entity", False)
nodes.append(ntop)
if len(token.word.split("-")) == 3:
nday = Node(token, stripzeros(token.word.split("-")[2]), label, True)
nmonth = Node(token, stripzeros(token.word.split("-")[1]), label, True)
nodes.append(nday)
nodes.append(nmonth)
relations.append((ntop, nday , ":day"))
relations.append((ntop, nmonth , ":month"))
if token.word.split("-")[0] != "XXXX":
nyear = Node(token, token.word.split("-")[0], label, True)
nodes.append(nyear)
relations.append((ntop, nyear , ":year"))
elif len(token.word.split("-")) == 2:
nmonth = Node(token, stripzeros(token.word.split("-")[1]), label, True)
nodes.append(nmonth)
relations.append((ntop, nmonth , ":month"))
if token.word.split("-")[0] != "XXXX":
nyear = Node(token, token.word.split("-")[0], label, True)
nodes.append(nyear)
relations.append((ntop, nyear , ":year"))
elif len(token.word.split("-")) == 1:
nyear = Node(token, token.word.split("-")[0], label, True)
nodes.append(nyear)
relations.append((ntop, nyear , ":year"))
return (nodes,relations)
if label == "LOCATION":
state = isState(token.word)
if state is not None:
return names(label, "state", token, var, variables)
country = isCountry(token.word)
if country is not None:
return names(label, "country", token, var, variables)
city = isCity(token.word)
if city is not None:
return names(label, "city", token, var, variables)
return False
if var.lower() in nationalities:
return names(label, "country", token, nationalities[var.lower()], variables)
if label == "PERSON":
return names(label, "person", token, var, variables)
if label == "ORGANIZATION":
org_type = isOrg(token.word)
if org_type is not None:
return names(label, org_type, token, var, variables)
return False
if label == "ORDINAL":
num = var.split(".")[0]
if num == "":
num = "0"
nodes = []
relations = []
v1 = variables.nextVar()
n1 = Node(token, v1, "ordinal-entity", False)
n2 = Node(token, num, "ORDINAL", True)
nodes.append(n1)
nodes.append(n2)
relations.append((n1, n2, ":value"))
return (nodes, relations)
if label =="PERCENT":
num = var.split("_")[0]
if num == "":
num = "0"
nodes = []
relations = []
v1 = variables.nextVar()
n1 = Node(token, v1, "percentage-entity", False)
n2 = Node(token, num, label, True)
nodes.append(n1)
nodes.append(n2)
relations.append((n1, n2, ":value"))
return (nodes, relations)
if label == "NUMBER" and var.replace(".","").isdigit():
nodes = []
relations = []
n1 = Node(token, var, "NUMBER", True)
nodes.append(n1)
return (nodes, relations)
if label =="MONEY" and len(var.split("_")) >= 2:
num1 = var.split("_")[0]
if num1 == "" or num1 == "(" or num1 == ")":
num1 = "0"
num2 = var.split("_")[1]
if num2 == "":
num2 = "0"
nodes = []
relations = []
v1 = variables.nextVar()
v2 = variables.nextVar()
n1 = Node(token, v1, "monetary-quantity", False)
n3 = Node(token, v2, num2, False)
n2 = Node(token, num1, label, True)
nodes.append(n1)
nodes.append(n2)
nodes.append(n3)
relations.append((n1, n2, ":quant"))
relations.append((n1, n3, ":unit"))
return (nodes, relations)
return False