-
Notifications
You must be signed in to change notification settings - Fork 41
/
ptb2ud.py
212 lines (184 loc) · 7.12 KB
/
ptb2ud.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
import logging
import StanfordDependencies
import bioc
from nltk.corpus import wordnet
from nltk.stem.wordnet import WordNetLemmatizer
from nltk.tag.mapping import tagset_mapping
class Lemmatizer(object):
def __init__(self):
self.wordnet_lemmatizer = WordNetLemmatizer()
self.mapping = tagset_mapping('en-ptb', 'universal')
def lemmatize(self, word, pos=None):
"""
Determines the lemma for a given word
Args:
word(str): word
pos(str): part-of-speech
Returns:
str: lemma
"""
if pos:
return self.wordnet_lemmatizer.lemmatize(word=word, pos=pos)
else:
return self.wordnet_lemmatizer.lemmatize(word=word)
def map_tag(self, tag):
if tag in self.mapping:
tag = self.mapping[tag]
if tag == 'NOUN':
return wordnet.NOUN
elif tag == 'VERB':
return wordnet.VERB
elif tag == 'ADJ':
return wordnet.ADJ
elif tag == 'ADV':
return wordnet.ADV
elif tag == 'ADJ_SAT':
return wordnet.ADJ_SAT
return None
class Ptb2DepConverter(object):
"""
Convert ptb trees to universal dependencies
"""
basic = 'basic'
collapsed = 'collapsed'
CCprocessed = 'CCprocessed'
collapsedTree = 'collapsedTree'
def __init__(self, lemmatizer, representation='CCprocessed', universal=False):
"""
Args:
representation(str): Currently supported representations are
'basic', 'collapsed', 'CCprocessed', and 'collapsedTree'
universal(bool): if True, use universal dependencies if they're available
"""
try:
import jpype
self._backend = 'jpype'
except ImportError:
self._backend = 'subprocess'
self.lemmatizer = lemmatizer
self.__sd = StanfordDependencies.get_instance(backend=self._backend)
self.representation = representation
self.universal = universal
def convert(self, parse_tree):
"""
Convert ptb trees in a BioC sentence
Args:
parse_tree(str): parse tree in PTB format
Examples:
(ROOT (NP (JJ hello) (NN world) (. !)))
"""
if self._backend == 'jpype':
dependency_graph = self.__sd.convert_tree(parse_tree,
representation=self.representation,
universal=self.universal,
add_lemmas=True)
else:
dependency_graph = self.__sd.convert_tree(parse_tree,
representation=self.representation,
universal=self.universal)
return dependency_graph
class NegBioPtb2DepConverter(Ptb2DepConverter):
def __init__(self, lemmatizer, representation='CCprocessed', universal=False):
"""
Args:
lemmatizer (Lemmatizer)
"""
super(NegBioPtb2DepConverter, self).__init__(
lemmatizer, representation, universal)
def convert_doc(self, document):
for passage in document.passages:
for sentence in passage.sentences:
# check for empty infons, don't process if empty
# this sometimes happens with poorly tokenized sentences
if not sentence.infons:
continue
elif not sentence.infons['parse tree']:
continue
try:
dependency_graph = self.convert(
sentence.infons['parse tree'])
anns, rels = convert_dg(dependency_graph, sentence.text,
sentence.offset,
has_lemmas=self._backend == 'jpype')
sentence.annotations = anns
sentence.relations = rels
except KeyboardInterrupt:
raise
except:
logging.exception(
"Cannot process sentence %d in %s", sentence.offset, document.id)
if self._backend != 'jpype':
for ann in sentence.annotations:
text = ann.text
pos = ann.infons['tag']
pos = self.lemmatizer.map_tag(pos)
lemma = self.lemmatizer.lemmatize(word=text, pos=pos)
ann.infons['lemma'] = lemma.lower()
return document
def adapt_value(value):
"""
Adapt string in PTB
"""
value = value.replace("-LRB-", "(")
value = value.replace("-RRB-", ")")
value = value.replace("-LSB-", "[")
value = value.replace("-RSB-", "]")
value = value.replace("-LCB-", "{")
value = value.replace("-RCB-", "}")
value = value.replace("-lrb-", "(")
value = value.replace("-rrb-", ")")
value = value.replace("-lsb-", "[")
value = value.replace("-rsb-", "]")
value = value.replace("``", "\"")
value = value.replace("''", "\"")
value = value.replace("`", "'")
return value
def convert_dg(dependency_graph, text, offset, ann_index=0, rel_index=0, has_lemmas=True):
"""
Convert dependency graph to annotations and relations
"""
annotations = []
relations = []
annotation_id_map = {}
start = 0
for node in dependency_graph:
if node.index in annotation_id_map:
continue
node_form = node.form
index = text.find(node_form, start)
if index == -1:
node_form = adapt_value(node.form)
index = text.find(node_form, start)
if index == -1:
logging.debug('Cannot convert parse tree to dependency graph at %d\n%d\n%s',
start, offset, str(dependency_graph))
return
ann = bioc.BioCAnnotation()
ann.id = 'T{}'.format(ann_index)
ann.text = node_form
ann.infons['tag'] = node.pos
if has_lemmas:
ann.infons['lemma'] = node.lemma.lower()
start = index
ann.add_location(bioc.BioCLocation(start + offset, len(node_form)))
annotations.append(ann)
annotation_id_map[node.index] = ann_index
ann_index += 1
start += len(node_form)
for node in dependency_graph:
if node.head == 0:
ann = annotations[annotation_id_map[node.index]]
ann.infons['ROOT'] = True
continue
relation = bioc.BioCRelation()
relation.id = 'R{}'.format(rel_index)
relation.infons['dependency'] = node.deprel
if node.extra:
relation.infons['extra'] = node.extra
relation.add_node(bioc.BioCNode('T{}'.format(
annotation_id_map[node.index]), 'dependant'))
relation.add_node(bioc.BioCNode('T{}'.format(
annotation_id_map[node.head]), 'governor'))
relations.append(relation)
rel_index += 1
return annotations, relations