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)
def translate_it_yo(tree): SUBJ = [Nonterminal("PRON"), Nonterminal("NP"), Nonterminal("N")] VERB = [Nonterminal("VP")] yoda_tree = Tree("Yoda", []) for i in range(len(tree)): if (tree[i].label() in SUBJ): yoda_tree.insert(1, tree[i]) if (tree[i].label() in VERB): V = tree[i][0] X = tree[i][1] yoda_tree.insert(0, X) yoda_tree.insert(2, V) return yoda_tree
def makeConstNode(i, node, constDic, rootindeces, tree): if node["cat"] in constDic: if node["cat"] == "V": if node.get("mode", "") == "infinitive": ntree = Tree("(VP (V " + node["t"] + "))") elif "V" in node["govcats"]: j, func = node["govcats"]["V"][0] if func.startswith("para"): # copy governor's const gov = tree[j] if "const" not in gov: makeConstNode(j, gov, constDic, rootindeces, tree) if unicode( gov["const"])[1] == "V": # governor's const is VP ntree = Tree("(VP (V " + node["t"] + "))") else: # governor's const is S ntree = Tree("(S (V " + node["t"] + "))") else: # verb depends on verb ntree = Tree("(VP (V " + node["t"] + "))") else: ntree = Tree("(S (V " + node["t"] + "))") else: ntree = constDic[node["cat"]].copy(True) ntree.insert(0, Tree("(" + node["cat"] + " " + node["t"] + ")")) else: if node["children"] or i in rootindeces or node["cat"] in node[ "govcats"]: ntree = Tree("(" + node["cat"] + "P (" + node["cat"] + " " + node["t"] + "))") else: ntree = Tree("(" + node["cat"] + " " + node["t"] + ")") ntree.depindex = i ntree.function = unicode(node["gov"].items()[0][1]) #ntree.t=node["t"] node["const"] = ntree for t in list(ntree.subtrees(lambda t: t.height() == 2)): t.depindex = i #t.lexid=node["lexid"] t.lexid = node["id"] t.t = node["t"]
def makeConstNode(i,node,constDic, rootindeces,tree): if node["cat"] in constDic: if node["cat"]=="V": if node.get("mode","")=="infinitive": ntree=Tree("(VP (V "+node["t"]+"))") elif "V" in node["govcats"]: j,func=node["govcats"]["V"][0] if func.startswith("para"): # copy governor's const gov=tree[j] if "const" not in gov: makeConstNode(j,gov,constDic, rootindeces,tree) if unicode(gov["const"])[1]=="V": # governor's const is VP ntree=Tree("(VP (V "+node["t"]+"))") else: # governor's const is S ntree=Tree("(S (V "+node["t"]+"))") else: # verb depends on verb ntree=Tree("(VP (V "+node["t"]+"))") else: ntree=Tree("(S (V "+node["t"]+"))") else: ntree=constDic[node["cat"]].copy(True) ntree.insert(0,Tree("("+node["cat"]+" "+node["t"]+")")) else: if node["children"] or i in rootindeces or node["cat"] in node["govcats"]: ntree=Tree("("+node["cat"]+"P ("+node["cat"]+" "+node["t"]+"))") else: ntree=Tree("("+node["cat"]+" "+node["t"]+")") ntree.depindex=i ntree.function=unicode(node["gov"].items()[0][1]) #ntree.t=node["t"] node["const"]=ntree for t in list(ntree.subtrees(lambda t: t.height() == 2)): t.depindex=i #t.lexid=node["lexid"] t.lexid=node["id"] t.t=node["t"]
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)