def test_betaARBvx(): run = s.get_semtree('alphanx0V', 'run', lemma='run') dog = s.get_semtree('alphaNXN', 'dog') obv = s.get_semtree('betaARBvx', 'obviously') run = run.substitute(dog, "NP_0") run = run.adjoin(obv, "VP") sem = SemanticParser.parse("motion(during(e1),x), Theme(e1,x), ISA(x,DOG), obviously(e1)") assert run.full_semantics().equiv(sem)
def test_alphanx0Vnx1(): chase = s.get_semtree('alphanx0Vnx1', 'chase', lemma='chase') cat = s.get_semtree('alphaNXN', 'cat') dog = s.get_semtree('alphaNXN', 'dog') chase = chase.substitute(cat, 'NP_0') chase = chase.substitute(dog, 'NP_1') sem = SemanticParser.parse("motion(during(e1),x0), motion(during(e1),x1), Agent(e1,x0), ISA(x0,CAT), Theme(e1,x1), ISA(x1,DOG)") assert chase.full_semantics().equiv(sem)
def test_betanxGnx(): dog = s.get_semtree('alphaNXN', 'dog') john = s.get_semtree('alphaNXN', 'John') poss = s.get_semtree('betanxGnx', '\'s') dog = dog.adjoin(poss, "NP") dog = dog.substitute(john, "NP-1") sem = SemanticParser.parse("belongs_to(x1,x2), ISA(x2,JOHN), ISA(x1,DOG)") assert dog.full_semantics().equiv(sem)
def test_betaVs(): run = s.get_semtree('alphanx0V', 'run', lemma='run') dog = s.get_semtree('alphaNXN', 'dog') does = s.get_semtree('betaPUs', 'does') run = run.substitute(dog, "NP_0") run = run.adjoin(does, "S_r") sem = SemanticParser.parse("motion(during(e1),x), Theme(e1,x), ISA(x,DOG)") assert run.full_semantics().equiv(sem)
def get_nonverb_tree_family(self, tree_name, anchor): annotations = { 'Ts0N1': { 'np_var_order': ['e1'], 'sem_dict': {'Event': 'ISA(e1, %s)' % anchor.upper()}, 'event': 'e1', }, 'Tnx0Pnx1': { 'np_var_order': ['a1', 'l1'], 'sem_dict': {'Event': '%s(e1, a1, l1)' % anchor, 'a1': 'Agent(e1,a1)', 'l1': 'Location(e1, l1)'}, 'event': 'e1', }, 'Tnx0N1': { 'np_var_order': ['a1'], 'sem_dict': {'Event': 'Agent(e1,a1)', 'a1': 'ISA(a1, %s)' % anchor.upper()}, 'event': 'e1', }, 'Tnx0BEnx1': { 'np_var_order': ['a1', 'l1'], 'sem_dict': {'Event': 'Agent(e1,a1)', 'l1': '', 'a1': ''}, 'event': 'e1', }, 'Tnx0Ax1': { 'np_var_order': ['a1',], 'sem_dict': {'Event': 'Agent(e1,a1)', 'a1': 'ISA(a1, %s)' % anchor.upper()}, 'event': 'e1', }, 'Ts0Ax1': { 'np_var_order': ['e1',], 'sem_dict': {'Event': 'ISA(e1, %s)' % anchor.upper()}, 'event': 'e1', }, 'Ts0Pnx1': { 'np_var_order': ['e1', 'l1'], 'sem_dict': {'Event': '%s(e1, l1)' % anchor, 'l1': 'Theme(e1, l1)'}, 'event': 'e1', }, } tree = self.grammar.get(tree_name, copy=True) np_var_order = annotations[tree.tree_family]['np_var_order'] sem_dict = annotations[tree.tree_family]['sem_dict'] np_var_order = [VariableParser.parse(v)[0] for v in np_var_order] for v in np_var_order: if v.name == annotations[tree.tree_family]['event']: v.arg_type = 'Event' for var_name, sem_str in sem_dict.items(): sem_dict[var_name] = SemanticParser.parse(sem_str) for v in sem_dict['Event'].variables(): if v.name == annotations[tree.tree_family]['event']: v.arg_type = 'Event' return [self.add_semantics(tree, anchor, np_var_order, sem_dict)]
def test_betanxPUnx(): run = s.get_semtree('alphanx0V', 'run', lemma='run') dog = s.get_semtree('alphaNXN', 'dog') cat = s.get_semtree('alphaNXN', 'cat') comma = s.get_semtree('betanxPUnx', ',') run = run.substitute(dog, "NP_0") run = run.adjoin(comma, "NP_0") run = run.substitute(cat, "NP") sem = SemanticParser.parse("motion(during(e1),x), equal(x,x1), Theme(e1,x), ISA(x,DOG), ISA(x1,CAT)") assert run.full_semantics().equiv(sem)
def test_betavxPnx(): behind = s.get_semtree('betavxPnx', 'behind') run = s.get_semtree('alphanx0V', 'run', lemma='run') dog = s.get_semtree('alphaNXN', 'dog') cat = s.get_semtree('alphaNXN', 'cat') run = run.substitute(dog, 'NP_0') run = run.adjoin(behind, 'VP') run = run.substitute(cat, 'NP') sem = SemanticParser.parse("motion(during(e1),x0), Theme(e1,x0), ISA(x0,DOG), behind(e1,x1), ISA(x1,CAT)") assert run.full_semantics().equiv(sem)
def test_betanx1CONJnx2(): run = s.get_semtree('alphanx0V', 'run', lemma='run') dog = s.get_semtree('alphaNXN', 'dog') cat = s.get_semtree('alphaNXN', 'cat') conj = s.get_semtree('betanx1CONJnx2', 'and') run = run.substitute(dog, 'NP_0') run = run.adjoin(conj, "NP_0") run = run.substitute(cat, "NP_2") sem = SemanticParser.parse("motion(e1,AND(x1,x2)), Theme(e1,AND(x1,x2)), ISA(x1,DOG), ISA(x2,CAT)") assert run.full_semantics().equiv(sem)
def test_betavxPs(): because = s.get_semtree('betavxPs', 'because') run = s.get_semtree('alphanx0V', 'run', lemma='run') dog = s.get_semtree('alphaNXN', 'dog') run2 = s.get_semtree('alphanx0V', 'run', lemma='run') cat = s.get_semtree('alphaNXN', 'cat') run = run.substitute(dog, "NP_0") run2 = run2.substitute(cat, "NP_0") run = run.adjoin(because, "VP") run = run.substitute(run2, "S") sem = SemanticParser.parse("motion(e1,x1), Theme(e1,x1), ISA(x1,DOG), because(e1,e2), motion(e2,x2), Theme(e2,x2), ISA(x2,CAT)") assert run.full_semantics().equiv(sem)
def test_betasPUs(): semi = s.get_semtree('betasPUs', ';') run = s.get_semtree('alphanx0V', 'run', lemma='run') dog = s.get_semtree('alphaNXN', 'dog') run2 = s.get_semtree('alphanx0V', 'run', lemma='run') cat = s.get_semtree('alphaNXN', 'cat') run = run.substitute(dog, "NP_0") run2 = run2.substitute(cat, "NP_0") run = run.adjoin(semi, 'S_r') run = run.substitute(run2, 'S_1') assert str(run.sem_var) == "AND(e1,e2)" sem = SemanticParser.parse("motion(during(e1),x), Theme(e1,x), ISA(x,DOG), motion(during(e2),x1), Theme(e2,x1), ISA(x1,CAT)") assert run.full_semantics().equiv(sem)
def get_nonverb_semtree(self, tree_name, anchor): """ Returns the semantically annotated tree specified by tree_name anchored by anchor. Note: the semantic info should eventually live somewhere that is more flexible (json, xml, etc) """ tree = self.grammar.get(tree_name) tree.lexicalize(anchor) tree = SemTree.convert(tree) ''' if tree.initial(): v = VariableFactory.get_var(pre=anchor[0]) else: v = VariableFactory.get_var() ''' sem_map = { "alphaNXN": { "NP": (lambda a: "", "x1"), "N": (lambda a: "ISA(x1, %s)" % a.upper(), "x1"), }, "betaAn": { "N_r": (lambda a: "ISA(x1, %s)" % a.upper(), "x1"), "N_f": (lambda a: "", "x1"), }, "betaNn": { "N_r": (lambda a: "ISA(x1, %s)" % a.upper(), "x1"), "N_f": (lambda a: "", "x1"), }, ("betaDnx", "a"): { "NP_r": (lambda a: "EXISTS(x1)|", "x1"), "NP_f": (lambda a: "", "x1"), }, ("betaDnx", "an"): { "NP_r": (lambda a: "EXISTS(x1)|", "x1"), "NP_f": (lambda a: "", "x1"), }, ("betaDnx", "the"): { "NP_r": (lambda a: "EXISTS(x1)|", "x1"), "NP_f": (lambda a: "", "x1"), }, ("betaDnx", "one"): { "NP_r": (lambda a: "EXISTS(x1)|", "x1"), "NP_f": (lambda a: "", "x1"), }, "betaVvx": { "VP_r": (lambda a: "", "x1"), "VP": (lambda a: "", "x1"), }, "betanxPnx": { "NP_r": (lambda a: "%s(x1, y1)" % a, "x1"), "NP": (lambda a: "", "y1"), "NP_f": (lambda a: "", "x1"), }, "betavxPnx": { "VP_r": (lambda a: "%s(x1, y1)" % a, "x1"), "NP": (lambda a: "", "y1"), "VP": (lambda a: "", "x1"), }, "betasPUs": { "S_r": (lambda a: "", "AND(x1,y1)"), "S_f": (lambda a: "", "x1"), "S_1": (lambda a: "", "y1"), }, "betaARBvx": { "VP": (lambda a: "", "x1"), "VP_r": (lambda a: "%s(x1)" % a, "x1"), }, "betanxPUnx": { "NP_f": (lambda a: "", "x1"), "NP_r": (lambda a: "equal(x1,y1)", "x1"), "NP": (lambda a: "", "y1"), }, "betaPUs": { "S_r": (lambda a: "", "x1"), "S": (lambda a: "", "x1"), }, "betaVs": { "S_r": (lambda a: "", "x1"), "S": (lambda a: "", "x1"), }, "betanx1CONJnx2": { "NP": (lambda a: "", "AND(x1,y1)"), "NP_1": (lambda a: "", "x1"), "NP_2": (lambda a: "", "y1"), }, "betanxGnx": { "NP_r": (lambda a: "EXISTS(x1)|belongs_to(x1,y1)", "x1"), "NP_f": (lambda a: "", "x1"), "NP": (lambda a: "", "y1"), }, "betavxPs": { "VP_r": (lambda a: "", "x1"), "VP_f": (lambda a: "", "x1"), "PP": (lambda a: "%s(x1, y1)" % a, "y1"), "S": (lambda a: "", "y1"), }, "betas1CONJs2": { "S": (lambda a: "", "AND(x1,y1)"), "S_1": (lambda a: "", "x1"), "S_2": (lambda a: "", "y1"), }, "betaARBs": { "S": (lambda a: "", "x1"), "S_r": (lambda a: "%s(x1)" % a, "x1"), }, "betaCONJs": { "S_c": (lambda a: "", "x1"), "S_r": (lambda a: "", "x1"), }, } if (tree_name, anchor) in sem_map: key = (tree_name, anchor) elif tree_name in sem_map: key = tree_name else: #print(tree_name, anchor) raise NotImplementedError node_dict = sem_map[key] for node_label, (sem_str, var_str) in node_dict.items(): sem_str = sem_str(anchor) node = tree.find(node_label) node.semantics = SemanticParser.parse(sem_str) var, rest = VariableParser.parse(var_str) node.sem_var = var return tree