Beispiel #1
0
    def __init__(self, nt, name, to, p=1.0, resample_p=1.0, bv_prefix=None):

        p = float(p)  # make sure these are floats
        
        self.__dict__.update(locals())
        
        for a in None2Empty(to):
            assert isinstance(a,str)
        
        if name == '':
            assert (to is None) or (len(to) == 1), "*** GrammarRules with empty names must have only 1 argument"
Beispiel #2
0
    def __init__(self, nt, name, to, p=1.0, bv_prefix=None):
        p = float(p)
        assert p>0.0, "*** p=0 in rule %s %s %s. What are you thinking?" %(nt,name,to)

        self.__dict__.update(locals())

        assert to is None or isinstance(to, list) or isinstance(to, tuple), "*** 'to' in a GrammarRule must be a list!"

        for a in None2Empty(to):
            assert isinstance(a,str)
        if name == '':
            assert (to is None) or (len(to) == 1), \
                "*** GrammarRules with empty names must have only 1 argument"
Beispiel #3
0
    def compute_proposal_probability(self,
                                     grammar,
                                     t1,
                                     t2,
                                     resampleProbability=lambdaOne):
        node_1, node_2 = least_common_difference(t1, t2)

        if (node_1 and node_2 and any([
                nodes_are_roughly_equal(arg, node_1)
                for arg in None2Empty(node_2.args)
        ])):

            lp_choosing_node_1 = t1.sampling_log_probability(
                node_1,
                resampleProbability=lambda t: can_insert_FunctionNode(
                    t, grammar) * resampleProbability(t))

            lp_choosing_rule = -nicelog(
                len(
                    filter(can_insert_GrammarRule,
                           grammar.rules[node_1.returntype])))
            lp_choosing_replacement = -nicelog(
                len(
                    filter(
                        lambda i: node_2.args[i].returntype == node_1.
                        returntype, xrange(len(node_2.args)))))

            lp_generation = []
            for arg in node_2.args:
                if not (arg.name == node_1.name and arg.returntype
                        == node_1.returntype and arg.args == node_1.args
                        ):  # if the nodes are significantly different
                    with BVRuleContextManager(grammar, node_2,
                                              recurse_up=True):
                        lp_generation += [grammar.log_probability(arg)]

            lp_copy_making_node_2 = lp_choosing_rule + lp_choosing_replacement + sum(
                lp_generation)

            return lp_choosing_node_1 + lp_copy_making_node_2
        else:
            return -Infinity  # the trees cannot be identical if we performed an insertion
Beispiel #4
0
    def compute_proposal_probability(self,
                                     grammar,
                                     t1,
                                     t2,
                                     resampleProbability=lambdaOne):
        node_1, node_2 = least_common_difference(t1, t2)

        if (node_1 and node_2 and any([
                nodes_are_roughly_equal(arg, node_2)
                for arg in None2Empty(node_1.args)
        ])):

            lp_choosing_node_1 = t1.sampling_log_probability(
                node_1, dp_rp(resampleProbability))
            lp_choosing_child = -nicelog(len(
                list_replicating_children(node_1)))
            return lp_choosing_node_1 + lp_choosing_child

        else:  # no possible deletion
            return -Infinity
Beispiel #5
0
def can_insert_GrammarRule(r):
    return any([r.nt==a for a in None2Empty(r.to)])
Beispiel #6
0
 def schemestring(self):
     """
         Print out in scheme format (+ 3 (- 4 5)).
     """
     if self.args is None:
         return self.name
     else:
         return '('+self.name + ' ' + ' '.join(map(lambda x: x.schemestring() if isFunctionNode(x) else str(x), None2Empty(self.args)))+')'