コード例 #1
0
ファイル: tree.py プロジェクト: steve3p0/LING511
    def embed_n_np(self, t: nltk.Tree):

        # RB - Adverb
        # RBR - Adverb, comparative
        # RBS - Adverb, superlative

        try:
            t.label()
        except AttributeError:
            # print(t)
            return

        try:
            for child in t:
                #t = nltk.ParentedTree.convert(t)
                if child.label() == child.right_sibling().label() == "NN":
                    # noun = child
                    noun = nltk.ParentedTree("NN", [child[0]])

                    np = nltk.ParentedTree("NP", [noun])
                    child_pos = self.get_position(child, t)
                    t.remove(child)
                    t.insert(child_pos, np)

                    t = nltk.ParentedTree.convert(t)
                    parent = t.parent()
                    parent = nltk.ParentedTree.convert(parent)
        except Exception:
            #print("swallow hard!")
            pass

        for child in t:
            self.embed_n_np(child)
コード例 #2
0
def flatten_tree(x: Tree):
    assert (x.label() == "@START@")
    assert (len(x) == 1)
    xstr = tree_to_lisp_tokens(x[0])
    nodes = [Tree(xe if xe not in "()" else "|" + xe, []) for xe in xstr]
    y = Tree(x.label(), nodes)
    return y
コード例 #3
0
ファイル: grammar.py プロジェクト: saist1993/parseq
def tree_to_lisp_tokens(x: Tree, brackets="()"):
    if len(x) > 0:
        children = [tree_to_lisp_tokens(xe, brackets=brackets) for xe in x]
        return [brackets[0], x.label()
                ] + [childe for child in children
                     for childe in child] + [brackets[1]]
    else:
        return [x.label()]
コード例 #4
0
ファイル: datasets.py プロジェクト: saist1993/parseq
 def restore_reverse(t:Tree)->Tree:
     tchildren = [restore_reverse(te) for te in t]
     t[:] = tchildren
     if t.label().startswith("arg:~") and not t.label() == "arg:~type":
         newt = Tree("SW:reverse", [t])
         t.set_label(f"arg:{t.label()[len('arg:~'):]}")
         return newt
     else:
         return t
コード例 #5
0
 def _reorder_rec(self, x: Tree):
     if x.label() in self.orderless:
         children = None
         if len(x) > 0:
             children = [self._reorder_rec(child) for child in x[:]]
             self.rnd.shuffle(children)
         ret = Tree(x.label(), children=children)
     else:
         ret = Tree(x.label(),
                    children=[self._reorder_rec(child) for child in x[:]])
     return ret
コード例 #6
0
ファイル: geoquery_rank.py プロジェクト: saist1993/parseq
def tree2toks(tree: Tree):
    if len(tree) == 0:
        return [tree.label()]
    else:
        children = [tree2toks(x) for x in tree]
        ret = [tree.label()] if tree.label() != "@NAMELESS@" else []
        ret.insert(0, "(")
        for child in children:
            ret += child
        ret.append(")")
        return ret
コード例 #7
0
ファイル: datasets.py プロジェクト: saist1993/parseq
 def remap_reverse_labels_blocks(t:Tree):
     mapd = {
         "arg:~left": "arg:right",
         "arg:~right": "arg:left",
         "arg:~below": "arg:above",
         "arg:~above": "arg:below"
     }
     if t.label() in mapd:
         t.set_label(mapd[t.label()])
     [remap_reverse_labels_blocks(te) for te in t]
     return t
コード例 #8
0
ファイル: tree.py プロジェクト: steve3p0/LING511
    def enforce_cpc(self, t: nltk.Tree):
        try:
            t.label()
        except AttributeError:
            # print(t)
            return

        if t.label() == "CP":
            t[0].set_label("C")

        for child in t:
            self.enforce_cpc(child)
コード例 #9
0
ファイル: grammar.py プロジェクト: lukovnikov/parseq
def tree_to_lisp_tokens(x: Tree, brackets="()", _bracketize_leafs=False):
    if len(x) > 0 or _bracketize_leafs:
        children = [
            tree_to_lisp_tokens(xe,
                                brackets=brackets,
                                _bracketize_leafs=_bracketize_leafs)
            for xe in x
        ]
        return [brackets[0], x.label()
                ] + [childe for child in children
                     for childe in child] + [brackets[1]]
    else:
        return [x.label()]
