-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathtreetagger.py
390 lines (318 loc) · 15 KB
/
treetagger.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
# -*- coding: utf-8 -*-
# Natural Language Toolkit: Interface to the TreeTagger POS-tagger
#
# Copyright (C) Mirko Otto
# Author: Mirko Otto <[email protected]>
"""
A Python module for interfacing with the Treetagger by Helmut Schmid.
"""
import os, tempfile, fnmatch, re
from subprocess import Popen, PIPE
from nltk.internals import find_binary, find_file
from nltk.tag.api import TaggerI
from nltk.chunk.api import ChunkParserI
from nltk.tree import Tree
from sys import platform as _platform
import sys
_treetagger_url = 'http://www.cis.uni-muenchen.de/~schmid/tools/TreeTagger/'
def files(path, pattern):
for file in os.listdir(path):
if (os.path.isfile(os.path.join(path, file)) and fnmatch.fnmatch(file, pattern)):
yield file
class TreeTagger(TaggerI):
r"""
A class for pos tagging with TreeTagger. The default encoding used by TreeTagger is utf-8. The input is the paths to:
- a language trained on training data
- (optionally) the path to the TreeTagger binary
This class communicates with the TreeTagger binary via pipes.
Example:
.. doctest::
:options: +SKIP
>>> from treetagger import TreeTagger
>>> tt = TreeTagger(language='english')
>>> tt.tag('What is the airspeed of an unladen swallow?')
[['What', 'WP', 'what'], ['is', 'VBZ', 'be'], ['the', 'DT', 'the'], ['airspeed', 'NN', 'airspeed'], ['of', 'IN', 'of'], ['an', 'DT', 'an'], ['unladen', 'JJ', '<unknown>'], ['swallow', 'NN', 'swallow'], ['?', 'SENT', '?']]
.. doctest::
:options: +SKIP
>>> from treetagger import TreeTagger
>>> tt = TreeTagger(language='german')
>>> tt.tag('Das Haus hat einen großen hübschen Garten.')
[['Das', 'ART', 'die'], ['Haus', 'NN', 'Haus'], ['hat', 'VAFIN', 'haben'], ['einen', 'ART', 'eine'], ['großen', 'ADJA', 'groß'], ['hübschen', 'ADJA', 'hübsch'], ['Garten', 'NN', 'Garten'], ['.', '$.', '.']]
"""
def __init__(self, path_to_treetagger=None, language='english',
verbose=False, abbreviation_list=None):
"""
Initialize the TreeTagger.
:param language: Default language is english.
The encoding used by the model. Unicode tokens
passed to the tag() method are converted to
this charset when they are sent to TreeTagger.
The default is utf-8.
This parameter is ignored for str tokens, which are sent as-is.
The caller must ensure that tokens are encoded in the right charset.
"""
if path_to_treetagger:
self._path_to_treetagger = path_to_treetagger
else:
self._path_to_treetagger = None
treetagger_paths = ['.']
if 'TREETAGGER_HOME' in os.environ:
if _platform.startswith('win'):
tt_path = os.path.normpath(os.path.join(os.environ['TREETAGGER_HOME'], 'bin'))
else:
tt_path = os.path.normpath(os.path.join(os.environ['TREETAGGER_HOME'], 'cmd'))
treetagger_paths.append(tt_path)
elif self._path_to_treetagger:
if _platform.startswith('win'):
tt_path = os.path.normpath(os.path.join(self._path_to_treetagger, 'bin'))
else:
tt_path = os.path.normpath(os.path.join(self._path_to_treetagger, 'cmd'))
treetagger_paths.append(tt_path)
else:
raise LookupError('Set \'TREETAGGER_HOME\' or use path_to_treetagger!')
treetagger_paths = list(map(os.path.expanduser, treetagger_paths))
self._abbr_list = abbreviation_list
if language in self.get_installed_lang():
if _platform.startswith('win'):
treetagger_bin_name = 'tag-' + language + '.bat'
else:
treetagger_bin_name = 'tree-tagger-' + language
else:
raise LookupError('Language not installed!')
try:
self._treetagger_bin = find_binary(
treetagger_bin_name,
searchpath=treetagger_paths,
url=_treetagger_url,
verbose=verbose)
except LookupError:
print('NLTK was unable to find the TreeTagger bin!')
def get_treetagger_path(self):
if 'TREETAGGER_HOME' in os.environ:
print('Environment variable \'TREETAGGER_HOME\' is ' + os.environ['TREETAGGER_HOME'])
else:
print('Environment variable \'TREETAGGER_HOME\' not set')
if self._path_to_treetagger:
print('Path to TreeTagger is ' + self._path_to_treetagger)
else:
print('Path to TreeTagger not set')
def get_installed_lang(self):
if 'TREETAGGER_HOME' in os.environ:
lang_path = os.path.normpath(os.path.join(os.environ['TREETAGGER_HOME'], 'lib'))
return [file[:-4] for file in files(lang_path, "*.par") if not file.endswith("chunker.par")]
elif self._path_to_treetagger:
lang_path = os.path.normpath(os.path.join(self._path_to_treetagger, 'lib'))
return [file[:-4] for file in files(lang_path, "*.par") if not file.endswith("chunker.par")]
else:
return []
def tag(self, sentences):
"""Tags a single sentence: a list of words.
The tokens should not contain any newline characters.
"""
# Write the actual sentences to the temporary input file
if isinstance(sentences, list):
_input = '\n'.join((x for x in sentences))
else:
_input = sentences
outfile = tempfile.NamedTemporaryFile(mode="w+", encoding="utf8", delete=False)
with tempfile.NamedTemporaryFile(mode="w+", encoding="utf8", delete=False) as infile:
infile.write(_input)
infile.flush()
# Run the tagger and get the output
if(self._abbr_list is None):
p = Popen([self._treetagger_bin, infile.name],
shell=False, stdin=PIPE, stdout=outfile, stderr=PIPE)
elif(self._abbr_list is not None):
p = Popen([self._treetagger_bin,"-a",self._abbr_list, infile.name],
shell=False, stdin=PIPE, stdout=outfile, stderr=PIPE)
(stdout, stderr) = p.communicate()
# Check the return code.
if p.returncode != 0:
print(stderr)
raise OSError('TreeTagger command failed!')
infile.close()
os.unlink(infile.name)
outfile.seek(0)
tagged_sentences = []
for line in outfile:
tagged_word = line.rstrip('\n')
tagged_word_split = tagged_word.split('\t')
tagged_sentences.append(tagged_word_split)
outfile.close()
os.unlink(outfile.name)
return tagged_sentences
class TreeTaggerChunker(ChunkParserI):
r"""
A class for chunking with TreeTagger Chunker. The default encoding used by TreeTagger is utf-8. The input is the paths to:
- a language trained on training data
- (optionally) the path to the TreeTagger binary
This class communicates with the TreeTagger Chunker binary via pipes.
Example:
.. doctest::
:options: +SKIP
>>> from treetagger import TreeTaggerChunker
>>> tt = TreeTaggerChunker(language='english')
>>> tt.parse('What is the airspeed of an unladen swallow?')
[['<NC>'], ['What', 'WP', 'what'], ['</NC>'], ['<VC>'], ['is', 'VBZ', 'be'], ['</VC>'], ['<NC>'], ['the', 'DT', 'the'], ['airspeed', 'NN', 'airspeed'], ['</NC>'], ['<PC>'], ['of', 'IN', 'of'], ['<NC>'], ['an', 'DT', 'an'], ['unladen', 'JJ', '<unknown>'], ['swallow', 'NN', 'swallow'], ['</NC>'], ['</PC>'], ['?', 'SENT', '?']]
.. doctest::
:options: +SKIP
>>> from treetagger import TreeTaggerChunker
>>> tt = TreeTaggerChunker(language='english')
>>> tt.parse_to_tree('What is the airspeed of an unladen swallow?')
Tree('S', [Tree('NC', [Tree('What', ['WP'])]), Tree('VC', [Tree('is', ['VBZ'])]), Tree('NC', [Tree('the', ['DT']), Tree('airspeed', ['NN'])]), Tree('PC', [Tree('of', ['IN']), Tree('NC', [Tree('an', ['DT']), Tree('unladen', ['JJ']), Tree('swallow', ['NN'])])]), Tree('?', ['SENT'])])
.. doctest::
:options: +SKIP
>>> from nltk.tree import Tree
>>> from treetagger import TreeTaggerChunker
>>> tt = TreeTaggerChunker(language='english')
>>> res = tt.parse_to_tree('What is the airspeed of an unladen swallow?')
>>> print(res)
(S
(NC (What WP))
(VC (is VBZ))
(NC (the DT) (airspeed NN))
(PC (of IN) (NC (an DT) (unladen JJ) (swallow NN)))
(? SENT))
"""
def __init__(self, path_to_treetagger=None, language='english',
verbose=False, abbreviation_list=None):
"""
Initialize the TreeTaggerChunker.
:param language: Default language is english.
The encoding used by the model. Unicode tokens
passed to the parse() and parse_to_tree() methods are converted to
this charset when they are sent to TreeTaggerChunker.
The default is utf-8.
This parameter is ignored for str tokens, which are sent as-is.
The caller must ensure that tokens are encoded in the right charset.
"""
if path_to_treetagger:
self._path_to_treetagger = path_to_treetagger
else:
self._path_to_treetagger = None
treetagger_paths = ['.']
if 'TREETAGGER_HOME' in os.environ:
if _platform.startswith('win'):
tt_path = os.path.normpath(os.path.join(os.environ['TREETAGGER_HOME'], 'bin'))
else:
tt_path = os.path.normpath(os.path.join(os.environ['TREETAGGER_HOME'], 'cmd'))
treetagger_paths.append(tt_path)
elif self._path_to_treetagger:
if _platform.startswith('win'):
tt_path = os.path.normpath(os.path.join(self._path_to_treetagger, 'bin'))
else:
tt_path = os.path.normpath(os.path.join(self._path_to_treetagger, 'cmd'))
treetagger_paths.append(tt_path)
else:
raise LookupError('Set \'TREETAGGER_HOME\' or use path_to_treetagger!')
treetagger_paths = list(map(os.path.expanduser, treetagger_paths))
self._abbr_list = abbreviation_list
if language in self.get_installed_lang():
if _platform.startswith('win'):
treetagger_chunker_bin_name = 'chunk-' + language + '.bat'
else:
treetagger_chunker_bin_name = 'tagger-chunker-' + language
else:
raise LookupError('Language not installed!')
try:
self._treetagger_chunker_bin = find_binary(
treetagger_chunker_bin_name,
searchpath=treetagger_paths,
url=_treetagger_url,
verbose=verbose)
except LookupError:
print('NLTK was unable to find the TreeTagger Chunker bin!')
def get_treetagger_path(self):
if 'TREETAGGER_HOME' in os.environ:
print('Environment variable \'TREETAGGER_HOME\' is ' + os.environ['TREETAGGER_HOME'])
else:
print('Environment variable \'TREETAGGER_HOME\' not set')
if self._path_to_treetagger:
print('Path to TreeTagger is ' + self._path_to_treetagger)
else:
print('Path to TreeTagger not set')
def get_installed_lang(self):
if 'TREETAGGER_HOME' in os.environ:
lang_path = os.path.normpath(os.path.join(os.environ['TREETAGGER_HOME'], 'lib'))
lang_files = [file[:-4] for file in files(lang_path, "*.par")]
lang_chunk_files = [file[:-12] for file in files(lang_path, "*-chunker.par")]
return [item for item in lang_chunk_files if item in lang_files]
elif self._path_to_treetagger:
lang_path = os.path.normpath(os.path.join(self._path_to_treetagger, 'lib'))
lang_files = [file[:-4] for file in files(lang_path, "*.par")]
lang_chunk_files = [file[:-12] for file in files(lang_path, "*-chunker.par")]
return [item for item in lang_chunk_files if item in lang_files]
else:
return []
def parse(self, tokens):
"""Tag and chunk a single sentence: a list of words.
The tokens should not contain any newline characters.
"""
# Write the actual sentences to the temporary input file
if isinstance(tokens, list):
_input = '\n'.join((x for x in tokens))
else:
_input = tokens
outfile = tempfile.NamedTemporaryFile(mode="w+", encoding="utf8", delete=False)
with tempfile.NamedTemporaryFile(mode="w+", encoding="utf8", delete=False) as infile:
infile.write(_input)
infile.flush()
# Run the tagger and get the output
if(self._abbr_list is None):
p = Popen([self._treetagger_chunker_bin, infile.name],
shell=False, stdin=PIPE, stdout=outfile, stderr=PIPE)
elif(self._abbr_list is not None):
p = Popen([self._treetagger_chunker_bin,"-a",self._abbr_list, infile.name],
shell=False, stdin=PIPE, stdout=outfile, stderr=PIPE)
(stdout, stderr) = p.communicate()
# Check the return code.
if p.returncode != 0:
print(stderr)
raise OSError('TreeTaggerChunker command failed!')
infile.close()
os.unlink(infile.name)
# Output the tagged ans chunked sentences
outfile.seek(0)
tagged_chunked_sentences = []
for line in outfile:
tagged_word = line.rstrip('\n')
tagged_word_split = tagged_word.split('\t')
tagged_chunked_sentences.append(tagged_word_split)
outfile.close()
os.unlink(outfile.name)
return tagged_chunked_sentences
def parse_to_tree(self, tokens):
tc_sentences = self.parse(tokens)
resar = []
res = ''
for idx, item in enumerate(tc_sentences):
if len(item) == 1:
erg = re.sub('</[a-zA-Z]*>',')',item[0])
if erg == ')':
res += erg
else:
erg1 = re.sub('<',' (',item[0])
erg2 = re.sub('>','',erg1)
res += erg2
if len(item) == 3:
res += ' ('+item[0]+' '+item[1] +')'
if item[1] == 'SENT' or item[1] == '$.' or item[1] == 'FS':
res = '(S '+res+')'
resar.append(res)
res = ''
if len(tc_sentences)==idx+1 and len(res) > 1 and res[0:2] != '(S':
res = '(S '+res+')'
resar.append(res)
res = ''
if len(resar) > 1:
erg = '(ROOT '+' '.join(resar)+')'
else:
erg = resar[0]
try:
return Tree.fromstring(erg)
except ValueError:
print('Something goes wrong. Please check the raw data:\n')
print(erg)
if __name__ == "__main__":
import doctest
doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE)