-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcorpus.py
680 lines (607 loc) · 26.3 KB
/
corpus.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
"""
This class is a modification from the publicly available repository at:
https://github.com/hazemalsaied/ATILF-LLF.v2/blob/master/Src/corpus.py
"""
import os
import operator
import nltk
from nltk import WordNetLemmatizer
from nltk.tokenize import WordPunctTokenizer
class Corpus:
"""
a class used to encapsulate all the information of the corpus
"""
def __init__(self, path):
"""
an initializer of the corpus, responsible of creating a structure of objects encapsulating all the information
of the corpus, its sentences, tokens and MWEs.
This function iterate over the lines of corpus document to create the precedent ontology
:param path: the physical path of the corpus document
"""
self.sentNum, self.mweNum, self.intereavingNum, self.emeddedNum, self.singleWordExp, self.continousExp = 0, 0, 0, 0,0,0
cuptFile = None
if os.path.isfile(os.path.join(path, 'train.cupt')):
cuptFile = os.path.join(path, 'train.cupt')
#mweFile = os.path.join(path, 'train.parsemetsv')
self.sentences = []
devCupt = None
if os.path.isfile(os.path.join(path, 'dev.cupt')):
devCupt = os.path.join(path, 'dev.cupt')
testCupt = None
if os.path.isfile(os.path.join(path, 'test.cupt')):
testCupt = os.path.join(path, 'test.cupt')
blindTestCupt = None
testBlind = os.path.join(path, 'test.blind.cupt')
if os.path.isfile(os.path.join(path, 'test.blind.cupt')):
blindTestCupt = os.path.join(path, 'test.blind.cupt')
if cuptFile is not None: # and testCupt is not None:
self.sentences, self.mweNum = Corpus.readCuptFile(cuptFile)
#self.mweNum = Corpus.readMweFile(mweFile, self.sentences)
self.sentNum = len(self.sentences)
for sent in self.sentences:
self.emeddedNum += sent.recognizeEmbededVMWEs()
self.intereavingNum += sent.recognizeInterleavingVMWEs()
x,y = sent.recognizeContinouosandSingleVMWEs()
self.singleWordExp += x
self.continousExp += y
if devCupt is not None: # added by me
self.devSents, self.devMweNum = Corpus.readCuptFile(devCupt)
if testCupt is not None: # added by me
self.testSents, self.testMweNum = Corpus.readCuptFile(testCupt)
if blindTestCupt is not None: # added by me
self.blindTestSents = Corpus.readBlindTestFile(blindTestCupt)
'''
else: # Am I right to thing that this is for those datasets that have no conllu info?
self.sentences, self.sentNum, self.mweNum = Corpus.readSentences(mweFile)
self.testSents, x, y = Corpus.readSentences(testBlind, forTest=True)
for sent in self.sentences:
self.emeddedNum += sent.recognizeEmbededVMWEs()
self.intereavingNum += sent.recognizeInterleavingVMWEs()
x, y = sent.recognizeContinouosandSingleVMWEs()
self.singleWordExp += x
self.continousExp += y
'''
@staticmethod
def readCuptFile(cuptFile):
sentences = []
mweNum = 0 # ADDED by me
with open(cuptFile) as corpusFile:
# Read the corpus file
lines = corpusFile.readlines()
sent = None
senIdx = 0
sentId = ''
sentenceText = ''
for line in lines:
if len(line) > 0 and line.endswith('\n'):
line = line[:-1]
if line.startswith('# sent_id:'):
sentId = line.split('# sentid:')[1].strip()
elif line.startswith('# text ='):
sentenceText = line.split('# text =')[1].strip() # ADDED by me
continue
elif line.startswith('#'):
continue
elif line.startswith('1\t'):
if sentId.strip() != '':
sent = Sentence(senIdx, sentid=sentId)
else:
sent = Sentence(senIdx)
senIdx += 1
sentences.append(sent)
if not line.startswith('#'):
lineParts = line.split('\t')
if len(lineParts) != 11 or '-' in lineParts[0]: # I CHANGED it to 11
continue
morpho = ''
if lineParts[5] != '_':
morpho = lineParts[5].split('|')
if lineParts[6] != '_' and lineParts[6] != '-': # I added the and part
token = Token(lineParts[0], lineParts[1], lemma=lineParts[2], ## I removed .lower() from lineParts[1]
abstractPosTag=lineParts[3], morphologicalInfo=morpho,
dependencyParent=int(lineParts[6]),
dependencyLabel=lineParts[7])
else:
print("No dependency!: ", lineParts)
# ME: It does never come to this else. Once it got here and it gave errors.
# In the case of English, this happened when the token id is 8.1, 22.1, etc.
token = Token(lineParts[0], lineParts[1], lemma=lineParts[2], ## I removed .lower() from lineParts[1]
abstractPosTag=lineParts[3], morphologicalInfo=morpho,
dependencyLabel=lineParts[7])
if lineParts[4] != '_':
token.posTag = lineParts[4]
else:
token.posTag = lineParts[3]
#NEW added by me
if lineParts[-1] != '*':
vMWEids = lineParts[-1].split(';')
for vMWEid in vMWEids:
id = int(vMWEid.split(':')[0])
# New MWE captured
if id not in sent.getWMWEIds():
if len(vMWEid.split(':')) > 1:
type = str(vMWEid.split(':')[1])
vMWE = VMWE(id, token, type)
else:
vMWE = VMWE(id, token)
mweNum += 1
sent.vMWEs.append(vMWE)
# Another token of an under-processing MWE
else:
vMWE = sent.getVMWE(id)
if vMWE is not None:
vMWE.addToken(token)
# associate the token with the MWE
token.setParent(vMWE)
########################################
# Associate the token with the sentence
sent.tokens.append(token)
sent.text += token.text + ' '
return sentences, mweNum # ADDED by me
@staticmethod
def readBlindTestFile(cuptFile):
sentences = []
mweNum = 0 # ADDED by me
with open(cuptFile) as corpusFile:
# Read the corpus file
lines = corpusFile.readlines()
sent = None
senIdx = 0
sentId = ''
sentenceText = ''
for line in lines:
if len(line) > 0 and line.endswith('\n'):
line = line[:-1]
if line.startswith('# sent_id:'):
sentId = line.split('# sentid:')[1].strip()
elif line.startswith('# text ='):
sentenceText = line.split('# text =')[1].strip() # ADDED by me
continue
elif line.startswith('#'):
continue
elif line.startswith('1\t'):
if sentId.strip() != '':
sent = Sentence(senIdx, sentid=sentId)
else:
sent = Sentence(senIdx)
senIdx += 1
sentences.append(sent)
if not line.startswith('#'):
lineParts = line.split('\t')
if len(lineParts) != 11 or '-' in lineParts[0]: # I CHANGED it to 11 since we have 11 columns
continue
morpho = ''
if lineParts[5] != '_':
morpho = lineParts[5].split('|')
if lineParts[6] != '_' and lineParts[6] != '-': # I added the and part
token = Token(lineParts[0], lineParts[1], lemma=lineParts[2], ## I removed .lower() from lineParts[1]
abstractPosTag=lineParts[3], morphologicalInfo=morpho,
dependencyParent=int(lineParts[6]),
dependencyLabel=lineParts[7])
else:
print(lineParts)
# It does never come to this else. Once it got here and it gave errors.
token = Token(lineParts[0], lineParts[1], lemma=lineParts[2], ## I removed .lower() from lineParts[1]
abstractPosTag=lineParts[3], morphologicalInfo=morpho,
dependencyLabel=lineParts[7])
if lineParts[4] != '_':
token.posTag = lineParts[4]
else:
token.posTag = lineParts[3]
########################################
# Associate the token with the sentence
sent.tokens.append(token)
sent.text += token.text + ' '
return sentences # ADDED by me
'''
@staticmethod
def readMweFile(mweFile, sentences):
mweNum = 0
with open(mweFile) as corpusFile:
# Read the corpus file
lines = corpusFile.readlines()
noSentToAssign = False
sentIdx = 0
for line in lines:
if line == '\n' or line.startswith('# sentence-text:') or (
line.startswith('# sentid:') and noSentToAssign) :
continue
if len(line) > 0 and line.endswith('\n'):
line = line[:-1]
if line.startswith('1\t'):
sent = sentences[sentIdx]
sentIdx += 1
lineParts = line.split('\t')
if '-' in lineParts[0]:
continue
if lineParts is not None and len(lineParts) == 4 and lineParts[3] != '_':
token = sent.tokens[int(lineParts[0]) - 1]
vMWEids = lineParts[3].split(';')
for vMWEid in vMWEids:
id = int(vMWEid.split(':')[0])
# New MWE captured
if id not in sent.getWMWEIds():
if len(vMWEid.split(':')) > 1:
type = str(vMWEid.split(':')[1])
vMWE = VMWE(id, token, type)
else:
vMWE = VMWE(id, token)
mweNum += 1
sent.vMWEs.append(vMWE)
# Another token of an under-processing MWE
else:
vMWE = sent.getVMWE(id)
if vMWE is not None:
vMWE.addToken(token)
# associate the token with the MWE
token.setParent(vMWE)
return mweNum
'''
@staticmethod
def readSentences(mweFile, forTest=False):
sentences = []
sentNum, mweNum = 0, 0
with open(mweFile) as corpusFile:
# Read the corpus file
lines = corpusFile.readlines()
sent = None
senIdx = 1
for line in lines:
if len(line) > 0 and line.endswith('\n'):
line = line[:-1]
if line.startswith('1\t'):
# sentId = line.split('# sentid:')[1]
if sent is not None:
# Represent the sentence as a sequece of tokens and POS tags
sent.setTextandPOS()
if not forTest:
sent.recognizeEmbededVMWEs()
sent.recognizeInterleavingVMWEs()
sent = Sentence(senIdx)
senIdx += 1
sentences.append(sent)
elif line.startswith('# sentence-text:'):
sentText = ''
if len(line.split(':')) > 1:
sent.text = line.split('# sentence-text:')[1]
lineParts = line.split('\t')
# Empty line or lines of the form: "8-9 can't _ _"
if len(lineParts) != 4 or '-' in lineParts[0]:
continue
token = Token(lineParts[0], lineParts[1])
# Trait the MWE
if not forTest and lineParts[3] != '_':
vMWEids = lineParts[3].split(';')
for vMWEid in vMWEids:
id = int(vMWEid.split(':')[0])
# New MWE captured
if id not in sent.getWMWEIds():
type = str(vMWEid.split(':')[1])
vMWE = VMWE(id, token, type)
mweNum += 1
sent.vMWEs.append(vMWE)
# Another token of an under-processing MWE
else:
vMWE = sent.getVMWE(id)
if vMWE is not None:
vMWE.addToken(token)
# associate the token with the MWE
token.setParent(vMWE)
# Associate the token with the sentence
sent.tokens.append(token)
sentNum = len(sentences)
return sentences, sentNum, mweNum
class Sentence:
"""
a class used to encapsulate all the information of a sentence
"""
def __init__(self, id, sentid=''):
self.sentid = sentid
self.id = id
self.tokens = []
self.vMWEs = []
self.identifiedVMWEs = []
self.text = ''
self.initialTransition = None
self.featuresInfo = []
@staticmethod
def fromTextToSent(text):
tokenizer = WordPunctTokenizer()
wordNetLemmatiser = WordNetLemmatizer()
sent = Sentence(0)
sent.text = text
tokenList = tokenizer.tokenize(text)
posTags = nltk.pos_tag(tokenList)
for token in tokenList:
tokenLemma = wordNetLemmatiser.lemmatize(token)
tokenPos = posTags[tokenList.index(token)][1]
tokenObj = Token(tokenList.index(token), token, lemma=tokenLemma, posTag=tokenPos)
sent.tokens.append(tokenObj)
return sent
def getWMWEs(self):
return self.vMWEs
def getWMWEIds(self):
result = []
for vMWE in self.vMWEs:
result.append(vMWE.getId())
return result
def getVMWE(self, id):
for vMWE in self.vMWEs:
if vMWE.getId() == int(id):
return vMWE
return None
def setTextandPOS(self):
tokensTextList = []
for token in self.tokens:
self.text += token.text + ' '
tokensTextList.append(token.text)
self.text = self.text.strip()
def recognizeEmbededVMWEs(self):
if len(self.vMWEs) <= 1:
return 0
result = 0
traitedPairs = []
for vMwe1 in self.vMWEs:
for vMwe2 in self.vMWEs:
if vMwe1 is not vMwe2:
isTraited = False
for pair in traitedPairs:
if vMwe1 in pair and vMwe2 in pair:
isTraited = True
if not isTraited:
traitedPairs.append([vMwe1, vMwe2])
# Get The longer VMWE
masterVmwe = vMwe1
slaveVmwe = vMwe2
if len(vMwe2.tokens) > len(vMwe2.tokens):
masterVmwe = vMwe2
slaveVmwe = vMwe1
slaveVmwe.isEmbeded = True
for token in slaveVmwe.tokens:
if masterVmwe not in token.parentMWEs:
slaveVmwe.isEmbeded = False
if slaveVmwe.isEmbeded:
result += 1
return result
def recognizeContinouosandSingleVMWEs(self):
singleWordExp, continousExp = 0,0
for mwe in self.vMWEs:
if len(mwe.tokens) == 1:
mwe.isSingleWordExp = True
mwe.isContinousExp = True
singleWordExp +=1
continousExp +=1
else:
if self.isContinousMwe(mwe):
continousExp +=1
return singleWordExp, continousExp
def isContinousMwe(self, mwe):
idxs = []
for token in mwe.tokens:
idxs.append(self.tokens.index(token))
#range = xrange(min(idxs), max(idxs))
mwe.isContinousExp = True
for i in range(min(idxs), max(idxs)): #range:
if i not in idxs:
mwe.isContinousExp = False
return mwe.isContinousExp
def recognizeInterleavingVMWEs(self):
if len(self.vMWEs) <= 1:
return 0
result = 0
for vmwe in self.vMWEs:
if vmwe.isEmbeded or vmwe.isInterleaving:
continue
for token in vmwe.tokens:
if len(token.parentMWEs) > 1:
for parent in token.parentMWEs:
if parent is not vmwe:
if parent.isEmbeded:
continue
parents = sorted(token.parentMWEs, key=lambda VMWE: VMWE.id)
if parent != parents[0]:
parent.isInterleaving = True
result += 1
return result
def getCorpusText(self, gold=True):
if gold:
mwes = self.vMWEs
else:
mwes = self.identifiedVMWEs
lines = ''
idx = 1
for token in self.tokens:
line = str(idx) + '\t' + token.text + '\t_\t'
idx += 1
for mwe in mwes:
if token in mwe.tokens:
if line.endswith('\t'):
line += str(mwe.id)
else:
line += ';' + str(mwe.id)
if line.endswith('\t'):
line += '_'
lines += line + '\n'
return lines
def getCorpusTextWithPlus(self):
goldMwes = self.vMWEs
predMwes = self.identifiedVMWEs
lines = ''
idx = 1
for token in self.tokens:
line = str(idx) + '\t' + token.text + '\t_\t'
idx += 1
for mwe in goldMwes:
if token in mwe.tokens:
if line.endswith('\t'):
line += '+'
break
if line.endswith('\t'):
line += '_\t'
else:
line += '\t'
for mwe in predMwes:
if token in mwe.tokens:
if line.endswith('\t'):
line += '+'
break
if line.endswith('\t'):
line += '_'
lines += line + '\n'
return lines
def printSummary(self):
vMWEText = ''
for vMWE in self.vMWEs:
vMWEText += str(vMWE) + '\n'
if len(self.identifiedVMWEs) > 0:
identifiedMWE = '### Identified MWEs: \n'
for mwe in self.identifiedVMWEs:
identifiedMWE += str(mwe) + '\n'
else:
identifiedMWE = ''
return '## Sentence No. ' + str(self.id) + ' - ' + self.sentid + '\n' + self.text + \
'\n### Existing MWEs: \n' + vMWEText + identifiedMWE
def __str__(self):
vMWEText = ''
for vMWE in self.vMWEs:
vMWEText += str(vMWE) + '\n'
if len(self.identifiedVMWEs) > 0:
identifiedMWE = '### Identified MWEs: \n'
for mwe in self.identifiedVMWEs:
identifiedMWE += str(mwe) + '\n\n'
else:
identifiedMWE = ''
featuresInfo = ''
result = ''
transition = self.initialTransition
idx = 0
while True:
type = ''
configuration = ''
if transition is not None:
if transition.type is not None:
type = transition.type.name
else:
type = ' '
configuration = str(transition.configuration)
if type == 'MERGE':
type = '**MERGE** '
if len(type) == 'SHIFT':
type = type + ' '
result += '\n\n' + str(
transition.id) + '- ' + type + ' ' + '>' + ' ' + configuration + '\n\n'
if transition.next is None:
break
transition = transition.next
if len(self.featuresInfo) == 2 and len(self.featuresInfo[1] )> 0:
sortedDic = sorted(self.featuresInfo[1][idx].items(), key=operator.itemgetter(0))
for item in sortedDic:
result += str(item[0]) + ': ' + str(item[1]) + ', '
idx += 1
else: #result += str(self.featuresInfo[1][idx]) + '\n\n'
break
# if len(self.featuresInfo) == 2:
# labels = self.featuresInfo[0]
# features = self.featuresInfo[1]
# for x in xrange(0, len(labels)):
# featuresInfo += str(x) + '- ' + str(labels[x]) + ' : ' + str(features[x]) + '\n\n'
return '## Sentence No. ' + str(self.id) + ' - ' + self.sentid + '\n' + self.text + \
'\n### Existing MWEs: \n' + vMWEText + identifiedMWE \
+ '\n' + result #str(self.initialTransition) + '\n### Features: \n' + featuresInfo
class Token:
"""
a class used to encapsulate all the information of a sentence tokens
"""
def __init__(self, position, txt, lemma='', posTag='', abstractPosTag='', morphologicalInfo=[], dependencyParent=-1,
dependencyLabel=''):
try: # added only for one case of line number 24.1 in the English data
self.position = int(position)
except ValueError: # added only for one case of line number 24.1 in he English data
self.position = float(position)
self.text = txt
# if lemma == '':
# self.lemma = Token.wordNetLemmatiser.lemmatize(txt)
# else:
self.lemma = lemma
self.abstractPosTag = abstractPosTag
self.posTag = posTag
self.morphologicalInfo = morphologicalInfo
self.dependencyParent = dependencyParent
self.dependencyLabel = dependencyLabel
self.parentMWEs = []
def setParent(self, vMWE):
self.parentMWEs.append(vMWE)
def __str__(self):
parentTxt = ''
if len(self.parentMWEs) != 0:
for parent in self.parentMWEs:
parentTxt += str(parent) + '\n'
return str(self.position) + ' : ' + self.text + ' : ' + self.posTag + '\n' + 'parent VMWEs:\t' + parentTxt
class VMWE:
"""
A class used to encapsulate the information of a verbal multi-word expression
"""
def __init__(self, id, token=None, type=None, isEmbeded=False, isInterleaving=False, isInTrainingCorpus=0):
self.id = int(id)
self.isInTrainingCorpus = isInTrainingCorpus
self.tokens = []
self.isSingleWordExp = False
self.isContinousExp = False
if token is not None:
self.tokens.append(token)
self.type = ''
if type is not None:
self.type = type
self.isEmbeded = isEmbeded
self.isInterleaving = isInterleaving
self.isVerbal = True
def getId(self):
return self.id
def addToken(self, token):
self.tokens.append(token)
@staticmethod
def isVerbalMwe(mwe):
isVerbal = False
for token in mwe.tokens:
if token.posTag.startswith('V'):
isVerbal = True
return isVerbal
def __str__(self):
tokensStr = ''
for token in self.tokens:
tokensStr += token.text + ' '
tokensStr = tokensStr.strip()
isInterleaving = ''
if self.isInterleaving:
isInterleaving = ', Interleaving '
isEmbedded = ''
if self.isEmbeded:
isEmbedded = ', Embedded'
#isContinousExp =''
#if self.isContinousExp:
#isContinousExp = 'Continous'
inTrainingCorpus = ''
if self.isInTrainingCorpus != 0:
inTrainingCorpus = ', ' + str(self.isInTrainingCorpus)
type = ''
if self.type != '':
type = '(' + self.type
if self.isInTrainingCorpus != 0:
type += ', ' + str(self.isInTrainingCorpus) + ')'
else:
type += ')'
return str(self.id) + '- ' + '**' + tokensStr + '** ' + type + isEmbedded + isInterleaving
def getString(self):
result = ''
for token in self.tokens:
result += token.text + ' '
return result[:-1]
def getLemmaString(self):
result = ''
for token in self.tokens:
if token.lemma.strip() != '':
result += token.lemma + ' '
else:
result += token.text + ' '
return result[:-1]
############################################################