-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
272 lines (244 loc) · 9.98 KB
/
utils.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
from configs import *
from entity import Entity
import codecs
from itertools import *
import rdflib
import urllib
import StringIO
from urllib import urlopen
from nltk.corpus import stopwords
from time import localtime, strftime
from fuzzywuzzy import fuzz
import string
from HTTP4Store import HTTP4Store
from rdflib import Graph, URIRef, Namespace, Literal
def gettext(elem, ignore=[], newline=[]):
text = elem.text or ""
text = text.rstrip()
#print elem.tag + "|" + str(newline)
#print text
#TODO: This is still a little whacky. Got's to work out how to handle this better....
for subelem in elem:
if subelem.tag not in ignore: text = text + " " + gettext(subelem, ignore, newline)
if subelem.tag in newline:
#print text
text = "\n" + text.lstrip() + "\n"
if subelem.tail:
text = text + subelem.tail.strip()
return text
def procdid(xfrag):
namespace = "{urn:isbn:1-931666-22-9}"
md = {
'dct:abstract': [], 'arch:langnote': [], 'dc:language': [], 'arch:corpcreator': [], 'arch:perscreator': [],
'dc:description': [], 'arch:location': [], 'dc:date': [], 'arch:datenormal': [],
'arch:materialspec': [],
}
entities = []
#print "FragTag" + gettext(xfrag).encode('utf-8')
for subelem in xfrag:
#print "Subelem" + str(subelem)
tag = subelem.tag.replace(namespace, '')
#print "Tag" + tag
if tag == "abstract": md[fieldrenamings[tag]].append(gettext(subelem))
if tag == "langmaterial":
md[fieldrenamings[tag]].append(gettext(subelem).rstrip())
for subsub in subelem:
if subsub.tag.replace(namespace, '') == 'language':
md['dc:language'].append(subsub.get('langcode'))
#TODO: This is one piece of entity processing....
# Fucker -- how to deal with component side?
if tag == "origination":
for child in subelem:
chitag = child.tag.replace(namespace, '')
chisource = child.get('source')
#right, so this needs to define an entity & return it,
#so I need to make sure I parse this from return
entity = Entity(chitag, gettext(child), chisource)
entities.append(entity)
if chitag == "corpname": md[fieldrenamings[tag][chitag]].append(gettext(child))
if chitag == "persname": md[fieldrenamings[tag][chitag]].append(gettext(child))
if tag == "physdesc": md[fieldrenamings[tag]].append(gettext(subelem))
if tag == "physloc": md[fieldrenamings[tag]].append(gettext(subelem))
if tag == "unitdate":
md[fieldrenamings[tag]].append(gettext(subelem))
#print subelem.attrib
#if '{http://www.w3.org/1999/xlink}normal' in subelem.attrib:
if'normal' in subelem.attrib:
md[fieldrenamings['normal']].append(subelem.get('normal'))
if tag == "materialspec": md[fieldrenamings[tag]].append(gettext(subelem))
return md, entities
def fuzzer(localstring, dbpstring):
lwl = localstring.replace('-','').replace(',.', '').split()
lfwl = [w for w in lwl if not w in stopwords.words('english')]
dwl = dbpstring.replace('-','').split()
dfwl = [w for w in dwl if not w in stopwords.words('english')]
ratio = fuzz.token_sort_ratio(str(lfwl), str(dfwl))
return ratio
def sameAsObj(uri):
q = ( "SELECT ?o WHERE { <" + uri + ">"
"<http://www.w3.org/2002/07/owl#sameas> ?o " +
"}" )
store = HTTP4Store('http://localhost:8080')
response = store.sparql(q)
sameAs = []
for data in response:
if data[u'o'].replace('/data/', '/resource/') not in sameAs: sameAs.append(data[u'o'].replace('/data/', '/resource/'))
return sameAs
def dbpField(sub, prop):
#sub = sub.replace('/data/', '/resource/')
q = ( "SELECT ?o WHERE { <" + sub + ">"
"<" + prop + "> ?o " +
"FILTER langMatches( lang(?o), \"EN\" )" +
"}" )
store = HTTP4Store('http://localhost:8080')
print q
response = store.sparql(q)
propData = []
for data in response:
if data[u'o'].rstrip('@EN').strip('"') not in propData: propData.append(data[u'o'].rstrip('@EN').strip('"'))
return propData
def load4store(data):
#def load4store(graph, data):
#Yeah, probably want to generalize this as above, but for now, f-it...
store = HTTP4Store('http://localhost:8080')
''''
#Is this even necessary?
if type(data) == unicode:
#print "URL: " + data.encode('utf-8')
if type(data) == list:
#print "Triple: " + " -- ".join(data)
'''
urlstr = data[3].replace("/resource/", "/data/").encode('utf-8')
url = urlopen(urlstr)
raw = url.read().decode('utf-8')
r = store.append_graph('http://chrpr.net/dbpedia', raw.encode('utf-8'), 'xml')
relGraph = rdflib.Graph(identifier="http://chrpr.net/data/relations")
uri = URIRef("http://chrpr.com/data/" + data[0].encode('utf-8') + ".rdf")
if data[1] == 'owl' and data[2] == 'sameas':
namespaces = {
"owl": Namespace("http://www.w3.org/2002/07/owl#")
}
curies = { "owl": "OWL" }
relGraph.bind("owl", "http://www.w3.org/2002/07/owl#")
relGraph.add([uri, namespaces['owl']['sameas'], URIRef(urlstr)])
turtle = relGraph.serialize(format="turtle")
r2 = store.append_graph('http://chrpr.net/relations', turtle, 'turtle')
def dbpmatch(entitySet):
start = strftime("%a, %d %b %Y %H:%M:%S +0000", localtime())
path = '/home/charper/store/dbpedia/'
log = codecs.open('dbp-log.txt', 'a', encoding='utf-8')
fuzzmatch = codecs.open('fuzzmatch.txt', 'w', encoding='utf-8')
bestmatch = codecs.open('bestmatch.txt', 'w', encoding='utf-8')
tokens = {}
table = string.maketrans("","")
for eid, entity in entitySet.iteritems():
headid = eid
tokens[headid] = []
for heading in entity.metadata['lookup']:
#headid = line.split('|')[0]
wl = heading.replace('-','').split()
fwl = [w for w in wl if not w in stopwords.words('english')]
full = []
full.append(heading)
full.extend(fwl)
# print full
# Borrowed from here: http://stackoverflow.com/questions/5486337/stop-words-using-nltk-in-python
tokens[headid].append(full)
#tokens.append(full)
#for t in tokens:
# print t
counter = 0
gcounter = 0
match = 0
nomatch = 0
topfuzz = {}
#with codecs.open(path + '500000_en.ttl', 'r', encoding='utf-8') as f:
#fname = path + 'labels_en.ttl'
fname = path + 'labels_en.ttl'
with codecs.open(fname, 'r', encoding='utf-8') as f:
for next_n_lines in izip_longest(*[f] * 100000, fillvalue="xxx"):
_file = StringIO.StringIO(next_n_lines)
#print file.read()
g = rdflib.Graph()
#for line in next_n_lines:
#result = g.parse(next_n_lines, format='n3')
# counter += 1
#print str(counter) + "|" + line
#while counter <= 1:
#result = g.parse(line, format='n3')
# g.add(line)
#gcounter += 1
#print gcounter
#result = g.parse(_file.read(), format='n3')
for line in next_n_lines:
if line != 'xxx':
result = g.parse(data=line, format='n3')
#print len(g)
for stmt in g:
#head = stmt[2].encode('utf-8').translate(table, string.punctuation)
head = stmt[2]
wl = head.replace('-','').replace(',', '').split()
fwl = [w for w in wl if not w in stopwords.words('english')]
# Borrowed from here: http://stackoverflow.com/questions/5486337/stop-words-using-nltk-in-python
for headid, lookups in tokens.iteritems():
for t in lookups:
#head = t[0]
if len(set(fwl).intersection( set(t[1:]) )) > 0:
ratio = fuzz.token_sort_ratio(str(fwl), str(t[1:]))
if ratio > 85:
#print str(stmt[2]) + " matches " + str(t[0]) + " fuzz: " + str(ratio)
#printmatch = str(stmt[0]) + "|" + str(stmt[2]) + "|" + str(t[0]).rstrip() + "|" + str(ratio)
printmatch = headid + "|" + stmt[0] + "|" + stmt[2] + "|" + t[0].rstrip() + "|" + str(ratio)
matchdata = [[stmt[0], stmt[2], str(ratio)]]
if headid not in topfuzz:
topfuzz[headid] = matchdata
else: topfuzz[headid].append(matchdata)
fuzzmatch.write(printmatch + "\n")
print printmatch.encode('utf-8')
match += 1
else:
nomatch += 1
#print str(fwl) + " matches " + str(t)
#match += 1
else:
nomatch += 1
#print stmt[2].encode('utf-8')
#for line in next_n_lines:
#for stmt in g:
# counter += 1
# print stmt
#print str(gcounter) + "|" + str(counter) + "|" + line.encode('utf-8')
# process next_n_lines
end = strftime("%a, %d %b %Y %H:%M:%S +0000", localtime())
store = HTTP4Store('http://localhost:8080')
for headid, fuzzes in topfuzz.iteritems():
print headid
print fuzzes
highest = 0
for matched in fuzzes:
#print "--".join(matched)
if matched[2] > highest:
lead = matched[0]
highest = matched[2]
bestmatch.write(headid + "|" + "|" + lead + "|" + str(highest) + "\n")
urlstr = lead.replace("/resource/", "/data/").encode('utf-8')
url = urlopen(urlstr)
raw = url.read().decode('utf-8')
r = store.append_graph('http://chrpr.net/dbpedia', raw.encode('utf-8'), 'xml')
relGraph = rdflib.Graph(identifier="http://chrpr.net/data/relations.rdf")
uri = URIRef("http://chrpr.com/data/" + headid.encode('utf-8') + ".rdf")
namespaces = {
"owl": Namespace("http://www.w3.org/2002/07/owl#")
}
curies = { "owl": "OWL" }
relGraph.bind("owl", "http://www.w3.org/2002/07/owl#")
relGraph.add([uri, namespaces['owl']['sames'], URIRef(urlstr)])
turtle = relGraph.serialize(format="turtle")
r2 = store.append_graph('http://chrpr.net/relations', turtle, 'turtle')
print '{0} <-> {1}'.format(start, end)
print 'No Match Count: ' + str(nomatch)
print 'Match Count: ' + str(match)
log.write(fname + "\n")
log.write('{0} <-> {1}'.format(start, end) + "\n")
log.write('No Match Count: ' + str(nomatch) + "\n")
log.write('Match Count: ' + str(match) + "\n")