Exemplo n.º 1
0
    def add_rule(self,
                 nt,
                 name,
                 to,
                 p,
                 resample_p=1.0,
                 bv_type=None,
                 bv_args=None,
                 bv_prefix='y',
                 bv_p=None):
        """
                Adds a rule and returns the added rule.

                *nt* - The Nonterminal. e.g. S in "S -> NP VP"

                *name* - The name of this function. NOTE: If you are introducing a bound variable, the name of this function must reflect that it is a lambda node! Currently, the only way to do this is to name it 'lambda'.

                *to* - What you expand to (usually a FunctionNode).

                *p* - Unnormalized probability of expansion

                *resample_p* - In resampling, what is the probability of choosing this node?

                *bv_type* - What bound variable was introduced

                *bv_args* - What are the args when we use a bv (None is terminals, else a type signature)

        """
        self.rule_count += 1

        if bv_type is not None:

            # Check the name
            assert (
                name.lower() == 'lambda' or name.lower() == 'applylambda'
            ), "When introducing bound variables, the name of the expanded function must be 'lambda'."

            newrule = BVAddGrammarRule(nt,
                                       name,
                                       to,
                                       p=p,
                                       resample_p=resample_p,
                                       bv_type=bv_type,
                                       bv_args=bv_args,
                                       bv_prefix=bv_prefix,
                                       bv_p=bv_p)

        else:
            newrule = GrammarRule(nt, name, to, p=p, resample_p=resample_p)

        # actually add it
        self.rules[nt].append(newrule)
        return newrule
Exemplo n.º 2
0
    def add_rule(self, nt, name, to, p, bv_type=None, bv_args=None, bv_prefix='y', bv_p=None):
        """Adds a rule and returns the added rule.

        Arguments
            nt (str): The Nonterminal. e.g. S in "S -> NP VP"
            name (str): The name of this function.
            to (list<str>): What you expand to (usually a FunctionNode).
            p (float): Unnormalized probability of expansion
            bv_type (str): What bound variable was introduced
            bv_args (list): What are the args when we use a bv (None is terminals, else a type signature)

        """
        self.rule_count += 1
        assert name is not None, "To use null names, use an empty string ('') as the name."
        if bv_type is not None:
            newrule = BVAddGrammarRule(nt, name,to, p=p, bv_type=bv_type, bv_args=bv_args, bv_prefix=bv_prefix, bv_p=bv_p)
        else:
            newrule = GrammarRule(nt, name, to, p=p)

        self.rules[nt].append(newrule)
        return newrule
Exemplo n.º 3
0
    def make(cls, make_h0, data, grammar, **args):
        """ This is the initializer we use to create this from a grammar, creating children for each way grammar.start can expand """
        h0 = make_h0(value=None)

        ## For each nonterminal, find out how much (in the prior) we pay for a hole that size
        hole_penalty = dict()
        dct = defaultdict(list)  # make a lit of the log_probabilities below
        for _ in xrange(1000):  # generate this many trees
            t = grammar.generate()
            for fn in t:
                dct[fn.returntype].append(grammar.log_probability(fn))
        hole_penalty = {nt: sum(dct[nt]) / len(dct[nt]) for nt in dct}

        #We must modify the grammar to include one more nonterminal here
        mynt = "<hsmake>"
        myr = GrammarRule(mynt, '', [grammar.start], p=1.0)
        grammar.rules[mynt].append(myr)
        return cls(make_h0(
            value=FunctionNode(None, mynt, '', [grammar.start], rule=myr)),
                   data,
                   grammar,
                   hole_penalty=hole_penalty,
                   parent=None,
                   **args)  # the top state