コード例 #10
0
def find_lhs_grammar_rule(first: Tree, second: Tree, grammar: CFG) -> Nonterminal:
    """
    Finds the LHS of a grammar rule contained in the input CFG grammar.

    :param first: first half of the grammar rule's RHS
    :param second: latter half of the grammar rule's RHS
    :param grammar: input CFG grammar
    :return: the LHS of a grammar rule, if it exists
    """
    grammar_rules = list((prod for prod in grammar.productions()
                          if len(prod.rhs()) == 2
                          and first.label() == prod.rhs()[0] and second.label() == prod.rhs()[1]))

    if grammar_rules:
        return grammar_rules[0].lhs()
コード例 #11
0
ファイル: data.py プロジェクト: saist1993/parseq
def remove_literals(x:Tree, literalparents=("placeid", "countryid", "riverid", "cityid", "stateid")):
    if x.label() in literalparents:
        del x[:]
        return x
    else:
        x[:] = [remove_literals(xe, literalparents) for xe in x]
        return x
コード例 #12
0
    def transform(self, node, new_tree):
        if isinstance(node, Tree):
            new_node = None

            if self.is_s_with_np_vp(node):
                children = []
                first_NP = False

                for child in node:
                    if child.label() == "NP" and not first_NP:
                        first_NP = True

                    if first_NP:
                        children.append(child)

                node = Tree(node.label, children)

            new_children = []
            for child in node:
                new_child = self.transform(child, new_tree)
                new_children.append(new_child)
                new_node = Tree(node.label(), new_children)

            return new_node

        return node
コード例 #13
0
ファイル: ToPcfg.py プロジェクト: ericmclachlan/Ling571
    def __count_productions_recursively(self,
                                        node: nltk.Tree) -> nltk.Nonterminal:
        """Recursively parses a tree representation of a sentence."""
        label = node.label()
        # Traverse the tree:
        if (len(node) == 2):
            # Handle non-leaf nodes:
            left = self.__count_productions_recursively(node[0])
            right = self.__count_productions_recursively(node[1])
            production = nltk.Production(
                nltk.Nonterminal(label),
                [nltk.Nonterminal(left.lhs()),
                 nltk.Nonterminal(right.lhs())])
        else:
            # Handle leaf node.
            token = node[0]
            self.token_count += 1
            if (token not in self.count_per_token):
                self.count_per_token[token] = 1
            else:
                self.count_per_token[token] += 1
            production = nltk.Production(nltk.Nonterminal(label), [token])

        # Update our count of this particular productions.
        if (production not in self.count_per_production):
            self.count_per_production[production] = 1
        else:
            self.count_per_production[production] += 1
        # Update our count of all productions with a particular LHS.
        lhs = production.lhs()
        if (lhs not in self.lhs_count):
            self.lhs_count[lhs] = 1
        else:
            self.lhs_count[lhs] += 1
        return production
コード例 #14
0
def translate_nltk_tree(tree: nltk.Tree, tree_def: TreeDefinition, label_map: T.Dict[str, int], normalizer: T.Callable[[str], str], ignore_leaves=False):
    if tree.height() > 2:
        return Tree(node_type_id="NODE",
                    children=list(map(lambda x: translate_nltk_tree(x, tree_def, label_map, normalizer, ignore_leaves), tree)),
                    value=tree_def.id_map["NODE"].value_type(abstract_value=tree.label()))
    else:
        normalized = normalizer(tree.leaves()[0])
        return Tree(node_type_id="PRE_LEAF",
                    children=[
                        Tree(
                            node_type_id="LEAF",
                            children=[],
                            value=tree_def.id_map["LEAF"].value_type(abstract_value=label_map.get(normalized,0))  # 0 is oov
                        )
                    ] if not ignore_leaves else [],
                    value=tree_def.id_map["PRE_LEAF"].value_type(abstract_value=tree.label()))
コード例 #15
0
 def sorted_tree_tandem(self, x: Tree, y: Tree):
     """ Sort both x and y trees by tree x """
     # sort tree children alphabetically
     xchildren, ychildren = [], []
     rets = [
         self.sorted_tree_tandem(xchild, ychild)
         for xchild, ychild in zip(x[:], y[:])
     ]
     if x.label() in self.orderless:
         rets = sorted(rets, key=lambda x: x[0])
     if len(rets) > 0:
         xchildren, ychildren = zip(*rets)
         xchildren, ychildren = list(xchildren), list(ychildren)
     xret = Tree(x.label(), children=xchildren)
     yret = Tree(y.label(), children=ychildren)
     return xret, yret
