-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconll.py
executable file
·275 lines (211 loc) · 9.84 KB
/
conll.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
#!/usr/local/bin/pypy
from itertools import izip
import sys
from subs import subssentiter
from upos_map import upos_map
import os
__author__ = 'husnusensoy'
NONE_STR = '_'
from string import Template
_CoNLLTemplate = Template(
'\t'.join(('$id', '$form', '$lemma', '$cpostag', '$postag', '$feats', '$head', '$deprel', '$phead', '$pdeprel')))
tconll = _CoNLLTemplate
class ConLLParsingException(Exception):
def __init__(self, line, ntoken):
self.line = line
self.ntoken = ntoken
def __str__(self):
return "%s has %d tokens (should be 10)" % (self.line, self.ntoken)
class CoNLLRecord:
def __init__(self, id, form, lemma, cpostag, postag, feats, head, deprel, phead, pdeprel):
self._id = None if id is None else int(id) # integer
self._form = form
self._lemma = lemma
self._cpostag = cpostag
self._postag = postag
self._feats = feats
self._head = None if head is None else int(head) # integer
self._deprel = deprel
self._phead = phead
self._pdeprel = pdeprel
def postag(self):
return self._cpostag if self._cpostag else self._postag
def setpostag(self, postag):
self._cpostag = postag
self._postag = postag
def setId(self, id):
self._id = id
def setHead(self, head):
self._head = head
@classmethod
def byline(cls, line):
t = [t if t != '_' else None for t in line.strip().split('\t')]
#print t
if len(t) != 10:
raise ConLLParsingException(line, len(t))
return cls(t[0], t[1], t[2], t[3], t[4], t[5], t[6], t[7], t[8], t[9])
def __repr__(self):
nvl = lambda x: x if x is not None else NONE_STR
return tconll.substitute(id=nvl(self._id), form=nvl(self._form),
lemma=nvl(self._lemma), cpostag=nvl(self._cpostag),
postag=nvl(self._postag), feats=nvl(self._feats),
head=nvl(self._head), deprel=nvl(self._deprel),
phead=nvl(self._phead), pdeprel=nvl(self._pdeprel))
class Reader():
def __init__(self, filename):
self.fd = open(filename, "r")
def __iter__(self):
return self
def next(self):
return self.fd.next()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.fd.close()
class ConLLSentence(list):
def sentence(self):
return [t._form for t in self]
class ConLLReader(Reader):
def __init__(self, filename):
Reader.__init__(self, filename)
def next(self):
tokens = ConLLSentence()
while True:
row = Reader.next(self)
if row == '\n':
return tokens
else:
tokens.append(CoNLLRecord.byline(row))
def open2(filename):
"""
:param filename: Name of the file to be processed
:return: Proper stream for the file type
"""
return ConLLReader(filename)
def ConLLdiriter(directory):
for path, subdirs, files in os.walk(directory):
for f in files:
yield os.path.join(path, f), os.path.basename(path)
def ConLLiter(directory, extension):
for f, section in ConLLdiriter(directory):
if f.endswith(extension):
with open2(f) as fp:
for sentence in fp:
yield sentence, section
def dump_corpus(file, corpus):
with open(file, "w") as fp:
for c in corpus:
for word in c:
print >> fp, str(word)
print >> fp
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(
description='CoNLL file transformer')
parser.add_argument('--file', help='CoNLL files to read')
parser.add_argument('--directory', help='CoNLL directory to read')
parser.add_argument('--extension', default='.dp', help='CoNLL file extension to read')
parser.add_argument('--section', type=str, help="WSJ sections to be filtered out")
parser.add_argument('--tagmode', type=str, default='nochange',
choices=['nochange', 'tagfile', 'onetagperword', 'remove', 'random'],
help="Tag manipulation operation on corpus")
parser.add_argument('--tagfile', type=str, default=None,
help="Tag file to be used. Only valid when used with --tagmode tagfile|random")
parser.add_argument('--ambigious', action='store_true', default=False, help="Threat tags as ambigious by not using a dictionary")
parser.add_argument('--formmode', type=str, default='nochange',
choices=['nochange', 'formfile', 'remove'],
help="Form manipulation operation on corpus")
parser.add_argument('--formfile', type=str, default=None,
help="Form file to be used. Only valid when used with --formmode formfile")
parser.add_argument('--subsmode', type=str, default='nochange',
choices=['nochange', 'best'],
help="Form manipulation operation on corpus")
parser.add_argument('--subsfile', type=str, default="/Users/husnusensoy/uparse/data/upos/wsj.sub.gz",
help="Substitution file to be used. Only valid when used with --subsmode formfile")
args = parser.parse_args()
if args.file and args.directory:
raise Exception("Choose to read from a file or a corpus directory")
if args.tagmode not in ['tagfile', 'random'] and args.tagfile:
raise Exception("--tagfile option can only be used with --tagmode tagfile|random options")
if args.tagmode == 'random' and not args.tagfile:
raise Exception("--tagmode random requires tagfile to be set")
if args.tagfile:
tags = upos_map(args.tagfile, '../data/upos/wsj.words.gz')
if args.tagmode == 'random':
tagset = [t for t in set(tags.values())]
import random
if args.formfile:
forms = upos_map(args.formfile, '../data/upos/wsj.words.gz')
if args.file:
# TODO: Fix this part.
with open2(args.file) as fp:
for record in fp:
print record
elif args.directory:
if not args.section:
sections = ["%02d" % s for s in range(0, 25)]
elif "-" in args.section:
begin_section, end_section = [int(s) for s in args.section.split("-")]
sections = ["%02d" % s for s in range(begin_section, end_section + 1)]
elif "," in args.section:
sections = ["%02d" % int(s) for s in args.section]
else:
sections = ["%02d" % int(args.section)]
import os
if args.subsmode != "nochange":
for subs, (conll, section) in izip(subssentiter(file=args.subsfile),
ConLLiter(directory=args.directory, extension=args.extension)):
if section in sections:
if not any([c == s for c, s in zip(conll.sentence(), subs.orginal())]):
sys.stderr.write(repr(subs))
sys.stderr.write("\n")
sys.stderr.write(repr(conll))
sys.stderr.write("\n")
break
else:
for word, substitution in zip(conll, subs):
if args.subsmode == "best":
word._form = substitution.best()
if args.tagmode == 'nochange':
word.setpostag(word.postag())
elif args.tagmode == 'tagfile':
word.setpostag(tags[word._form])
elif args.tagmode == 'onetagperword':
raise Exception("Not implemented yet")
elif args.tagmode == 'remove':
word.setpostag(None)
elif args.tagmode == 'random':
word.setpostag(random.choice(tagset))
if args.formmode == 'nochange':
pass
elif args.formmode == 'formfile':
word._form = forms[word._form]
elif args.formmode == 'remove':
word._form = None
sys.stdout.write(str(word))
sys.stdout.write("\n")
sys.stdout.write("\n")
else:
for conll, section in ConLLiter(directory=args.directory, extension=args.extenstion):
if section in sections:
for sentence in conll:
for word in sentence:
if args.tagmode == 'nochange':
word.setpostag(word.postag())
elif args.tagmode == 'tagfile':
word.setpostag(tags[word._form])
elif args.tagmode == 'onetagperword':
raise Exception("Not implemented yet")
elif args.tagmode == 'remove':
word.setpostag(None)
elif args.tagmode == 'random':
word.setpostag(random.choice(tagset))
if args.formmode == 'nochange':
pass
elif args.formmode == 'formfile':
word._form = forms[word._form]
elif args.formmode == 'remove':
word._form = None
sys.stdout.write(str(word))
sys.stdout.write("\n")
sys.stdout.write("\n")