natural language toolkit text trees c 20012023 nltk project edward loper edlopergmail com steven bird stevenbird1gmail com peter ljunglf peter ljunglofgu se tom aarsen url https www nltk org for license information see license txt parsing use tree reads removeemptytopbracketingtrue instead parse a sinica treebank string and return a tree trees are represented as nested brackettings as shown in the following example x represents a chinese character sgoal nphead nep xxtheme nphead nhaa xquantity dab xhead vl2 x0periodcategory return a tree corresponding to the string representation rtype tree param s the string to be converted type s str s re subr ss s remove leading identifier s re subr w s remove role tags return s natural language toolkit text trees c 2001 2023 nltk project edward loper edloper gmail com steven bird stevenbird1 gmail com peter ljunglöf peter ljunglof gu se tom aarsen url https www nltk org for license information see license txt parsing use tree read s remove_empty_top_bracketing true instead parse a sinica treebank string and return a tree trees are represented as nested brackettings as shown in the following example x represents a chinese character s goal np head nep xx theme np head nhaa x quantity dab x head vl2 x 0 periodcategory return a tree corresponding to the string representation rtype tree param s the string to be converted type s str pull nonterminal inside parens non terminal s re sub r s s s remove leading identifier s re sub r w s remove role tags return s
import re

from nltk.tree.tree import Tree






def bracket_parse(s):
    
    raise NameError("Use Tree.read(s, remove_empty_top_bracketing=True) instead.")


def sinica_parse(s):
    
    tokens = re.split(r"([()| ])", s)
    for i in range(len(tokens)):
        if tokens[i] == "(":
            tokens[i - 1], tokens[i] = (
                tokens[i],
                tokens[i - 1],
            )  
        elif ":" in tokens[i]:
            fields = tokens[i].split(":")
            if len(fields) == 2:  
                tokens[i] = fields[1]
            else:
                tokens[i] = "(" + fields[-2] + " " + fields[-1] + ")"
        elif tokens[i] == "|":
            tokens[i] = ""

    treebank_string = " ".join(tokens)
    return Tree.fromstring(treebank_string, remove_empty_top_bracketing=True)







__all__ = [
    "bracket_parse",
    "sinica_parse",
]