Ejemplo n.º 1
0
    def get_transducer(self):
        self.transducer_generated = self.get_generation_validity()

        if not self.transducer_generated:
            transducer = None
        else:
            from bracket_rule_transducer import BracketRuleTransducer
            rule_transducer_factory = BracketRuleTransducer(self)
            transducer = rule_transducer_factory.get_transducer()
        return transducer
    def test_bracket_markings(self):
        self.initialise_segment_table("plural_english_segment_table.txt")
        from bracket_rule_transducer import BracketRuleTransducer
        from bracket_rule_transducer import LEFT_IDENTITY_BRACKET, RIGHT_BRACKETS, LEFT_BRACKETS, RIGHT_IDENTITY_BRACKET
        rule = Rule([{"cons": "+"}], [{"voice": "-"}], [], [{"voice": "-"}], False)
        word = 'zt'

        prologued_word_transducer = get_prologued_word(word)

        rule.extract_data_from_feature_bundle_lists()
        bracket_rule_transducer_factory = BracketRuleTransducer(rule)
        if rule.obligatory:
            obligatory_dfa = bracket_rule_transducer_factory._get_obligatory_dfa([LEFT_IDENTITY_BRACKET],
                                                                                 RIGHT_BRACKETS)
            obligatory_transducer = pyfst_from_dfa(obligatory_dfa)
            prologued_obligatory = safe_compose(prologued_word_transducer, obligatory_transducer)

            custom_obligatory_transducer = pyfst_from_dfa(
                bracket_rule_transducer_factory._get_obligatory_dfa([RIGHT_IDENTITY_BRACKET], LEFT_BRACKETS))
            prologued_obligatory = safe_compose(prologued_obligatory, custom_obligatory_transducer)

        else:
            prologued_obligatory = prologued_word_transducer

        right_context_transducer = bracket_rule_transducer_factory._get_right_context_dfa()
        prologued_obligatory_right = safe_compose(prologued_obligatory, right_context_transducer)

        left_context_transducer = bracket_rule_transducer_factory._get_left_context_dfa()
        prologued_obligatory_right_left = safe_compose(prologued_obligatory_right, left_context_transducer)
        print(get_transducer_outputs(prologued_obligatory_right_left, limit=10))

        replace_transducer = bracket_rule_transducer_factory.get_replace_transducer()
        prologued_obligatory_right_left_replace = safe_compose(prologued_obligatory_right_left, replace_transducer)
        print(get_transducer_outputs(prologued_obligatory_right_left_replace, limit=10))
    def debug_generation(self):
        self.hall_of_fame.update(self.population)
        record = self.stats.compile(self.population) if self.stats else {}
        self.logbook.record(gen=self.generation,
                            nevals=self.new_individuals_in_generation,
                            **record)
        self.logger.info(self.logbook.stream)

        best_hypothesis_str = hypothesis_to_string(self.hall_of_fame[0])
        record.update({
            'generation': self.generation,
            'best_hypothesis': best_hypothesis_str
        })
        self.logger.log_stats_record(record, self.island_number)

        if self.generation != self.initial_generation and self.generation % ga_config.DUMP_POPULATION_INTERVAL == 0:
            self.dump_population()

        if self.generation > 0 and self.generation % ga_config.CLEAR_KEY_VALUE_CACHE_INTERVAL == 0:
            self.cache.flush()

        if self.generation > 0 and self.generation % ga_config.CLEAR_TRANSDUCERS_CACHE_INTERVAL == 0:
            from bracket_rule_transducer import BracketRuleTransducer
            self.logger.info("Clearing rule set cache...")
            RuleSet.clear_caching()
            self.logger.info("Clearing bracket rule transducers cache...")
            BracketRuleTransducer.clear_caching()

        if ga_config.LOG_POPULATION_INTERVAL > 0 and self.generation % ga_config.LOG_POPULATION_INTERVAL == 0 and self.generation > 0:
            self.log_all_population()

        if self.generation % ga_config.HALL_OF_FAME_DEBUG_INTERVAL == 0:

            self.logger.debug('\n\n**** {} top {} hypothesis:****\n'.format(
                self.island_name, ga_config.HALL_OF_FAME_HYPOTHESES))
            for i in range(ga_config.HALL_OF_FAME_HYPOTHESES):
                try:
                    hypo = self.hall_of_fame[i]
                    self.logger.debug('** #{} **'.format(i + 1))
                    log_hypothesis(hypo, self.logger.debug)
                    self.logger.debug('\n')
                except IndexError:
                    break
 def _clear_modules_caching(self):
     from bracket_rule_transducer import BracketRuleTransducer
     BracketRuleTransducer.clear_caching()
     RuleSet.clear_caching()
    def _rule_construction_helper(self, rule, word):

        from bracket_rule_transducer import get_prologue_inverse_transducer
        from bracket_rule_transducer import BracketRuleTransducer
        from bracket_rule_transducer import LEFT_IDENTITY_BRACKET, RIGHT_IDENTITY_BRACKET, RIGHT_APPLICATION_BRACKET, LEFT_APPLICATION_BRACKET
        remove_multiple_paths = False
        second_obligatory = True

        prologued_word_transducer = get_prologued_word(word)

        rule.extract_data_from_feature_bundle_lists()
        bracket_rule_transducer_factory = BracketRuleTransducer(rule)
        if rule.obligatory:
            obligatory_dfa = bracket_rule_transducer_factory._get_obligatory_dfa([LEFT_IDENTITY_BRACKET],
                                                                                 [RIGHT_IDENTITY_BRACKET])
            obligatory_transducer = pyfst_from_dfa(obligatory_dfa)
            prologued_obligatory = safe_compose(prologued_word_transducer, obligatory_transducer)
            if second_obligatory:
                custom_obligatory_transducer = pyfst_from_dfa(
                    bracket_rule_transducer_factory._get_obligatory_dfa([RIGHT_IDENTITY_BRACKET],
                                                                        [LEFT_IDENTITY_BRACKET]))
                prologued_obligatory = safe_compose(prologued_obligatory, custom_obligatory_transducer)

        else:
            prologued_obligatory = prologued_word_transducer

        right_context_transducer = bracket_rule_transducer_factory._get_right_context_dfa()
        prologued_obligatory_right = safe_compose(prologued_obligatory, right_context_transducer)

        replace_transducer = bracket_rule_transducer_factory.get_replace_transducer()
        prologued_obligatory_right_replace = safe_compose(prologued_obligatory_right, replace_transducer)

        left_context_transducer = bracket_rule_transducer_factory._get_left_context_dfa()
        prologued_obligatory_right_replace_left = safe_compose(prologued_obligatory_right_replace,
                                                               left_context_transducer)
        print(get_transducer_outputs(prologued_obligatory_right_replace_left, limit=10))

        # remove_multiple_paths
        if remove_multiple_paths:
            custom_obligatory_transducer = pyfst_from_dfa(
                bracket_rule_transducer_factory._get_custom_obligatory_dfa([RIGHT_APPLICATION_BRACKET],
                                                                           [LEFT_IDENTITY_BRACKET]))
            prologued_obligatory_right_replace_left = safe_compose(prologued_obligatory_right_replace_left,
                                                                   custom_obligatory_transducer)

            custom_obligatory_transducer = pyfst_from_dfa(
                bracket_rule_transducer_factory._get_custom_obligatory_dfa([RIGHT_IDENTITY_BRACKET],
                                                                           [LEFT_APPLICATION_BRACKET]))
            prologued_obligatory_right_replace_left = safe_compose(prologued_obligatory_right_replace_left,
                                                                   custom_obligatory_transducer)

        print(get_transducer_outputs(prologued_obligatory_right_replace_left, limit=10))

        prologue_inverse_transducer = get_prologue_inverse_transducer()
        prologued_obligatory_right_replace_left_inverse = safe_compose(prologued_obligatory_right_replace_left,
                                                                       prologue_inverse_transducer)
        prologued_obligatory_right_replace_left_inverse.remove_epsilon()
        transducer_outputs = get_transducer_outputs(prologued_obligatory_right_replace_left_inverse, limit=10)
        print(transducer_outputs)

        return transducer_outputs