def test_pass_token(self): graph = Graph(self.states) score = 30 expected_score = score + graph.local_cost() expected_history = [20, 30, self.state1] graph.pass_token(Token(score, expected_history[:-1])) graph.commit() first_state = self.states[0] self.assertEqual(expected_score, first_state.token.score) self.assertEqual(expected_history, first_state.token.history)
def test_transitions_with_self_loops(self): first_state = State(20, [0.2, 0.4]) graph = Graph([first_state]) graph.add_transition(Transition.free(first_state, first_state)) graph.evolve() graph.commit() graph.evolve() graph.commit() score = graph._nodes[0].token.score self.assertEqual(- np.log(0.2) - np.log(0.4), score)
def test_word_transition(self): graph_a = Graph([self.states[0]]) graph_b = Graph([self.states[1]]) a_to_b = Transition(graph_a, graph_b, 0.99) graph_a.add_transition(a_to_b) top_level = Graph([graph_a, graph_b]) top_level.evolve() top_level.commit() self.assertEqual(graph_a.token, top_level.optimal_path()) transit_cost = a_to_b.full_cost() top_level.evolve() top_level.commit() token = top_level.optimal_path() self.assertEqual(- np.log(0.5) + transit_cost, token.score) self.assertEqual([self.state1, self.state2], token.history)
def test_multiple_states_model(self): graph_id = 7 graph = Graph(self.states, graph_id=graph_id) graph.add_transition( Transition.free(self.states[0], self.states[1]) ) graph.add_transition( Transition.free(self.states[1], self.states[1]) ) graph.evolve() graph.commit() self.assertEqual(Token(- np.log(self.p1[0]), [self.state1], [graph_id]), graph.optimal_path()) graph.evolve() graph.commit() expected = Token(- np.log(self.p1[0]) - np.log(self.p2[1]), [self.state1, self.state2], [graph_id]) self.assertEqual(expected, graph.optimal_path())
def test_one_state_model(self): states = [self.states[0]] graph = Graph(states) graph.add_transition( Transition.free(self.states[0], self.states[0]) ) graph.evolve() graph.commit() t1_prob = self.p1[0] t2_prob = self.p1[1] self.assertEqual(Token(- np.log(t1_prob), [self.state1]), graph.optimal_path()) graph.evolve() graph.commit() expected = Token(- np.log(t1_prob) - np.log(t2_prob), [self.state1, self.state1]) self.assertEqual(expected, graph.optimal_path())
def test_passing_token_to_graph_adds_word_id_to_history(self): graph = Graph(self.states, graph_id=7) token = Token(25, [3], words=[3]) graph.pass_token(token) graph.commit() self.assertEqual([3, 7], self.states[0].token.words)