コード例 #16
0
def simplify_tree_for_eval(x: Tree):  # removes @SLOT@'s and @START@
    children = [simplify_tree_for_eval(xe) for xe in x]
    children = [child for child in children if child is not None]
    x[:] = children
    if x.label() == "@SLOT@":
        return None
    else:
        return x
コード例 #17
0
ファイル: world.py プロジェクト: pyknife/allennlp
    def _construct_node_from_actions(self,
                                     current_node: Tree,
                                     remaining_actions: List[List[str]],
                                     add_var_function: bool) -> List[List[str]]:
        """
        Given a current node in the logical form tree, and a list of actions in an action sequence,
        this method fills in the children of the current node from the action sequence, then
        returns whatever actions are left.

        For example, we could get a node with type ``c``, and an action sequence that begins with
        ``c -> [<r,c>, r]``.  This method will add two children to the input node, consuming
        actions from the action sequence for nodes of type ``<r,c>`` (and all of its children,
        recursively) and ``r`` (and all of its children, recursively).  This method assumes that
        action sequences are produced `depth-first`, so all actions for the subtree under ``<r,c>``
        appear before actions for the subtree under ``r``.  If there are any actions in the action
        sequence after the ``<r,c>`` and ``r`` subtrees have terminated in leaf nodes, they will be
        returned.
        """
        if not remaining_actions:
            logger.error("No actions left to construct current node: %s", current_node)
            raise ParsingError("Incomplete action sequence")
        left_side, right_side = remaining_actions.pop(0)
        if left_side != current_node.label():
            logger.error("Current node: %s", current_node)
            logger.error("Next action: %s -> %s", left_side, right_side)
            logger.error("Remaining actions were: %s", remaining_actions)
            raise ParsingError("Current node does not match next action")
        if right_side[0] == '[':
            # This is a non-terminal expansion, with more than one child node.
            for child_type in right_side[1:-1].split(', '):
                if child_type.startswith("'lambda"):
                    # We need to special-case the handling of lambda here, because it's handled a
                    # bit weirdly in the action sequence.  This is stripping off the single quotes
                    # around something like `'lambda x'`.
                    child_type = child_type[1:-1]
                child_node = Tree(child_type, [])
                current_node.append(child_node)  # you add a child to an nltk.Tree with `append`
                if not self.is_terminal(child_type):
                    remaining_actions = self._construct_node_from_actions(child_node,
                                                                          remaining_actions,
                                                                          add_var_function)
        elif self.is_terminal(right_side):
            # The current node is a pre-terminal; we'll add a single terminal child.  We need to
            # check first for whether we need to add a (var _) around the terminal node, though.
            if add_var_function and right_side in self._lambda_variables:
                right_side = f"(var {right_side})"
            if add_var_function and right_side == 'var':
                raise ParsingError('add_var_function was true, but action sequence already had var')
            current_node.append(Tree(right_side, []))  # you add a child to an nltk.Tree with `append`
        else:
            # The only way this can happen is if you have a unary non-terminal production rule.
            # That is almost certainly not what you want with this kind of grammar, so we'll crash.
            # If you really do want this, open a PR with a valid use case.
            raise ParsingError(f"Found a unary production rule: {left_side} -> {right_side}. "
                               "Are you sure you want a unary production rule in your grammar?")
        return remaining_actions
コード例 #18
0
def stem_id_words_tree(tree:Tree, idparents, stem=False, strtok=None):
    if stem is True:
        assert(len(tree) == 0)  # should be leaf
    if len(tree) == 0:
        if stem is True:
            if re.match(r"'([^']+)'", tree.label()):
                pas = re.match(r"'([^']+)'", tree.label()).group(1)
                pas = strtok(pas)
                return [Tree("str", pas)]
            else:
                return [tree]
        else:
            return [tree]
    else:
        tostem = tree.label() in idparents
        children = [stem_id_words_tree(k, idparents, stem=tostem, strtok=strtok)
                    for k in tree]
        children = [a for b in children for a in b]
        return [Tree(tree.label(), children)]
