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))
Example #2
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 _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