コード例 #1
0
    def __projection_based_derivation_tree(self, la, variational=False, op=prod):
        manager = PyDerivationManager(self.grammar, self.nontMap)
        derivations = [der for _, der in self.base_parser.k_best_derivation_trees()]
        manager.convert_derivations_to_hypergraph(derivations)
        manager.set_io_cycle_limit(200)
        manager.set_io_precision(0.000001)
        self.debug = False
        self.log_mode = True
        edge_weights = py_edge_weight_projection(la, manager, variational=variational, debug=self.debug,
                                                 log_mode=self.log_mode)
        der = manager.viterbi_derivation(0, edge_weights, self.grammar, op=op, log_mode=self.log_mode)
        if der is None:
            if True or self.debug:
                nans = 0
                infs = 0
                zeros = 0
                for weight in edge_weights:
                    if math.isnan(weight):
                        nans += 1
                    if math.isinf(weight):
                        infs += 1
                    if weight == 0.0:
                        zeros += 1
                print("[", len(edge_weights), nans, infs, zeros, "]")
                if len(edge_weights) < 200:
                    print("orig:", edge_weights)
                    edge_weights = py_edge_weight_projection(la, manager, variational=variational, debug=True, log_mode=self.log_mode)
                    print("1:", edge_weights)
                    edge_weights = py_edge_weight_projection(la, manager, variational=variational, debug=True,
                                                             log_mode=self.log_mode)
                    print("2:", edge_weights)

            print("p", end="")
            _, der = next(self.k_best_derivation_trees())
        return der
コード例 #2
0
    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