コード例 #19
0
 def normalize_tree(self, x: Tree):
     # sort tree children alphabetically
     anonstrs, children = [], []
     rets = [self.normalize_tree(child) for child in x]
     if x.label() in self.orderless:
         rets = sorted(rets, key=lambda x: x[0])
     if len(rets) > 0:
         anonstrs, children = zip(*rets)
         anonstrs, children = list(anonstrs), list(children)
     if re.match(self.entitypattern, x.label()):
         anonret = "@ENT@"
     elif re.match(self.variablepattern, x.label()):
         anonret = "@VAR@"
     else:
         anonret = x.label()
     if len(anonstrs) > 0:
         anonret = f"({anonret} {' '.join(anonstrs)})"
     ret = Tree(x.label(), children=children)
     return anonret, ret
コード例 #20
0
def tree_get_ner(t: nltk.Tree) -> list:
    ner = []
    label = t.label()

    if label != 'S':
        ner = [(' '.join(l for (l, _) in t.leaves()), label)]

    for branch in t:
        if type(branch) == nltk.Tree:
            ner += tree_get_ner(branch)

    return ner
コード例 #21
0
ファイル: overnight_basic.py プロジェクト: saist1993/parseq
 def simplify_tree(t: Tree):
     if t.label() == "call":
         assert (len(t[0]) == 0)
         # if not t[0].label().startswith("SW."):
         #     print(t)
         # assert(t[0].label().startswith("SW."))
         t.set_label(t[0].label())
         del t[0]
     elif t.label() == "string":
         afterstring.update(set([tc.label() for tc in t]))
         assert (len(t) == 1)
         assert (len(t[0]) == 0)
         t.set_label(f"arg:{t[0].label()}")
         del t[0]
     if t.label().startswith(
             "edu.stanford.nlp.sempre.overnight.SimpleWorld."):
         t.set_label("SW:" + t.label(
         )[len("edu.stanford.nlp.sempre.overnight.SimpleWorld."):])
     if t.label() == "SW:getProperty":
         assert (len(t) == 2)
         ret = simplify_tree(t[1])
         ret.append(simplify_tree(t[0]))
         return ret
     elif t.label() == "SW:singleton":
         assert (len(t) == 1)
         assert (len(t[0]) == 0)
         return t[0]
     elif t.label() == "SW:ensureNumericProperty":
         assert (len(t) == 1)
         return simplify_tree(t[0])
     elif t.label() == "SW:ensureNumericEntity":
         assert (len(t) == 1)
         return simplify_tree(t[0])
     elif t.label() == "SW:aggregate":
         assert (len(t) == 2)
         ret = simplify_tree(t[0])
         assert (ret.label() in ["arg:avg", "arg:sum"])
         assert (len(ret) == 0)
         ret.set_label(f"agg:{ret.label()}")
         ret.append(simplify_tree(t[1]))
         return ret
     else:
         t[:] = [simplify_tree(tc) for tc in t]
         return t
コード例 #22
0
    def _build_hierplane_tree(self, tree: Tree, index: int,
                              is_root: bool) -> JsonDict:
        """
        Recursively builds a JSON dictionary from an NLTK ``Tree`` suitable for
        rendering trees using the `Hierplane library<https://allenai.github.io/hierplane/>`.

        Parameters
        ----------
        tree : ``Tree``, required.
            The tree to convert into Hierplane JSON.
        index : int, required.
            The character index into the tree, used for creating spans.
        is_root : bool
            An indicator which allows us to add the outer Hierplane JSON which
            is required for rendering.

        Returns
        -------
        A JSON dictionary render-able by Hierplane for the given tree.
        """
        children = []
        for child in tree:
            if isinstance(child, Tree):
                # If the child is a tree, it has children,
                # as NLTK leaves are just strings.
                children.append(
                    self._build_hierplane_tree(child, index, is_root=False))
            else:
                # We're at a leaf, so add the length of
                # the word to the character index.
                index += len(child)

        label = tree.label()
        span = " ".join(tree.leaves())
        hierplane_node = {
            "word": span,
            "nodeType": label,
            "attributes": [label],
            "link": label
        }
        if children:
            hierplane_node["children"] = children
        # TODO(Mark): Figure out how to span highlighting to the leaves.
        if is_root:
            hierplane_node = {
                "linkNameToLabel": LINK_TO_LABEL,
                "nodeTypeToStyle": NODE_TYPE_TO_STYLE,
                "text": span,
                "root": hierplane_node
            }
        return hierplane_node
