-
Notifications
You must be signed in to change notification settings - Fork 1
/
gold_parse_reader.py
184 lines (158 loc) · 7.13 KB
/
gold_parse_reader.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
import logging
from feature_extractor import SparseFeatureExtractor
from sentence_batch import SentenceBatch
from parser_state import ParserState
from arc_standard_transition_system import ArcStandardTransitionSystem, \
ArcStandardTransitionState
from arc_eager_transition_system import ArcEagerTransitionState, \
ArcEagerTransitionSystem
'''
Verify that GoldParseReader parsed a sentence properly
'''
def verifyGoldSentenceIntegrity(state):
for k in range(state.numTokens()):
assert state.head(k) == state.goldHead(k), '%d, %s, %d!=%d' % \
(k, state.getToken(k).FORM, state.head(k), state.goldHead(k))
'''
Provide a batch of gold sentences to the trainer
Maintains batch_size slots of sentences, each one with its own parser state
'''
class GoldParseReader(object):
def __init__(self, input_corpus, batch_size, feature_strings, feature_maps,
transition_system, epoch_print = True):
self.input_corpus = input_corpus
self.batch_size = batch_size
self.feature_strings = feature_strings
self.feature_maps = feature_maps
self.epoch_print = epoch_print
self.feature_extractor = SparseFeatureExtractor(self.feature_strings,
self.feature_maps)
self.sentence_batch = SentenceBatch(input_corpus, self.batch_size)
self.parser_states = [None for i in range(self.batch_size)]
self.arc_states = [None for i in range(self.batch_size)]
if transition_system == 'arc-standard':
self.transition_system = ArcStandardTransitionSystem()
self.transition_state_class = ArcStandardTransitionState
elif transition_system == 'arc-eager':
self.transition_system = ArcEagerTransitionSystem()
self.transition_state_class = ArcEagerTransitionState
else:
assert None, 'transition system must be arc-standard or arc-eager'
self.logger = logging.getLogger('GoldParseReader')
self.num_epochs = 0
def state(self, i):
assert i >= 0 and i < self.batch_size
return self.parser_states[i]
'''
Advance the sentence for slot i
'''
def advanceSentence(self, i):
self.logger.debug('Slot(%d): advance sentence' % i)
assert i >= 0 and i < self.batch_size
if(self.sentence_batch.advanceSentence(i)):
self.parser_states[i] = ParserState(self.sentence_batch.sentence(i),
self.feature_maps)
# necessary for initializing and pushing root
# keep arc_states in sync with parser_states
self.arc_states[i] = \
self.transition_state_class(self.parser_states[i])
else:
self.parser_states[i] = None
self.arc_states[i] = None
'''
Perform the next gold action for each state
'''
def performActions(self):
for i in range(self.batch_size):
if self.state(i) != None:
self.logger.debug('Slot(%d): perform actions' % i)
nextGoldAction = \
self.transition_system.getNextGoldAction(self.state(i))
#print('nextGoldAction:', nextGoldAction)
self.logger.debug('Slot(%d): perform action %d=%s' %
(i, nextGoldAction, self.transition_system.actionAsString(
nextGoldAction, self.state(i), self.feature_maps)))
try:
self.transition_system.performAction(
action=nextGoldAction,
state=self.state(i))
except:
self.logger.debug(
'Slot(%d): invalid action at batch slot' % i)
# This is probably because of a non-projective input
# We could projectivize or remove it...
self.transition_system.performAction(
action=self.transition_system.getDefaultAction(
self.state(i)),
state=self.state(i))
'''
Concatenate and return feature bags for all sentence slots, grouped
by feature major type
Returns (None, None, None, ...) if no sentences left
'''
def nextFeatureBags(self):
self.performActions()
for i in range(self.batch_size):
if self.state(i) == None:
continue
while(self.transition_system.isFinalState(self.state(i))):
verifyGoldSentenceIntegrity(self.state(i))
self.logger.debug('Advancing sentence ' + str(i))
self.advanceSentence(i)
if self.state(i) == None:
break
if self.sentence_batch.size() == 0:
self.num_epochs += 1
if self.epoch_print:
self.logger.info('Starting epoch ' + str(self.num_epochs))
self.sentence_batch.rewind()
for i in range(self.batch_size):
self.advanceSentence(i)
# a little bit different from SyntaxNet:
# we don't support feature groups
# we automatically group together the similar types
# features_output = [[] for i in range(self.feature_strings)]
features_major_types = None
features_output = None
gold_actions = None
statesToExtract = []
# Populate feature outputs
for i in range(self.batch_size):
if self.state(i) == None:
continue
statesToExtract.append(self.state(i))
# Populate feature outputs
for i in range(self.batch_size):
if self.state(i) == None:
continue
self.logger.debug('Slot(%d): extract features' % i)
'''
If you want to enable more detailed logging, please set
doLogging here. Disabled for performance.
'''
fvec = self.feature_extractor.extract(self.state(i), \
doLogging=False)
assert len(fvec.types) == len(self.feature_strings)
major_types, ids = fvec.concatenateSimilarTypes()
if features_output == None:
features_major_types = [t for t in major_types]
features_output = [[] for t in major_types]
else:
assert len(features_major_types) == len(major_types)
assert len(features_output) == len(major_types)
for k in range(len(features_major_types)):
features_output[k] += ids[k]
# Fill in gold actions
for i in range(self.batch_size):
if self.state(i) != None:
if gold_actions == None:
gold_actions = []
try:
gold_actions.append(
self.transition_system.getNextGoldAction(self.state(i)))
except:
self.logger.info('Warning: invalid batch slot')
gold_actions.append(
self.transition_system.getDefaultAction(self.state(i)))
return features_major_types, features_output, gold_actions, \
self.num_epochs