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 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)