Repository navigation
Expand file tree
/
Copy pathconll_reader.py
More file actions
297 lines (230 loc) · 8.43 KB
/
Copy pathconll_reader.py
File metadata and controls
297 lines (230 loc) · 8.43 KB
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
from __future__ import unicode_literals
import codecs
from collections import namedtuple
import sys
import six
from typing import Union, Optional, List
from namedlist import namedlist
class CoNLLNode(object):
@classmethod
def from_line(cls, line):
fields = line.split()
try:
return cls(*fields)
except TypeError:
print("Line: " + line)
raise
def to_line(self, sep='\t'):
return sep.join(six.text_type(i) for i in self)
class CoNLLUNode(CoNLLNode,
namedlist("_", ["id_", "form", "lemma", "cpostag", "postag", "feats",
"head", "deprel", "deps", "misc"])):
@classmethod
def from_line(cls, line):
fields = line.split()
if len(fields) < len(cls._fields):
fields.extend(["_" for _ in range(len(cls._fields) - len(fields))])
try:
return cls(*fields)
except TypeError:
print("Line: " + line)
raise
class CoNLL09Node(namedtuple("_", ["id_", "form", "lemma", "plemma", "postag", "ppostag", "feats",
"pfeats", "head", "phead", "deprel", "pdeprel", "fillpred", "pred", "arg"])):
@classmethod
def from_line(cls, line):
fields = line.split()
if len(fields) < 13:
raise AttributeError("too few fields: {}".format(line))
if len(fields) < 14:
fields.extend(["-"] * (14 - len(fields)))
cols = fields[0:14]
cols.append(fields[14:])
return cls(*cols)
def to_line(self, sep="\t"):
return sep.join(list(self[0:-1]) + self[-1])
class CoNLL08Node(namedtuple("_", ["id_", "form", "lemma", "postag", "ppostag",
"split_form", "split_lemma", "ppostags",
"head", "deprel", "pred", "arg"])):
@classmethod
def from_line(cls, line):
fields = line.split()
cols = fields[0:11]
cols.append(fields[11:])
try:
return cls(*cols)
except TypeError:
print("Line: " + line)
raise
def to_line(self, sep="\t"):
return sep.join(six.text_type(i) for i in list(self[0:-1]) + self[-1])
def __hash__(self):
return int(self.id_)
class SDPNode(namedlist("_", ["id_", "form", "lemma", "postag",
"top", "pred", "sense", "arg"])):
@classmethod
def from_line(cls, line):
fields = line.split()
cols = fields[0:7]
cols.append(fields[7:])
try:
return cls(*cols)
except TypeError:
print("Line: " + line)
raise
def to_line(self, sep="\t"):
return sep.join(six.text_type(i) for i in list(self[0:-1]) + self[-1])
def __hash__(self):
return int(self.id_)
class OldSDPNode(namedlist("_", ["id_", "form", "lemma", "postag",
"top", "pred", "arg"])):
@classmethod
def from_line(cls, line):
fields = line.split()
cols = fields[0:6]
if len(cols) == 4:
cols.extend(["-", "-"])
cols.append(fields[6:])
try:
return cls(*cols)
except TypeError:
print("Line: " + line)
raise
def to_line(self, sep="\t"):
return sep.join(six.text_type(i) for i in list(self[0:-1]) + self[-1])
def __hash__(self):
return int(self.id_)
class TTNode(namedtuple("_", ["start", "end", "form", "lemma", "postag"])):
@classmethod
def from_line(cls, line):
fields = line.split()
return cls(*fields)
def to_line(self):
raise NotImplementedError
class SimpleNode(namedtuple("_", ["word", "postag", "head", "deprel"])):
@classmethod
def from_line(cls, line):
fields = line.split()
return cls(*fields)
def to_line(self, sep='\t'):
return sep.join(six.text_type(i) for i in self)
class CoNLL06Node(CoNLLNode,
namedtuple("_", ["id_", "form", "lemma", "cpostag", "postag",
"feats", "head", "deprel", "phead", "pdeprel"])):
pass
class BaseConLLSentence(list):
NodeType = None
def __init__(self, *args):
super(BaseConLLSentence, self).__init__(*args)
self.comment = None # type: Optional[Union[str, List[str]]]
@classmethod
def get_sentence(cls, file_object):
sentence = cls()
start = True
for i in file_object:
line = i.strip()
if not line:
break
if start:
if line.startswith("#") and line[1] != "\t":
comment = line[1:].strip()
if sentence.comment is None:
sentence.comment = comment
elif isinstance(sentence.comment, six.string_types):
sentence.comment = [sentence.comment, comment]
else:
assert isinstance(sentence.comment, list)
sentence.comment.append(comment)
continue
sentence.append(cls.NodeType.from_line(line))
start = False
return sentence
@classmethod
def get_all_sentences(cls, file_object, limit=None):
result = []
while True:
sentence = cls.get_sentence(file_object)
if not sentence:
break
result.append(sentence)
if limit is not None and len(result) == limit:
break
return result
def get_comment_line(self):
if self.comment is None:
return ""
elif isinstance(self.comment, six.string_types):
return "# {}".format(self.comment.strip()).replace("\n", " ") + "\n"
else:
assert isinstance(self.comment, (list, tuple))
return "\n".join("# {}".format(i.strip()).replace("\n", " ") for i in self.comment) + "\n"
def to_string(self, sep="\t"):
return self.get_comment_line() + "\n".join(i.to_line(sep) for i in self) + "\n\n"
class CoNLL09Sentence(BaseConLLSentence):
NodeType = CoNLL09Node
class CoNLLUSentence(BaseConLLSentence):
NodeType = CoNLLUNode
class TTSentence(BaseConLLSentence):
NodeType = TTNode
class CoNLL08Sentence(BaseConLLSentence):
NodeType = CoNLL08Node
class SimpleSentence(BaseConLLSentence):
NodeType = SimpleNode
class CoNLL06Sentence(BaseConLLSentence):
NodeType = CoNLL06Node
class SDPSentence(BaseConLLSentence):
NodeType = SDPNode
@classmethod
def get_sentence(cls, file_object):
sentence = cls()
for i in file_object:
line = i.strip()
if line.startswith("#"):
sentence.comment = line[1:].strip()
continue
if line == "null":
continue
if not line:
break
sentence.append(cls.NodeType.from_line(line))
return sentence
class OldSDPSentence(BaseConLLSentence):
NodeType = OldSDPNode
@classmethod
def get_sentence(cls, file_object):
sentence = cls()
for i in file_object:
line = i.strip()
if line.startswith("#"):
sentence.comment = line[1:].strip()
continue
if line == "null":
continue
if not line:
break
sentence.append(cls.NodeType.from_line(line))
return sentence
def sent_convert(sent):
result = CoNLLUSentence()
for i in sent:
# noinspection PyArgumentList
result.append(
CoNLLUNode(i.id_, i.form, i.lemma, '_', i.postag, i.feats, i.head, i.deprel, '_', '_', []))
return result
def main():
with codecs.open(sys.argv[1]) as f:
sents = CoNLL09Sentence.get_all_sentences(f)
with codecs.open(sys.argv[2], "w") as f_w:
for i in sents:
f_w.write(sent_convert(i).to_string())
def make_converter(SourceSentence, TargetSentence, node_converter, sep="\t"):
def converter(source_file, output_file):
with codecs.open(source_file) as f:
sents = SourceSentence.get_all_sentences(f)
with codecs.open(output_file, "w") as f_w:
for source_sent in sents:
converted_sent = TargetSentence(node_converter(i) for i in source_sent)
f_w.write(converted_sent.to_string(sep))
return converter
if __name__ == '__main__':
main()