Exemple #1
0
    def test_something(self):
        grammar, r1, r2 = self.build_grammar()
        nont_map = Enumerator()
        grammarInfo = PyGrammarInfo(grammar, nont_map)

        def w(x):
            return "S", x

        rtg = RTG(w(3))
        rtg.construct_and_add_rule(w(3), r1, [w(1), w(2)])
        rtg.construct_and_add_rule(w(3), r1, [w(2), w(1)])
        rtg.construct_and_add_rule(w(2), r1, [w(1), w(1)])
        rtg.construct_and_add_rule(w(1), r2, [])

        rtg2 = RTG(("A", 3))

        rtg3 = RTG(w(3))
        rtg3.construct_and_add_rule(w(3), r1, [w(1), w(2)])
        rtg3.construct_and_add_rule(w(3), r1, [w(2), w(1)])
        rtg3.construct_and_add_rule(w(2), r2, [w(1), w(1)])
        rtg3.construct_and_add_rule(w(1), r2, [])

        traces = PyDerivationManager(grammar, nont_map)
        traces.convert_rtgs_to_hypergraphs([rtg, rtg2, rtg3])

        self.assertTrue(
            traces.is_consistent_with_grammar(grammarInfo, traceId=0))
        self.assertFalse(
            traces.is_consistent_with_grammar(grammarInfo, traceId=1))
        self.assertFalse(
            traces.is_consistent_with_grammar(grammarInfo, traceId=2))
    def __projection_based_derivation_tree(self, la, variational=False, op=prod):
        if self.nontMap is None:
            print("A nonterminal map is required for weight projection based parsing!")
            return None
        manager = PyDerivationManager(self.grammar, self.nontMap)
        manager.convert_chart_to_hypergraph(self.chart, self.disco_grammar, debug=False)
        if self.grammarInfo is not None:
            assert manager.is_consistent_with_grammar(self.grammarInfo)
        manager.set_io_cycle_limit(200)
        manager.set_io_precision(0.000001)

        if not isinstance(la, list):
            la = [la]

        edge_weights = None

        for l in la:
            edge_weights_l = py_edge_weight_projection(l, manager, variational=variational, debug=self.debug,
                                                 log_mode=self.log_mode)
            if edge_weights is None:
                edge_weights = edge_weights_l
            else:
                if self.log_mode:
                    edge_weights = [w1 + w2 for w1, w2 in zip(edge_weights, edge_weights_l)]
                else:
                    edge_weights = [op(w1, w2) for w1, w2 in zip(edge_weights, edge_weights_l)]

        if self.debug:
            nans = 0
            infs = 0
            zeros = 0
            for weight in edge_weights:
                if weight == float("nan"):
                    nans += 1
                if weight == float("inf") or weight == float("-inf"):
                    infs += 1
                if weight == 0.0:
                    zeros += 1
            print("[", len(edge_weights), nans, infs, zeros, "]")
            if len(edge_weights) < 100:
                print(edge_weights)

        der = manager.viterbi_derivation(0, edge_weights, self.grammar, op=op, log_mode=self.log_mode)
        if der is None:
            print("p", end="")
            der = self.latent_viterbi_derivation(debug=self.debug)
        if der is not None:
            der = LCFRSDerivationWrapper(der)
        if der is None:
            _, der = next(self.k_best_derivation_trees())
        return der