コード例 #23
0
ファイル: tree.py プロジェクト: steve3p0/LING511
    def convert_adv_neg(self, t: nltk.Tree):

        try:
            t.label()
        except AttributeError:
            # print(t)
            return

        if t.label() in ["AdvP"]:
            phrase = t

            try:
                if phrase[0].label() == "Adv" and \
                   phrase[0][0] == 'not':
                    #t = nltk.ParentedTree.convert(t)
                    t.set_label('NegP')
                    phrase[0].set_label("Neg")
            except:
                #print("swallow hard!")
                pass

        for child in t:
            self.convert_adv_neg(child)
コード例 #24
0
def updateGrammar(Tree):
    if len(Tree) == 1:
        label = parse(Tree)
        #print(label)
        symbols[Tree.label()] += 1
        if label in dynamic_grammar:
            dynamic_grammar[label] += 1
        else:
            dynamic_grammar[label] = 1
        return

    left = Tree[0]
    right = Tree[1]
    selfLabel = '%s -> %s %s' % (Tree.label(), left.label(), right.label())
    symbols[Tree.label()] += 1
    updateGrammar(left)
    updateGrammar(right)
    if selfLabel in dynamic_grammar:
        dynamic_grammar[selfLabel] += 1
    else:
        dynamic_grammar[selfLabel] = 1

    return
コード例 #25
0
def find_chunk(tree: nltk.Tree,
               target: str,
               res: List[nltk.Tree] = None) -> nltk.Tree:
    """Find and return a target chunk."""
    res = res if res is not None else []
    if tree.label() == target:
        res.append(tree)
        return

    for subtree in tree:
        if type(subtree) == nltk.tree.Tree:
            find_chunk(subtree, target, res)

    return res
コード例 #26
0
ファイル: book_parser.py プロジェクト: TheDecks/Studies
 def extract_label(tree: nltk.Tree, label: str = 'PERSON') -> List[str]:
     """
     nltk parses sentence as nested tree structure. Function iteratively searches for a tree labeled as >label<.
     As of
     https://gist.github.com/onyxfish/322906?fbclid=IwAR0Qt7gGkrDDCeozS2Thd3qBjx_mgClqGI7GhcmQsONtIuqiB81KkXHWaxk .
     """
     entity_names = []
     if hasattr(tree, 'label') and tree.label:
         if tree.label() == label:
             entity_names.append(' '.join([child[0] for child in tree]))
         else:
             for child in tree:
                 entity_names.extend(BookParser.extract_label(child))
     return entity_names
コード例 #27
0
    def _build_hierplane_tree(self, tree: Tree, index: int, is_root: bool) -> JsonDict:
        """
        Recursively builds a JSON dictionary from an NLTK ``Tree`` suitable for
        rendering trees using the `Hierplane library<https://allenai.github.io/hierplane/>`.

        Parameters
        ----------
        tree : ``Tree``, required.
            The tree to convert into Hierplane JSON.
        index : int, required.
            The character index into the tree, used for creating spans.
        is_root : bool
            An indicator which allows us to add the outer Hierplane JSON which
            is required for rendering.

        Returns
        -------
        A JSON dictionary render-able by Hierplane for the given tree.
        """
        children = []
        for child in tree:
            if isinstance(child, Tree):
                # If the child is a tree, it has children,
                # as NLTK leaves are just strings.
                children.append(self._build_hierplane_tree(child, index, is_root=False))
            else:
                # We're at a leaf, so add the length of
                # the word to the character index.
                index += len(child)

        label = tree.label()
        span = " ".join(tree.leaves())
        hierplane_node = {
                "word": span,
                "nodeType": label,
                "attributes": [label],
                "link": label
        }
        if children:
            hierplane_node["children"] = children
        # TODO(Mark): Figure out how to span highlighting to the leaves.
        if is_root:
            hierplane_node = {
                    "linkNameToLabel": LINK_TO_LABEL,
                    "nodeTypeToStyle": NODE_TYPE_TO_STYLE,
                    "text": span,
                    "root": hierplane_node
            }
        return hierplane_node
コード例 #28
0
ファイル: Evaluation.py プロジェクト: xxin1984/x-parser
def _recursive_get_labels(node: Tree, i, pos_labels: list, cst_labels: list):
    if isinstance(node[0], str):
        # preleaf node
        word = node[0]
        j = i + len(word)
        span = i, j
        pos = node.label()
        pos_label = pos, span
        pos_labels.append(pos_label)
        return j
    else:
        # constituent node
        child_j = i
        for child in node:
            child_j = _recursive_get_labels(node=child,
                                            i=child_j,
                                            pos_labels=pos_labels,
                                            cst_labels=cst_labels)
        j = child_j
        span = i, child_j
        cst = node.label()
        cst_label = cst, span
        cst_labels.append(cst_label)
        return j
