-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathnltktags.py
225 lines (176 loc) · 5.29 KB
/
nltktags.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Handling NLTK to generate tags
#
# 2013-10-25 Created by Pascal Pfiffner
#
import os
import logging
import codecs
import inspect
import nltk
import operator
from nlp import NLPProcessing, list_to_sentences
class NLTKTags (NLPProcessing):
""" Aggregate handling tasks specifically for NLTK. """
def __init__(self):
super(NLTKTags, self).__init__()
self.name = 'nltk-tags'
@property
def _in_dir(self):
return os.path.join(self.root, 'nltk-tags-in')
@property
def _out_dir(self):
return os.path.join(self.root, 'nltk-tags-out')
def _create_directories_if_needed(self):
in_dir = self._in_dir
out_dir = self._out_dir
if not os.path.exists(in_dir):
os.mkdir(in_dir)
if not os.path.exists(out_dir):
os.mkdir(out_dir)
def _run(self):
in_dir = self._in_dir
out_dir = self._out_dir
if not os.path.exists(in_dir) or not os.path.exists(out_dir):
return
# init our simple noun-phrase chunker
grammar = r"""
NUM:
{<CD>} # "%" is interpreted as NN...
NBAR:
{<NN.*|JJ>*<NUM>*<NN.*>+} # Nouns and Adjectives, terminated with Nouns
NP:
{<NBAR>} # An NBAR is also a NP
{<NBAR><IN><NBAR>} # Above, connected with in/of/etc...
"""
chunker = nltk.RegexpParser(grammar)
filelist = os.listdir(in_dir)
tag_count = {}
i = 0
for f in filelist:
i = i + 1
logging.debug(" Reading file %d of %d" % (i, len(filelist)))
with codecs.open(os.path.join(in_dir, f), 'r', 'utf-8') as handle:
text = handle.read()
# use NLTK to chunk the text
chunks = []
sentences = nltk.sent_tokenize(text)
if sentences and len(sentences) > 0:
for sentence in sentences:
tokens = nltk.word_tokenize(sentence)
tagged = nltk.pos_tag(tokens)
tree = chunker.parse(tagged)
# get noun phrases
np = []
for st in _nltk_find_leaves(tree, 'NP'):
leaves = st.leaves()
if len(leaves) > 0:
tag = ' '.join([noun[0] for noun in leaves]).lower()
np.append(tag)
# count tags
if tag in tag_count:
tag_count[tag] = tag_count[tag] + 1
else:
tag_count[tag] = 1
if len(np) > 0:
chunks.extend(np)
# write to outfile
if len(chunks) > 0:
outfile = os.path.join(out_dir, f)
with codecs.open(outfile, 'w', 'utf-8') as w_handle:
for chunk in chunks:
w_handle.write("%s\n" % unicode(chunk))
# tag count
if len(tag_count) > 0:
with codecs.open(os.path.join(out_dir, 'tags.txt'), 'w', 'utf-8') as handle:
for tag in sorted(tag_count.iteritems(), key=operator.itemgetter(1), reverse=True):
handle.write("%s: %d\n" % (tag[0], int(tag[1])))
def _write_input(self, text, filename):
if text is None \
or len(text) < 1 \
or filename is None:
return False
in_dir = self._in_dir
if not os.path.exists(in_dir):
logging.error("The input directory for %s at %s does not exist" % (self.name, in_dir))
return False
infile = os.path.join(in_dir, filename)
if os.path.exists(infile):
return False
# write it
with codecs.open(infile, 'w', 'utf-8') as handle:
# handle.write(unicode(text))
# handle.write("\n=====\n")
handle.write(unicode(list_to_sentences(text)))
return True
def _parse_output(self, filename, **kwargs):
""" Parse NLTK output. """
if filename is None:
return None
# is there cTAKES output?
out_dir = self._out_dir
if not os.path.exists(out_dir):
logging.error("The output directory for %s at %s does not exist" % (self.name, out_dir))
return None
outfile = os.path.join(out_dir, filename)
if not os.path.exists(outfile):
# do not log here and silently fail
return None
tags = []
# read tags
with codecs.open(outfile, 'r', 'utf-8') as handle:
#line = handle.readline(keepends=False) # "keepends" not supported in readline! (http://bugs.python.org/issue8630)
lines = handle.readlines()
for line in lines:
tags.append(line.strip())
# create and return a dictionary (don't filter empty lists)
ret = {
'tags': tags,
}
# clean up
if self.cleanup:
os.remove(outfile)
in_dir = self._in_dir
infile = os.path.join(in_dir, filename)
if os.path.exists(infile):
os.remove(infile)
return ret
def _nltk_find_leaves(tree, leave_name):
try:
tree.node
except AttributeError:
return []
res = []
if leave_name == tree.node:
res.append(tree)
else:
for child in tree:
leaves = _nltk_find_leaves(child, leave_name)
if len(leaves) > 0:
res.extend(leaves)
return res
# we can execute this file to do some testing
if '__main__' == __name__:
testtext = "History of clincally significant hypogammaglobulinemia, common variable immunodeficiency, or humeral immunodeficiency."
testfile = 'test.txt'
run_dir = os.path.join(os.path.dirname(__file__), 'nltk-tags-test')
my_nlp = NLTKTags({'root': run_dir, 'cleanup': True})
my_nlp.prepare()
# create test input
if not my_nlp.write_input(testtext, testfile):
print "xx> Failed to write test input to file"
# run
try:
my_nlp.run()
except Exception as e:
print "xx> Failed: %s" % e
# parse output
ret = my_nlp.parse_output(testfile)
print ret
# clean up
os.rmdir(my_nlp._in_dir)
os.rmdir(my_nlp._out_dir)
os.rmdir(run_dir)
print "--> Done"