def oracle_moves(gold_heads): dependents = defaultdict(int) for head in gold_heads: dependents[head] += 1 m = None config = Parser.initial_config(len(gold_heads)) i, stack, heads = config while not Parser.is_final_config(config): valid_moves = Parser.valid_moves(config) second_topmost = stack[-2] if len(stack) >= 2 else None topmost = stack[-1] if len(stack) >= 1 else None if Parser.LA in valid_moves and gold_heads[ second_topmost] == topmost and dependents[second_topmost] == 0: m = Parser.LA dependents[topmost] -= 1 elif Parser.RA in valid_moves and gold_heads[ topmost] == second_topmost and dependents[topmost] == 0: m = Parser.RA dependents[second_topmost] -= 1 elif Parser.SH in valid_moves: m = Parser.SH else: raise "Oracle failed, no moves valid" yield config, m config = Parser.next_config(config, m)
def predict(self, words, tags): pred_heads = [] config = Parser.initial_config(len(words)) while not Parser.is_final_config(config): valid_moves = Parser.valid_moves(config) features = self.featurize(words, tags, config) pred_moves = self.model.forward(features) best_m_s = [valid_moves[0], pred_moves[valid_moves[0]]] for m in valid_moves: if pred_moves[m] > best_m_s[1]: best_m_s = [m, pred_moves[m]] config = Parser.next_config(config, best_m_s[0]) return config[2]