コード例 #29
0
ファイル: world.py プロジェクト: pyknife/allennlp
def nltk_tree_to_logical_form(tree: Tree) -> str:
    """
    Given an ``nltk.Tree`` representing the syntax tree that generates a logical form, this method
    produces the actual (lisp-like) logical form, with all of the non-terminal symbols converted
    into the correct number of parentheses.
    """
    # nltk.Tree actually inherits from `list`, so you use `len()` to get the number of children.
    # We're going to be explicit about checking length, instead of using `if tree:`, just to avoid
    # any funny business nltk might have done (e.g., it's really odd if `if tree:` evaluates to
    # `False` if there's a single leaf node with no children).
    if len(tree) == 0:  # pylint: disable=len-as-condition
        return tree.label()
    if len(tree) == 1:
        return tree[0].label()
    return '(' + ' '.join(nltk_tree_to_logical_form(child) for child in tree) + ')'
コード例 #30
0
ファイル: tree.py プロジェクト: steve3p0/LING511
    def convert_adv_deg(self, t: nltk.Tree):

        # RB - Adverb
        # RBR - Adverb, comparative
        # RBS - Adverb, superlative

        try:
            t.label()
        except AttributeError:
            # print(t)
            return

        if t.label() in ["ADJP", "ADVP"]:
            phrase = t

            try:
                if phrase[0].label() == "RB" and \
                   phrase[1].label() in ["RB", "JJ"]:
                    #t = nltk.ParentedTree.convert(t)
                    adv = phrase[0]
                    if adv[0] in ["too", "very"]:
                        if len(t) > 1:
                            if adv.right_sibling().label() in ["RB", "JJ"]:
                                deg = nltk.ParentedTree("Deg", [adv[0]])
                                t.remove(t[0])
                                t.insert(0, deg)

                                t = nltk.ParentedTree.convert(t)
                                parent = t.parent()
                                parent = nltk.ParentedTree.convert(parent)
            except:
                #print("swallow hard!")
                pass

        for child in t:
            self.convert_adv_deg(child)
コード例 #31
0
ファイル: xp_over_xp.py プロジェクト: mounicam/hedge_trimmer
    def transform(self, node, new_tree):
        if isinstance(node, Tree):
            new_node = None

            if self.is_x_over_x(node):
                node = Tree(node.label, [node[0]])

            new_children = []
            for child in node:
                new_child = self.transform(child, new_tree)
                new_children.append(new_child)
                new_node = Tree(node.label(), new_children)

            return new_node

        return node
コード例 #32
0
ファイル: world.py プロジェクト: zwdcs/allennlp
def nltk_tree_to_logical_form(tree: Tree) -> str:
    """
    Given an ``nltk.Tree`` representing the syntax tree that generates a logical form, this method
    produces the actual (lisp-like) logical form, with all of the non-terminal symbols converted
    into the correct number of parentheses.
    """
    # nltk.Tree actually inherits from `list`, so you use `len()` to get the number of children.
    # We're going to be explicit about checking length, instead of using `if tree:`, just to avoid
    # any funny business nltk might have done (e.g., it's really odd if `if tree:` evaluates to
    # `False` if there's a single leaf node with no children).
    if len(tree) == 0:  # pylint: disable=len-as-condition
        return tree.label()
    if len(tree) == 1:
        return tree[0].label()
    return '(' + ' '.join(nltk_tree_to_logical_form(child)
                          for child in tree) + ')'
