forked from proycon/pynlpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtagger.py
executable file
·361 lines (322 loc) · 13.2 KB
/
tagger.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
#! /usr/bin/env python
# -*- coding: utf8 -*-
###############################################################
# PyNLPl - FreeLing Library
# by Maarten van Gompel (proycon)
# http://ilk.uvt.nl/~mvgompel
# Radboud University Nijmegen
#
# Licensed under GPLv3
#
# Generic Tagger interface for PoS-tagging and lemmatisation,
# offers an interface to various software
#
###############################################################
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division
from __future__ import absolute_import
from pynlpl.common import u
import sys
if sys.version < '3':
from codecs import getwriter
stderr = getwriter('utf-8')(sys.stderr)
stdout = getwriter('utf-8')(sys.stdout)
else:
stderr = sys.stderr
stdout = sys.stdout
import io
import codecs
import json
import getopt
import subprocess
class Tagger(object):
def __init__(self, *args):
global WSDDIR
self.tagger = None
self.mode = args[0]
if args[0] == "file":
if len(args) != 2:
raise Exception("Syntax: file:[filename]")
self.tagger = codecs.open(args[1],'r','utf-8')
elif args[0] == "frog":
if len(args) != 3:
raise Exception("Syntax: frog:[host]:[port]")
from pynlpl.clients.frogclient import FrogClient
port = int(args[2])
self.tagger = FrogClient(args[1],port)
elif args[0] == "freeling":
if len(args) != 3:
raise Exception("Syntax: freeling:[host]:[port]")
from pynlpl.clients.freeling import FreeLingClient
host = args[1]
port = int(args[2])
self.tagger = FreeLingClient(host,port)
elif args[0] == "corenlp":
if len(args) != 1:
raise Exception("Syntax: corenlp")
import corenlp
print("Initialising Stanford Core NLP",file=stderr)
self.tagger = corenlp.StanfordCoreNLP()
elif args[0] == 'treetagger':
if not len(args) == 2:
raise Exception("Syntax: treetagger:[treetagger-bin]")
self.tagger = args[1]
elif args[0] == "durmlex":
if not len(args) == 2:
raise Exception("Syntax: durmlex:[filename]")
print("Reading durm lexicon: ", args[1],file=stderr)
self.mode = "lookup"
self.tagger = {}
f = codecs.open(args[1],'r','utf-8')
for line in f:
fields = line.split('\t')
wordform = fields[0].lower()
lemma = fields[4].split('.')[0]
self.tagger[wordform] = (lemma, 'n')
f.close()
print("Loaded ", len(self.tagger), " wordforms",file=stderr)
elif args[0] == "oldlex":
if not len(args) == 2:
raise Exception("Syntax: oldlex:[filename]")
print("Reading OLDLexique: ", args[1],file=stderr)
self.mode = "lookup"
self.tagger = {}
f = codecs.open(args[1],'r','utf-8')
for line in f:
fields = line.split('\t')
wordform = fields[0].lower()
lemma = fields[1]
if lemma == '=':
lemma == fields[0]
pos = fields[2][0].lower()
self.tagger[wordform] = (lemma, pos)
print("Loaded ", len(self.tagger), " wordforms",file=stderr)
f.close()
else:
raise Exception("Invalid mode: " + args[0])
def __iter__(self):
if self.mode != 'file':
raise Exception("Iteration only possible in file mode")
line = self.tagger.next()
newwords = []
postags = []
lemmas = []
for item in line:
word,lemma,pos = item.split('|')
newwords.append(word)
postags.append(pos)
lemmas.append(lemma)
yield newwords, postags, lemmas
def reset(self):
if self.mode == 'file':
self.tagger.seek(0)
def process(self, words, debug=False):
if self.mode == 'file':
line = self.tagger.next()
newwords = []
postags = []
lemmas = []
for item in line.split(' '):
if item.strip():
try:
word,lemma,pos = item.split('|')
except:
raise Exception("Unable to parse word|lemma|pos in " + item)
newwords.append(word)
postags.append(pos)
lemmas.append(lemma)
return newwords, postags, lemmas
elif self.mode == "frog":
newwords = []
postags = []
lemmas = []
for fields in self.tagger.process(' '.join(words)):
word,lemma,morph,pos = fields[:4]
newwords.append(word)
postags.append(pos)
lemmas.append(lemma)
return newwords, postags, lemmas
elif self.mode == "freeling":
postags = []
lemmas = []
for fields in self.tagger.process(words, debug):
word, lemma,pos = fields[:3]
postags.append(pos)
lemmas.append(lemma)
return words, postags, lemmas
elif self.mode == "corenlp":
data = json.loads(self.tagger.parse(" ".join(words)))
words = []
postags = []
lemmas = []
for sentence in data['sentences']:
for word, worddata in sentence['words']:
words.append(word)
lemmas.append(worddata['Lemma'])
postags.append(worddata['PartOfSpeech'])
return words, postags, lemmas
elif self.mode == 'lookup':
postags = []
lemmas = []
for word in words:
try:
lemma, pos = self.tagger[word.lower()]
lemmas.append(lemma)
postags.append(pos)
except KeyError:
lemmas.append(word)
postags.append('?')
return words, postags, lemmas
elif self.mode == 'treetagger':
s = " ".join(words)
s = u(s)
p = subprocess.Popen([self.tagger], shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = p.communicate(s.encode('utf-8'))
newwords = []
postags = []
lemmas = []
for line in out.split('\n'):
line = line.strip()
if line:
fields = line.split('\t')
newwords.append( unicode(fields[0],'utf-8') )
postags.append( unicode(fields[1],'utf-8') )
lemmas.append( unicode(fields[2],'utf-8') )
if p.returncode != 0:
print(err,file=stderr)
raise OSError('TreeTagger failed')
return newwords, postags, lemmas
else:
raise Exception("Unknown mode")
def treetagger_tag(self, f_in, f_out,oneperline=False, debug=False):
def flush(sentences):
if sentences:
print("Processing " + str(len(sentences)) + " lines",file=stderr)
for sentence in sentences:
out = ""
p = subprocess.Popen([self.tagger], shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(results, err) = p.communicate("\n".join(sentences).encode('utf-8'))
for line in results.split('\n'):
line = line.strip()
if line:
fields = line.split('\t')
word = fields[0]
pos = fields[1]
lemma = fields[2]
if oneperline:
if out: out += "\n"
out += word + "\t" + lemma + "\t" + pos
else:
if out: out += " "
if '|' in word:
word = word.replace('|','_')
if '|' in lemma:
lemma = lemma.replace('|','_')
if '|' in pos:
pos = pos.replace('|','_')
out += word + "|" + lemma + "|" + pos
if pos[0] == '$':
out = u(out)
f_out.write(out + "\n")
if oneperline: f_out.write("\n")
out = ""
if out:
out = u(out)
f_out.write(out + "\n")
if oneperline: f_out.write("\n")
#buffered tagging
sentences = []
linenum = 0
for line in f_in:
linenum += 1
print(" Buffering input @" + str(linenum),file=stderr)
line = line.strip()
if not line or ('.' in line[:-1] or '?' in line[:-1] or '!' in line[:-1]) or (line[-1] != '.' and line[-1] != '?' and line[-1] != '!'):
flush(sentences)
sentences = []
if not line.strip():
f_out.write("\n")
if oneperline: f_out.write("\n")
sentences.append(line)
flush(sentences)
def tag(self, f_in, f_out,oneperline=False, debug=False):
if self.mode == 'treetagger':
self.treetagger_tag(f_in, f_out,oneperline=False, debug=False)
else:
linenum = 0
for line in f_in:
linenum += 1
print(" Tagger input @" + str(linenum),file=stderr)
if line.strip():
words = line.strip().split(' ')
words, postags, lemmas = self.process(words, debug)
out = ""
for word, pos, lemma in zip(words,postags, lemmas):
if word is None: word = ""
if lemma is None: lemma = "?"
if pos is None: pos = "?"
if oneperline:
if out: out += "\n"
out += word + "\t" + lemma + "\t" + pos
else:
if out: out += " "
if '|' in word:
word = word.replace('|','_')
if '|' in lemma:
lemma = lemma.replace('|','_')
if '|' in pos:
pos = pos.replace('|','_')
out += word + "|" + lemma + "|" + pos
if not isinstance(out, unicode):
out = unicode(out, 'utf-8')
f_out.write(out + "\n")
if oneperline:
f_out.write("\n")
else:
f_out.write("\n")
def usage():
print("tagger.py -c [conf] -f [input-filename] -o [output-filename]",file=stderr)
if __name__ == "__main__":
try:
opts, args = getopt.getopt(sys.argv[1:], "f:c:o:D")
except getopt.GetoptError as err:
# print help information and exit:
print(str(err),file=stderr)
usage()
sys.exit(2)
taggerconf = None
filename = None
outfilename = None
oneperline = False
debug = False
for o, a in opts:
if o == "-c":
taggerconf = a
elif o == "-f":
filename = a
elif o == '-o':
outfilename =a
elif o == '-l':
oneperline = True
elif o == '-D':
debug = True
else:
print >>sys.stderr,"Unknown option: ", o
sys.exit(2)
if not taggerconf:
print("ERROR: Specify a tagger configuration with -c",file=stderr)
sys.exit(2)
if not filename:
print("ERROR: Specify a filename with -f",file=stderr)
sys.exit(2)
if outfilename:
f_out = io.open(outfilename,'w',encoding='utf-8')
else:
f_out = stdout;
f_in = io.open(filename,'r',encoding='utf-8')
tagger = Tagger(*taggerconf.split(':'))
tagger.tag(f_in, f_out, oneperline, debug)
f_in.close()
if outfilename:
f_out.close()