Exemple #1
0
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)
Exemple #2
0
 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]
    def predict(self, words, tags):
        parser = Parser()

        # 1. Start in the initial configuration for the input sentence.
        config = parser.initial_config(len(words))

        # 2. As long as there are valid moves, ask the averaged perceptron for the next move to take.

        while len(self.valid_moves(config)) != 0:
            features = self.featurize(words, tags, config)
            output_vector = self.model.forward(features)
            move = max(output_vector, key=output_vector.get)
            config = self.next_config(config, move)

        # 3. Return the list of heads associated with the final configuration.
        return config[2]