-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconvertwn.py
425 lines (344 loc) · 17.5 KB
/
convertwn.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#!python
import os
import operator
import sys
import cPickle as pickle
import subprocess
import sys
class WNimporter():
def __init__(self, wordnetRDFfilename = '../wordnet/wn31.nt'):
self.replacements = { 'http://wordnet-rdf.princeton.edu/ontology' : 'wdo', \
'http://wordnet-rdf.princeton.edu/wn31' : 'wn', \
'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' : 'w3#type', \
'http://www.w3.org/2000/01/rdf-schema#label' : 'w3#label', \
'http://www.w3.org/1999/02/22-rdf-syntax-ns#first' : 'w3#first', \
'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest' : 'w3#rest', \
'http://lemon-model.net/lemon' : 'lemon' }
self.filters = [ 'http://wordnet-rdf.princeton.edu/ontology#translation', \
'wn20', \
'wn30', \
'http://www.w3.org/2002/07/owl#sameAs', \
'http://wordnet-rdf.princeton.edu/ontology#old_sense_key', \
'http://wordnet-rdf.princeton.edu/ontology#verbnet_class' ]
self.WNfname = wordnetRDFfilename
self.path = os.path.dirname(self.WNfname)
self.nodesFilename = os.path.join(self.path, 'WNnodes.csv')
self.edgesFilename = os.path.join(self.path, 'WNedges.csv')
self.logFilename = os.path.join(self.path, 'WNimporter.log')
self.WN_unique_fname = self.WNfname[:-3] + '_unique' + self.WNfname[-3:]
def convertWordNet(self):
# Wordnet 3.1 RDF has duplicate lines. So first of all we filter them
# we use the standard UNIX command 'sort' or open the file if it already exists
if not os.path.isfile(self.WN_unique_fname):
print '#### Filtering duplicated lines in', self.WNfname, ': it will take a while...',
sys.stdout.flush()
args = ['sort', '-us', '-o', self.WN_unique_fname, self.WNfname]
subprocess.check_call(args)
print 'done.'
print '#### Opening WordNet 3.1 with unique lines...',
sys.stdout.flush()
self.wordnetLines = self.readFile(self.WN_unique_fname)
print 'done.'
print '#### Filtering and replacing prefixes in WordNet...',
sys.stdout.flush()
self.triples = self.filterAndReplace(self.wordnetLines)
print 'done.'
print '#### Creating nodes and relationships...',
sys.stdout.flush()
self.processTriples(self.triples)
print 'done.'
print '#### Creating CSV files...',
sys.stdout.flush()
self.calculateLowercaseNodesIDs()
self.writeCSVs()
print 'done.'
print '#### Log file written in ',
self.writeLogFile()
#def writeFile(self, lines, filename):
# with open(filename, 'w') as f:
# for l in lines:
# f.write(l)
def readFile(self, fname):
f = open(fname)
lines = f.readlines()
f.close()
return lines
def filterAndReplace(self, lines):
FRtriples = []
for l in lines:
if l != "\n":
triple = self.getTriple(l)
if all( [f not in e for f in self.filters for e in triple] ): # if none of the self.filters applies
for i in range(0, len(triple)):
for r in self.replacements:
triple[i] = triple[i].replace(r, self.replacements[r])
FRtriples.append(triple)
return FRtriples
def getTriple(self, l):
first_space = l.find(' ')
second_space = l.find(' ', first_space+1)
triple = [l[0:first_space].strip(), l[first_space+1:second_space].strip(), l[second_space:-3].strip()] # removing ' .\n' at the end
for i in range(0, 3):
if triple[i].startswith('<') and triple[i].endswith('>'):
triple[i] = triple[i][1:-1]
return triple
def psd(self, d, returnSorted = False):
sd = sorted(d.items(), key=operator.itemgetter(1), reverse=True)
longest = max([len(i) for i in d.keys()])
for i in sd:
print i[0].ljust(longest + 5), str(i[1]).rjust(10)
if returnSorted:
return sd
def processTriples(self, triples):
self.nodes = {}
self.relationships = []
self.duplicate_gloss = []
for idx, t in enumerate(triples):
#if idx != 0 and idx % 1000 == 0:
# print '.',
try:
#t = getTriple(l)
if t[0] in ['', '\n']:
continue # empty line
if t[0].startswith('_'):
continue # we ignore lists
nodeid = None
t0Sdash = t[0].split('#')
if len(t0Sdash) > 1:
if t0Sdash[1] == 'CanonicalForm' or t0Sdash[1].startswith('Form-'):
if t[1] == 'lemon#writtenRep':
nodeid = t0Sdash[0]
if nodeid not in self.nodes:
self.nodes[nodeid] = {}
if 'Forms' not in self.nodes[nodeid]:
self.nodes[nodeid]['Forms'] = {}
self.nodes[nodeid]['Forms'][t0Sdash[1]] = t[2].split('@')[0][1:-1]
# #CanonicalForm and #Form- nodes have only 'lemon#writtenRep' and 'w3#type' relationships
# As we don't create nodes for Forms, we ignore the 'w3#type' relationship that would
# overwrite the base node one.
continue
elif t0Sdash[1].startswith('Component-'):
continue # we ignore 'Component-' nodes
# else:
# We consider it as a normal node, e.g. 'wn/nationality-n#1-n'
# this is the id:ID property. We don't need to set it as it's already implicitly in the dictionary as key
nodeid = t[0]
if nodeid not in self.nodes:
self.nodes[nodeid] = {}
n = self.nodes[nodeid] # shortcut
# LABEL property
if t[1] == 'w3#type' and t[2] not in ['lemon#Form', 'lemon#Component']: # nodeid is set to the base component
n[':LABEL'] = t[2]
# Natural Text properties
elif t[1] in ['wdo#gloss']:
if t[1] in n:
self.duplicate_gloss.append('Duplicate gloss in ' + t[0] + '\n' + n[t[1]] + '\n' + t[2].split('@')[0][1:-1] + '\n')
#print n[ t[1] ], 'already in n!', t[0], t[1], t[2]
#print 'Ignoring.'
#break
n[ t[1] ] = t[2].split('@')[0][1:-1] # "the st...zation"@eng -> the st...zation
# Properties with multiple values
elif t[1] in ['w3#label', 'wdo#sample', 'wdo#verb_frame_sentence']:
if t[1] not in n:
n[ t[1] ] = []
n[ t[1] ].append( t[2].split('@')[0][1:-1] )
# Relationships
elif t[1] in ['lemon#sense', 'lemon#reference', 'wdo#derivation', 'wdo#hyponym', 'wdo#hypernym', 'wdo#similar', 'wdo#antonym', \
'wdo#member_meronym', 'wdo#member_holonym', 'wdo#pertainym', 'wdo#also', \
'wdo#part_meronym', 'wdo#part_holonym', 'wdo#instance_hyponym', 'wdo#instance_hypernym' \
'wdo#domain_member_category', 'wdo#domain_category', 'wdo#verb_group', 'wdo#domain_region', 'wdo#domain_member_region' \
'wdo#attribute', 'wdo#domain_usage', 'wdo#domain_member_usage', 'wdo#substance_holonym', 'wdo#substance_meronym', \
'wdo#entail', 'wdo#action', 'wdo#cause', 'wdo#theme', 'wdo#result', 'wdo#participle', 'wdo#agent', 'wdo#patient', \
'wdo#beneficiary', 'wdo#location', 'wdo#instrument', 'wdo#goal', 'wdo#creator', 'wdo#product', 'wdo#experiencer']:
self.relationships.append( {':START_ID': nodeid, ':END_ID': t[2], ':TYPE': t[1]} )
# Reverse relationships
elif t[1] in ['wdo#synset_member']:
self.relationships.append( {':START_ID': t[2], ':END_ID': nodeid, ':TYPE': t[1]} )
# Integer properties
elif t[1] in ['wdo#sense_number', 'wdo#tag_count', 'wdo#lex_id']:
if t[1] in n:
print t[1], 'already in n!', t[0], t[1], t[2]
break
if t[2] != '"None"':
n[ t[1] ] = int( t[2].split('^^')[0][1:-1] ) # "0"^^<http://www.w3.org/2001/XMLSchema#integer> -> 0
#else:
# print '\nNumeric property is None, ignoring.', t[0], t[1], t[2]
# String properties
elif t[1] in ['wdo#part_of_speech', 'wdo#lexical_domain', 'wdo#adjposition', 'wdo#phrase_type']:
if t[1] in n:
print t[1], 'already in n!', t[0], t[1], t[2]
break
n[ t[1] ] = t[2]
# IGNORED:
# lemon#canonicalForm : this is handled differently because we don't create a node for canonicalForm, we have an attribute in the base node
# lemon#writtenRep : these relationships are for CanonicalForm and Form nodes only
# lemon#otherForm : link to other Forms. But here Forms are properties, not nodes.
# wdo#sense_tag : not sure what to do with these...
# w3#first : used in lists for sense sentences, which we ignore.
# w3#rest : same
# lemon#decomposition : sentence decomposition, we ignore parts of sentences
except Exception as e:
print ''
print e.__repr__()
print e.message
print 'idx =', idx
print t
print 'Exception, exiting'
break
def writeCSVs(self):
self.not_found_nodes = []
with open(self.nodesFilename, 'w') as f:
nodeProperties = ['id:ID', ':LABEL', 'wdo#gloss', 'wdo#part_of_speech', 'wdo#sense_number:int', 'wdo#lex_id:int', 'wdo#tag_count:int', 'Forms[]', \
'wdo#lexical_domain', 'w3#label[]', 'wdo#sample[]', 'wdo#verb_frame_sentence[]', 'wdo#adjposition', 'wdo#phrase_type']
header = '{0}\n'.format('\t'.join(nodeProperties))
f.write(header)
for nodeID in self.nodes:
n = self.nodes[nodeID] # faster to type
l = ''
l += '"{0}"\t"{1}"\t'.format(nodeID, n[':LABEL'])
if 'wdo#gloss' in n:
l += '"{0}"\t'.format(n['wdo#gloss'])
else:
l += '\t'
if 'wdo#part_of_speech' in n:
l += '"{0}"\t'.format(n['wdo#part_of_speech'])
else:
l += '\t'
if 'wdo#sense_number' in n:
l += str(n['wdo#sense_number']) + '\t'
else:
l += '\t'
if 'wdo#lex_id' in n:
l += str(n['wdo#lex_id']) +'\t'
else:
l += '\t'
if 'wdo#tag_count' in n:
l += str(n['wdo#tag_count']) +'\t'
else:
l += '\t'
if 'Forms' in n:
# I get all the forms, sort them based on the name (CanonicalForm, Form1, etc.), get only the data,
# create a string with ';' as delimiter and finally surround it with quotes and add a '\t'
l += '"{0}"\t'.format(';'.join([seq[1] for seq in sorted(n['Forms'].items(), key=operator.itemgetter(0))]))
else:
l += '\t'
if 'wdo#lexical_domain' in n:
l += '"{0}"\t'.format(n['wdo#lexical_domain'])
else:
l += '\t'
if 'w3#label' in n:
l +='"{0}"\t'.format(';'.join(n['w3#label']))
else:
l += '\t'
if 'wdo#sample' in n:
l += '"{0}"\t'.format(';'.join(n['wdo#sample']))
else:
l += '\t'
if 'wdo#verb_frame_sentence' in n:
l += '"{0}"\t'.format(';'.join(n['wdo#verb_frame_sentence']))
else:
l += '\t'
if 'wdo#adjposition' in n:
l += '"{0}"\t'.format(n['wdo#adjposition'])
else:
l += '\t'
if 'wdo#phrase_type' in n:
l += '"{0}"\n'.format(n['wdo#phrase_type'])
else:
l += '\n'
f.write(l)
self.rels_with_orphan_nodes = []
with open(self.edgesFilename, 'w') as f:
edgeProperties = [':START_ID', ':END_ID', ':TYPE']
header = '{0}\n'.format('\t'.join(edgeProperties))
f.write(header)
for r in self.relationships:
start_node = self.lookForNode(r[':START_ID'])
end_node = self.lookForNode(r[':END_ID'])
if start_node != None and end_node != None:
rel = '"{0}"\n'.format( '"\t"'.join([start_node, end_node, r[':TYPE']]) )
f.write(rel)
else:
self.rels_with_orphan_nodes.append(r)
def calculateLowercaseNodesIDs(self):
self.lowercase_nodes = {}
self.collisions = []
for n in self.nodes:
l = n.lower()
if l in self.lowercase_nodes:
self.collisions.append( [n, l, self.lowercase_nodes[l]] )
#print 'COLLISION!', self.lowercase_nodes[l], l, n
else:
self.lowercase_nodes[ n.lower() ] = n
def lookForNode(self, nid):
if nid in self.nodes:
return nid
else:
# capitalize first letter
# 'wn/djiboutian-a#1-a' -> 'wn/Djiboutian-a#1-a'
altNid = nid[:3] + nid[3].capitalize() + nid[4:]
if altNid not in self.nodes:
Shash = nid.split('#') # if there is a '#' suffix
altNid = Shash[0][3:] #'orthodox+church-n'
if '+' in nid: # 'wn/orthodox+church-n#1-n'
Splus = altNid.split('+')
altNid = 'wn/' + '+'.join([ t.capitalize() for t in Splus ]) # 'wn/Orthodox+Church-n'
elif altNid.count('-') > 1: # 'anglo-saxon-n'
Sdash = altNid.split('-')
altNid = 'wn/' + '-'.join( [ t.capitalize() for t in Sdash[:-1] ] + [Sdash[-1]] ) # 'wn/Anglo-Saxon-n', we don't capitalize the last 'n'
if len(Shash) > 1:
altNid = altNid + '#' + Shash[1] # 'wn/Orthodox+Church-n#1-n'
if altNid not in self.nodes:
# let's try with a lowercase version
l = nid.lower()
if l in self.lowercase_nodes:
altNid = self.lowercase_nodes[l]
else:
self.not_found_nodes.append(nid)
return None
return altNid
def writeLogFile(self):
log = []
log.append('#### WordNet import log file.')
log.append('\n#### Filters: ')
for f in self.filters:
log.append(f)
log.append('\n#### Replacements: ')
for r in self.replacements:
log.append( r + ' : ' + self.replacements[r] )
log.append('\n#### Node referred in relationships but not existing:')
for n in self.not_found_nodes:
log.append(n)
log.append('\n#### The above nodes were referred in the following triples:')
for r in self.rels_with_orphan_nodes:
log.append( '<{0}> <{1}> <{2}> .'.format(r[':START_ID'], r[':END_ID'], r[':TYPE']) )
log.append('\n#### Duplicate gloss properties:')
for d in self.duplicate_gloss:
log.append(d)
self.log = log
with open(self.logFilename, 'w') as f:
for l in self.log:
f.write(l + '\n')
print self.logFilename
def main():
numargs = len(sys.argv)
w = None
if numargs == 2:
w = WNimporter(sys.argv[1])
else:
w = WNimporter()
try:
w.convertWordNet()
except Exception as e:
print ''
print e.__repr__()
print e.message
print "Usage:\npython convertwn.py <input file>\n"
# if numargs == 3:
# cn5ToCSV(sys.argv[1], True)
# if numargs == 2:
# cn5ToCSV(sys.argv[1], False)
# if numargs < 2 or numargs > 3:
# print "Usage:\npython convertcn.py <input directory> [ALL_LANGUAGES]\n"
if __name__ == "__main__":
main()