Ejemplo n.º 1
0
 def matches(self, tree, statemap):
     """Returns the count of this rule's LHS matches in the given tree.
     Accounts for pattern matching too."""
     count = 0
     my_vars = variables_to_paths(self.lhs)
     ## Check whether, once you replace the right parts of the given tree
     ## with variables, it's equal to this rule's LHS.
     dprint("matching this lhs:", self.lhs)
     for path, state in statemap.items():
         indexed = tree_index(tree, path)
         subtree = deepcopy(indexed)
         dprint("subtree:", subtree)
         try:
             for (var,varpath) in my_vars:
                 if varpath is ():
                     subtree = var
                     break
                 subtree[varpath] = var
         except:
             # print("EXCEPTION:", path)
             # tree is wrong shape, keep tranglin'.
             continue
         if self.state == state and subtree == self.lhs:
             # print("FOUND A MATCH.")
             count += 1
     return count
Ejemplo n.º 2
0
 def matches(self, tree, statemap):
     """Returns the count of this rule's LHS matches in the given tree.
     Accounts for pattern matching too."""
     count = 0
     my_vars = variables_to_paths(self.lhs)
     ## Check whether, once you replace the right parts of the given tree
     ## with variables, it's equal to this rule's LHS.
     dprint("matching this lhs:", self.lhs)
     for path, state in statemap.items():
         indexed = tree_index(tree, path)
         subtree = deepcopy(indexed)
         dprint("subtree:", subtree)
         try:
             for (var, varpath) in my_vars:
                 if varpath is ():
                     subtree = var
                     break
                 subtree[varpath] = var
         except:
             # print("EXCEPTION:", path)
             # tree is wrong shape, keep tranglin'.
             continue
         if self.state == state and subtree == self.lhs:
             # print("FOUND A MATCH.")
             count += 1
     return count
Ejemplo n.º 3
0
    def apply(self, tree, statemap):
        """Returns a list of (new tree, new statemap) by applying this rule in
        all the places where it works."""
        #dprint("APPLYING", self.lhs, "to", tree, statemap)
        out = []
        my_vars = variables_to_paths(self.lhs)
        for path, state in statemap.items():
            indexed = tree_index(tree, path)
            subtree = deepcopy(indexed)
            try:
                for (var, vp) in my_vars:
                    if vp is ():
                        subtree = var
                        break
                    subtree[vp] = var
            except:
                continue

            if self.state == state and subtree == self.lhs:
                newtree = deepcopy(tree)
                newsubtree = replace(indexed, self.lhs, self.rhs)
                newstates_with_prepend = {
                    tuple(list(path) + list(rulepath)): state
                    for (rulepath, state) in self.newstates.items()
                }
                newstates_with_prepend.update(statemap)
                del newstates_with_prepend[path]

                if path == ():
                    out.append((newsubtree, newstates_with_prepend))
                else:
                    newtree[path] = newsubtree
                    out.append((newtree, newstates_with_prepend))
        dprint("PRODUCED:", out)
        return out
Ejemplo n.º 4
0
def transduce(tree, rules, initial):
    """Given a tree, a set of rules, and an initial state, return the list of
    all the trees that are produced by the transduction."""

    # list of the current generation of SearchStates
    current = []
    complete = []

    # give the root the initial state.
    statemap = {(): 'q'}
    current.append(SearchState(tree, statemap, 1.0))

    progress = True
    while progress:
        nextgen = []
        # for every tree that has a state: find every rule that applies to
        # that tree. apply that rule to that tree, put the results into the
        # nextgen.
        progress = False
        for ss in current:  # (tr, statemap)
            if statemap != {}:
                for i, rule in enumerate(rules):
                    if rule.matches(ss.tree, ss.statemap):
                        results = rule.apply(ss.tree, ss.statemap)
                        for (newtr, newstatemap) in results:
                            w = rule.weight * ss.weight
                            newss = SearchState(newtr, newstatemap, w)
                            if newstatemap == {}:
                                complete.append(newss)
                            else:
                                nextgen.append(newss)
                        break

        if nextgen:
            dprint(nextgen)
            progress = True
            current = nextgen
    return complete
Ejemplo n.º 5
0
def transduce(tree, rules, initial):
    """Given a tree, a set of rules, and an initial state, return the list of
    all the trees that are produced by the transduction."""

    # list of the current generation of SearchStates
    current = []
    complete = []

    # give the root the initial state.
    statemap = {():'q'}
    current.append(SearchState(tree, statemap, 1.0))

    progress = True
    while progress:
        nextgen = []
        # for every tree that has a state: find every rule that applies to
        # that tree. apply that rule to that tree, put the results into the
        # nextgen.
        progress = False
        for ss in current: # (tr, statemap)
            if statemap != {}:
                for i,rule in enumerate(rules):
                    if rule.matches(ss.tree, ss.statemap):
                        results = rule.apply(ss.tree, ss.statemap)
                        for (newtr, newstatemap) in results:
                            w = rule.weight * ss.weight
                            newss = SearchState(newtr, newstatemap, w)
                            if newstatemap == {}:
                                complete.append(newss)
                            else:
                                nextgen.append(newss)
        if nextgen:
            dprint(nextgen)
            progress = True
            current = nextgen
    return complete