コード例 #33
0
    def _construct_node_from_actions(
            self, current_node: Tree,
            remaining_actions: List[List[str]]) -> List[List[str]]:
        """
        Given a current node in the logical form tree, and a list of actions in an action sequence,
        this method fills in the children of the current node from the action sequence, then
        returns whatever actions are left.

        For example, we could get a node with type ``c``, and an action sequence that begins with
        ``c -> [<r,c>, r]``.  This method will add two children to the input node, consuming
        actions from the action sequence for nodes of type ``<r,c>`` (and all of its children,
        recursively) and ``r`` (and all of its children, recursively).  This method assumes that
        action sequences are produced `depth-first`, so all actions for the subtree under ``<r,c>``
        appear before actions for the subtree under ``r``.  If there are any actions in the action
        sequence after the ``<r,c>`` and ``r`` subtrees have terminated in leaf nodes, they will be
        returned.
        """
        if not remaining_actions:
            logger.error("No actions left to construct current node: %s",
                         current_node)
            raise ParsingError("Incomplete action sequence")
        left_side, right_side = remaining_actions.pop(0)
        if left_side != current_node.label():
            logger.error("Current node: %s", current_node)
            logger.error("Next action: %s -> %s", left_side, right_side)
            logger.error("Remaining actions were: %s", remaining_actions)
            raise ParsingError("Current node does not match next action")
        if right_side[0] == '[':
            # This is a non-terminal expansion, with more than one child node.
            for child_type in right_side[1:-1].split(', '):
                child_node = Tree(child_type, [])
                current_node.append(
                    child_node
                )  # you add a child to an nltk.Tree with `append`
                # For now, we assume that all children in a list like this are non-terminals, so we
                # recurse on them.  I'm pretty sure that will always be true for the way our
                # grammar induction works.  We can revisit this later if we need to.
                remaining_actions = self._construct_node_from_actions(
                    child_node, remaining_actions)
        else:
            # The current node is a pre-terminal; we'll add a single terminal child.  By
            # construction, the right-hand side of our production rules are only ever terminal
            # productions or lists of non-terminals.
            current_node.append(
                Tree(right_side,
                     []))  # you add a child to an nltk.Tree with `append`
        return remaining_actions
コード例 #34
0
ファイル: domain_language.py プロジェクト: apmoore1/allennlp
    def _construct_node_from_actions(self,
                                     current_node: Tree,
                                     remaining_actions: List[List[str]]) -> List[List[str]]:
        """
        Given a current node in the logical form tree, and a list of actions in an action sequence,
        this method fills in the children of the current node from the action sequence, then
        returns whatever actions are left.

        For example, we could get a node with type ``c``, and an action sequence that begins with
        ``c -> [<r,c>, r]``.  This method will add two children to the input node, consuming
        actions from the action sequence for nodes of type ``<r,c>`` (and all of its children,
        recursively) and ``r`` (and all of its children, recursively).  This method assumes that
        action sequences are produced `depth-first`, so all actions for the subtree under ``<r,c>``
        appear before actions for the subtree under ``r``.  If there are any actions in the action
        sequence after the ``<r,c>`` and ``r`` subtrees have terminated in leaf nodes, they will be
        returned.
        """
        if not remaining_actions:
            logger.error("No actions left to construct current node: %s", current_node)
            raise ParsingError("Incomplete action sequence")
        left_side, right_side = remaining_actions.pop(0)
        if left_side != current_node.label():
            logger.error("Current node: %s", current_node)
            logger.error("Next action: %s -> %s", left_side, right_side)
            logger.error("Remaining actions were: %s", remaining_actions)
            raise ParsingError("Current node does not match next action")
        if right_side[0] == '[':
            # This is a non-terminal expansion, with more than one child node.
            for child_type in right_side[1:-1].split(', '):
                child_node = Tree(child_type, [])
                current_node.append(child_node)  # you add a child to an nltk.Tree with `append`
                # For now, we assume that all children in a list like this are non-terminals, so we
                # recurse on them.  I'm pretty sure that will always be true for the way our
                # grammar induction works.  We can revisit this later if we need to.
                remaining_actions = self._construct_node_from_actions(child_node, remaining_actions)
        else:
            # The current node is a pre-terminal; we'll add a single terminal child.  By
            # construction, the right-hand side of our production rules are only ever terminal
            # productions or lists of non-terminals.
            current_node.append(Tree(right_side, []))  # you add a child to an nltk.Tree with `append`
        return remaining_actions
コード例 #35
0
 def convert(tree: Tree) -> Node:
     root = StanfordNode(tree.label())
     for child in tree: root.append(child if isinstance(child, str) else StanfordParser.convert(child))
     return root
コード例 #36
0
ファイル: FGParser.py プロジェクト: ggosline/taxonparser
def FindNode(lab:str, tree:Tree) -> Tree:
    if tree.label()[TYPE] == lab:
        return tree
    for t in tree:
        if isinstance(t, Tree):
            return FindNode(lab, t)