/
play.py
291 lines (271 loc) · 8.99 KB
/
play.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
import urllib
import logging
import argparse
import random
from xml.etree.ElementTree import ElementTree,fromstring
from lxml import etree
try:
from clint.textui import puts, colored
from clint.textui import columns
except ImportError:
print "clint library not found: try sudo easy_install clint"
from nltk.corpus import stopwords # NLTK is needed for the English stopword list
try:
from gensim import corpora, models, similarities # gensim provides an handy implementation of, among other things, LSI and topic models
except ImportError:
print 'gensim library not found: try sudo easy_install gensim\n'
gensim = None
raise ImportError
"""
author: Matteo Romanello, <matteo.romanello@kcl.ac.uk>
"""
global input_file,logger
input_file = "zbios.txt"
logger = None
def do_lookup(seed,query_limit = 5):
"""
Do a DBPedia lookup via its API.
"""
results = []
lookup_url = "http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryString=%s&MaxHits=%i"%(seed,query_limit)
return get(lookup_url)
def format_perseus_uri(i_string):
"""
Given an input ID, creates a URI compliant with the scheme defined by Perseus
"""
prefix = "http://data.perseus.org/people/smith:"
return "%s%s"%(prefix,i_string)
def get(uri):
"""
Retrieves the URI and returns its content as a string.
Args:
uri: the URI to retrieve
"""
logger.info("fetching resource <%s>"%uri)
handle = urllib.urlopen(uri)
result = ''
while (1):
next = handle.read()
if not next:
break
result += next
handle.close()
return result
def transform_tei(tei_input):
"""
Get information from the TEI by applying an XSLT transformation.
Parse the TEI/XML and keep just the information that is relevant in this context
(= for the automatic alignment of Perseus' URIs with DBPedia's).
"""
dumb_xml = ""
xslt_root = etree.parse("transform.xsl")
transform = etree.XSLT(xslt_root)
doc = etree.XML(tei_input)
dumb_xml = transform(doc)
return dumb_xml
def parse_lookup_reply(xml):
"""
Parse the response of the DBpedia lookup service.
Args:
xml: the xml string containing the lookup result
Returns:
a dictionary, of the kind d = {"label":"...","uri":"...",desc":...","label":"..."}
"""
results = []
result = {"label":None,"uri":None,"desc":None,}
doc = etree.XML(xml)
res = list(doc.findall(".//{%s}Result"%"http://lookup.dbpedia.org/"))
logger.info("Found %i results via DBPedia lookup"%len(res))
for r in res:
result["label"] = r.find("{%s}Label"%"http://lookup.dbpedia.org/").text
result["uri"] = r.find("{%s}URI"%"http://lookup.dbpedia.org/").text
result["desc"] = r.find("{%s}Description"%"http://lookup.dbpedia.org/").text
results.append(result)
result = {"label":None,"uri":None,"desc":None,}
return results
def parse_xml(etree_input):
"""
Args:
etree_input: is the output of the function transform_tei(...)
Returns:
A dictionary of the kind res = {"names":[],"desc":"..."}
"""
res = {"names":[],"desc":None}
names = etree_input.findall(".//name")
desc = etree_input.findall(".//desc")
el = names + desc
for i,item in enumerate(list(el)):
if(item.tag == "name"):
res["names"].append(item.text)
else:
res["desc"] = item.text
return res
def suggest_matching(docs,query):
"""
The idea is to suggest the document among docs which is most matching the query (query).
The suggestion is based on gensim's implementation of Latent Semantic Indexing, with tfidf as similarity measure.
"""
# stopword list comes from NLTK
# we might want to consider removing pucntuation
stoplist = stopwords.words('english')
texts = [[word for word in document[1].lower().split() if word not in stoplist] for document in docs]
"""
# this removes the words with lowest frequency: check if enabling it improves the results
all_tokens = sum(texts, [])
tokens_once = set(word for word in set(all_tokens) if all_tokens.count(word) == 1)
texts = [[word for word in text if word not in tokens_once] for text in texts]
"""
dictionary = corpora.Dictionary(texts)
dictionary.save('test.dict')
#print dictionary
#print dictionary.token2id
corpus = [dictionary.doc2bow(text) for text in texts]
logger.debug(corpus)
tfidf = models.TfidfModel(corpus)
index = similarities.SparseMatrixSimilarity(tfidf[corpus])
vec_bow = dictionary.doc2bow(query.lower().split())
vec_tfidf = tfidf[vec_bow] # convert the query to TFIDF space
sims = index[vec_tfidf] # perform a similarity query against the corpus
sims = sorted(enumerate(sims), key=lambda item: -item[1])
#print list(enumerate(sims))
res = [(docs[sims[i][0]],str(sims[i][1]),docs[i][0]) for i in range(len(sims))]
return res
def init_logger(verbose=False, log_file=None):
"""
Initialises a logger.
"""
import logging
l_logger = logging.getLogger("script")
if(verbose):
l_logger.setLevel(logging.DEBUG)
else:
l_logger.setLevel(logging.INFO)
if(log_file is None):
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(logging.Formatter("%(asctime)s -- %(name)s -- %(levelname)s -- %(message)s"))
l_logger.addHandler(ch)
l_logger.info("Logger initialised.")
else:
pass # I should actually complete this function as initialise a logger which writes to a file
return l_logger
def run(limit=5):
"""
Go through an input list of Smith IDs for entities, and for each of them
try to find a matching entry in DBpedia.
"""
try:
f = open(input_file,"r")
data = f.read().split("\n")
random.shuffle(data)
f.close()
logger.info("There are %i Smith IDs in the input list"%len(data))
for n in range(len(data)):
if(n < limit):
match_entity(data[n])
else:
break
except IOError:
print "this time didn't work"
def match_entity(id):
"""
Tries to match a Perseus-Smith entity against a DBpedia entry.
"""
logger.debug("%s"%id)
test_url = format_perseus_uri(id)
xml = get(test_url)
temp = transform_tei(xml)
names = set(parse_xml(temp)["names"])
desc = parse_xml(temp)["desc"] # this is the Smith's entry
for n in names:
#for t in n.split():
max_res = 10
lookup_results = parse_lookup_reply(do_lookup(n,max_res))
while(len(lookup_results) == max_res):
lookup_results = parse_lookup_reply(do_lookup(n,max_res*max_res))
if(len(lookup_results)==max_res):
break
documents = [(r["uri"],r["desc"]) for r in lookup_results if r["desc"] is not None]
logger.debug(documents)
if(len(documents)>1):
"""
there is > 1 result from dbpedia.
will try to disambiguate using TFIDF model
"""
results = suggest_matching(documents,query=desc)
for n,r in enumerate(results):
logger.debug("##%i## (%s) %s"%(n,r[1],r[0]))
puts(columns([colored.green("[SMITH DICTIONARY ENTRY]\n%s\n"%desc.encode("utf-8")),60],
[colored.magenta("[DBPEDIA 1st CANDIDATE] (TFIDF score: %s)\n\"%s\"\n"%(results[0][1].encode("utf-8"),results[0][0][1])),None]))
puts(colored.cyan("Is \"%s\" the sameAs \"%s\"?\n"%(test_url,results[0][0][0])))
answer = raw_input("[Yy/Nn]: ")
return True
elif(len(documents)==1):
""
""
puts(columns([colored.green("[SMITH DICTIONARY ENTRY]\n%s\n"%desc.encode("utf-8")),60],
[colored.magenta("[DBPEDIA 1st CANDIDATE]\n%s\n"%documents[0][1].encode("utf-8")),None]))
puts(colored.cyan("Is \"%s\" the sameAs \"%s\"?\n"%(test_url,documents[0][0])))
answer = raw_input("[Yy/Nn]: ")
return True
else:
print "No results from the DBpedia query"
return False
return
def get_input(input_file = "zbios.txt"):
"""
Go through an input list of Smith IDs for entities, and for each of them
try to find a matching entry in DBpedia.
"""
try:
f = open(input_file,"r")
data = f.read().split("\n")
random.shuffle(data)
f.close()
logger.info("There are %i Smith IDs in the input list"%len(data))
return data
except IOError:
print "There was a problem reading the input file"
return
def main():
"""
Run the whole thing.
"""
parser = argparse.ArgumentParser(prog="script.py",description='Match Perseus\' Smith URIs against DBpedia URIs.') # initialise the argument parser
parser.add_argument('--id', action="store", dest="id", type=str,default=None)
parser.add_argument('--verbose','-v',action='store_true', default=False,help='Logs DEBUG information (default is INFO level)')
args = parser.parse_args()
global logger
logger = init_logger(verbose=args.verbose,log_file=None) # initialise the logger
try:
assert args.id is not None
logger.debug("id==%s"%args.id)
try:
match_entity(args.id)
except Exception as inst:
print inst
#run(limit=1)
except:
logger.error("Needed valid ID: pass one using the %s parameter"%"--id")
data = get_input()
want_continue = True
while(want_continue):
try:
res = match_entity(data.pop())
if(res is True):
answer = raw_input("Do you want to continue? [Yy/Nn]: ")
if(answer == "Y" or answer == "y"):
want_continue = True
else:
want_continue = False
else:
print "Keep going!"
except Exception as inst:
print inst
print "There was a problem. Trying with the next one."
# initialise the logger
#run(limit=1)
# load the data
#load_data(opts.aph_volume,opts.input_filename,opts.json_filename)
if __name__ == '__main__':
main()