def setUp(self): self.state1 = 50 self.state2 = 20 self.p1 = [0.5, 0.1] self.p2 = [0.1, 0.2] self.states = [State(self.state1, self.p1), State(self.state2, self.p2)]
class TransitionTests(TestCase): def setUp(self): self.p = [0.5, 0.2, 0.1] self.state = State(25, self.p) self.p_transit = 0.25 self.transition = Transition(self.state, self.state, self.p_transit) def test_cost(self): transition = Transition(self.state, self.state, 1) self.assertEqual(0, transition.cost()) self.assertEqual(- np.log(self.p_transit), self.transition.cost()) transition = Transition.free(self.state, self.state) self.assertEqual(0, transition.cost()) def test_full_cost(self): self.assertEqual(- np.log(0.5) - np.log(self.p_transit), self.transition.full_cost()) def test_pass_token(self): expected_score = self.transition.full_cost() self.state.initialize_token() self.transition.pass_token() self.state.commit() self.assertEqual(Token(expected_score, [self.state.state]), self.state.token)
def test_before_commit(self): state = State(self.state_value, self.p) token = Token(45.3, []) state.pass_token(token) self.assertEqual(Token(state.infinite_score, []), state.token)
def test_add_transition(self): state = State(self.state_value, self.p) dest = State(2, self.p) transition = Transition(state, dest, 0.2) state.add_transition(transition) self.assertEqual(1, len(state.transitions)) destination_state = state.transitions[0].destination self.assertEqual(2, destination_state.state) self.assertEqual(self.p, destination_state.p)
def test_local_cost(self): state = State(self.state_value, [1]) self.assertEqual(0, state.local_cost()) state = State(self.state_value, self.p) self.assertEqual(- np.log(self.p[0]), state.local_cost()) state.commit() self.assertEqual(- np.log(self.p[1]), state.local_cost())
def test_pass_token_with_transit_cost(self): state = State(self.state_value, self.p) local_cost = state.local_cost() token = Token(45.3, [state]) state.pass_token(token, transit_cost=10) state.pass_token(Token(20.3, []), transit_cost=2) state.commit() self.assertEqual(Token(20.3 + local_cost + 2, [self.state_value]), state.token)
def test_token_after_passing_token_and_commit(self): state = State(self.state_value, self.p) local_cost = state.local_cost() token = Token(45.3, [state]) state.pass_token(token) state.pass_token(Token(20.3, [])) state.commit() self.assertEqual(Token(20.3 + local_cost, [self.state_value]), state.token)
def test_commit_without_passing_token(self): state = State(self.state_value, self.p) token = Token(45.3, [30]) state.pass_token(token) state.commit() state.commit() self.assertEqual(Token(state.infinite_score, []), state.token)
def test_pass_token_retains_words(self): state = State(self.state_value, self.p) words = [5, 10] token = Token(25, [], words) state.pass_token(token) state.commit() self.assertEqual(words, state.token.words)
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_token_on_nested_graph(self): g1 = Graph([self.states[0]]) g2 = Graph([self.states[1]]) g3_state = State(34, [0.2]) g3 = Graph([g3_state]) top_level = Graph([g1, g2, g3]) token = Token(3, [23]) expected_score = 3 + g3.local_cost() g3_state.pass_token(token) g3_state.commit() self.assertEqual(expected_score, top_level.token.score)
def setUp(self): self.p = [0.5, 0.2, 0.1] self.state = State(25, self.p) self.p_transit = 0.25 self.transition = Transition(self.state, self.state, self.p_transit)
def test_pass_token_after_commit(self): state = State(self.state_value, self.p) token = Token(45.3, [state]) state.pass_token(token) state.commit() cost = state.local_cost() state.pass_token(Token(80.3, [])) state.pass_token(Token(50.3, [])) state.pass_token(Token(90.3, [])) state.commit() self.assertEqual(Token(50.3 + cost, [self.state_value]), state.token)
def test_token_after_calling_initialization_method(self): state = State(self.state_value, self.p) state.initialize_token() self.assertEqual(Token(0, []), state.token)
def test_initial_token(self): state = State(self.state_value, self.p) self.assertEqual(Token(state.infinite_score, []), state.token)
def test_no_initial_transitions(self): state = State(self.state_value, self.p) self.assertEqual(0, len(state.transitions))