예제 #1
0
 def test_hmm_transition_matrix(self):
     hmm = HMM({INITIAL_STATE: ['q1'],
                'q1': (['q2', 'q1', FINAL_STATE], ['dag', 'kot']),
                'q2': (['q1', FINAL_STATE], ['z'])})
     log_hmm(hmm)
     transition_matrix = hmm.get_transition_matrix()
     print(transition_matrix)
예제 #2
0
 def test_get_totally_random_hmm(self):
     configurations['MIN_NUM_OF_INNER_STATES'] = 1
     configurations['MAX_NUM_OF_INNER_STATES'] = 5
     configurations['RANDOM_HMM_MAX_EMISSIONS_PER_STATE'] = 5
     self.initialise_segment_table("plural_english_segment_table.txt")
     hmm = HMM.get_random_hmm_totally_random(data=['dag', 'dagzook', 'kook', 'kooz'])
     log_hmm(hmm)
     self.write_to_dot_to_file(hmm, "random_hmm")
예제 #3
0
 def test_remove_states(self):
     hmm = HMM({INITIAL_STATE: ['q1'],
                'q1': (['q4', FINAL_STATE], ['a']),
                'q4': (['q8', 'q8', FINAL_STATE], ['d']),
                'q7': (['q8'], ['d']),
                'q8': (['q2', FINAL_STATE], ['g']),
                'q2': (['q1', FINAL_STATE], ['z'])})
     hmm.remove_states(['q1'])
     print(hmm.inner_states)
     self.write_to_dot_to_file(hmm, 'after_remove')
     log_hmm(hmm)
예제 #4
0
 def test_defragment_states(self):
     hmm = HMM({INITIAL_STATE: ['q1'],
                'q1': (['q4', FINAL_STATE], ['a']),
                'q4': (['q10', 'q10', FINAL_STATE], ['d']),
                'q7': ([], ['d']),
                'q10': (['q2', FINAL_STATE], ['g']),
                'q2': (['q1', FINAL_STATE], ['z'])})
     self.write_to_dot_to_file(hmm, 'before_defragment')
     hmm.ensure_contiguous_state_indices()
     print(hmm.inner_states)
     self.write_to_dot_to_file(hmm, 'after_defragment')
     log_hmm(hmm)
예제 #5
0
 def test_remove_random(self):
     self.initialise_segment_table("plural_english_segment_table.txt")
     for _ in range(100000):
         hmm = HMM.get_random_hmm()
         try:
             hmm.make_mutation()
             # hmm.remove_random_state()
             hmm.get_transducer()
         except Exception:
             from traceback import print_exc
             log_hmm(hmm)
             print_exc()
             break
예제 #6
0
    def test_hmm_connected_components(self):
        hmm = HMM({INITIAL_STATE: ['q1'],
                   'q1': (['q2', FINAL_STATE], ['dag', 'kot']),
                   'q2': (['q1', FINAL_STATE], ['z'])})
        log_hmm(hmm)
        component_states = hmm.get_connected_components(ignore_initial_and_final_states=False)
        self.write_to_dot_to_file(hmm, 'connected_hmm')
        print(component_states)

        assert component_states[0][0] == FINAL_STATE
        assert component_states[2][0] == INITIAL_STATE
        assert 'q1' in component_states[1]
        assert 'q2' in component_states[1]

        component_states = hmm.get_connected_components(ignore_initial_and_final_states=True)
        assert 'q1' in component_states[0]
        assert 'q2' in component_states[0]
        assert [INITIAL_STATE] not in component_states
        assert [FINAL_STATE] not in component_states
예제 #7
0
    def test_random_hmms_crossover(self):
        self.initialise_segment_table("plural_english_segment_table.txt")
        for _ in range(1):
            hmm_1 = HMM.get_random_hmm(data=['dag', 'zook', 'kook', 'kooz'])
            hmm_2 = HMM.get_random_hmm(data=['dag', 'zook', 'kook', 'kooz'])
            print('HMM 1')
            log_hmm(hmm_1)
            print('HMM 2')
            log_hmm(hmm_2)

            offspring_1, offspring_2 = HMM.crossover(hmm_1, hmm_2)
            print('Offspring 1')
            print('HMM', offspring_1.inner_states, 'TRANSITIONS', offspring_1.transitions, 'EMISSIONS',
                  offspring_1.emissions)
            print('Offspring 2')
            print('HMM', offspring_2.inner_states, 'TRANSITIONS', offspring_2.transitions, 'EMISSIONS',
                  offspring_2.emissions)

            self.write_to_dot_to_file(hmm_1, 'random_parent_1')
            self.write_to_dot_to_file(hmm_2, 'random_parent_2')
            self.write_to_dot_to_file(offspring_1, 'random_offspring_1')
            self.write_to_dot_to_file(offspring_2, 'random_offspring_2')
            offspring_1.get_transducer()
            offspring_2.get_transducer()
예제 #8
0
    def test_crossover_by_matrix(self):
        hmm_1 = HMM({INITIAL_STATE: ['q1', 'q2'],
                     'q1': (['q2', FINAL_STATE], ['koko', 'dogo']),
                     'q2': ([FINAL_STATE], ['zz'])})
        hmm_2 = HMM({INITIAL_STATE: ['q1'],
                     'q1': (['q2'], ['dog', 'kat']),
                     'q2': (['q3'], ['s']),
                     'q3': ([FINAL_STATE], ['z'])})
        print("Parent 1")
        log_hmm(hmm_1)
        print("Parent 2")
        log_hmm(hmm_2)

        offspring_1, offspring_2 = HMM.crossover_by_matrix(hmm_1, hmm_2)
        print("Offspring 1")
        log_hmm(offspring_1)
        print("Offspring 2")
        log_hmm(offspring_2)

        self.write_to_dot_to_file(hmm_1, 'matrix_parent_1')
        self.write_to_dot_to_file(hmm_2, 'matrix_parent_2')
        self.write_to_dot_to_file(offspring_1, 'matrix_offspring_1')
        self.write_to_dot_to_file(offspring_2, 'matrix_offspring_2')
예제 #9
0
    def test_crossover(self):
        hmm_1 = HMM({INITIAL_STATE: ['q1'],
                     'q1': (['q2', FINAL_STATE], ['koko', 'dogo']),
                     'q2': ([FINAL_STATE], ['zz'])})
        hmm_2 = HMM({INITIAL_STATE: ['q1'],
                     'q1': (['q2'], ['dog', 'kat']),
                     'q2': (['q3'], ['s']),
                     'q3': ([FINAL_STATE], ['z'])})
        print("Parent 1")
        log_hmm(hmm_1)
        print()

        print("Parent 2")
        log_hmm(hmm_2)

        offspring_1, offspring_2 = HMM.crossover(hmm_1, hmm_2)
        print()
        print("Offspring 1")
        log_hmm(offspring_1)

        print()
        print("Offspring 2")
        log_hmm(offspring_2)