コード例 #1
0
ファイル: arc_eager_test.py プロジェクト: andersjo/hals
def generate_all_projective_parses(size):
    arc_eager = ArcEager(1)
    initial = arc_eager.state(size)
    stack = []
    stack.append(initial)

    parses = set()
    while len(stack):
        state = stack.pop()
        if arc_eager.is_final(state):
            heads, labels = arc_eager.extract_parse(state)
            parses.add(tuple(heads))
        else:
            for action in arc_eager.allowed(state):
                state_copy = deepcopy(state)
                arc_eager.perform(state_copy, action)
                stack.append(state_copy)

    return parses
コード例 #2
0
ファイル: arc_eager_test.py プロジェクト: andersjo/hals
    def test_dynamic_oracle_is_complete(self):
        SIZE = 4
        arc_eager = ArcEager(1)
        dyn_oracle = ArcEagerDynamicOracle()

        valid_parses = generate_all_projective_parses(SIZE)
        for valid_parse in valid_parses:
            sent = MockSentence(len(valid_parse) + 1)
            for v, u in enumerate(valid_parse):
                sent.adjacency[u, v] = True

            state = arc_eager.state(SIZE)

            while not arc_eager.is_final(state):
                allowed_actions = arc_eager.allowed(state)
                costs = dyn_oracle(state, sent, allowed_actions)
                self.assertEqual(costs.min(), 0)
                index = costs.argmin()
                arc_eager.perform(state, allowed_actions[index])

            heads, labels = arc_eager.extract_parse(state)
            self.assertEqual(tuple(heads), valid_parse)