def test_string_roundtrip1(self): t1 = terms.from_string('a(A, A)') t2 = terms.from_string('a(B, B)') self.assertTrue(t1.equivalent(t2)) t1 = t1.to_string() t1 = terms.from_string(t1) self.assertTrue(t1.equivalent(t2))
def test_augment(self): t = terms.from_string('answer(A,longest(A,(river(A),traverse(A,B),state(B),next_to(B,C),most(C,D,(state(C),next_to(C,D),state(D))))))') u = t.augment() self.assertEqual(u.to_string(), 'answer(A,longest_1(A,(river_1(A),traverse_1(A,B),state_1(B),next_to_1(B,C),most_1(C,D,(state_2(C),next_to_2(C,D),state_3(D))))))') t = terms.from_string('answer(A,lowest(B,(state(A),traverse(C,A),const(C,riverid(mississippi)),loc(B,A),place(B))))') u = t.augment() self.assertEqual(u.to_string(), 'answer(A,lowest_1(B,(state_1(A),traverse_1(C,A),const_1(C,riverid(mississippi)),loc_1(B,A),place_1(B))))')
def test_replace(self): before = terms.from_string('a(A, B, (C, A))') after = terms.from_string('a(X, X, (Y, X))') self.assertFalse(before.equivalent(after)) A = before.args[0] B = before.args[1] now = before.replace(A, B) self.assertTrue(now.equivalent(after))
def test_marked_terms(self): t1 = terms.from_string('a(A)') t2 = terms.from_string('b(B)') t3 = terms.ConjunctiveTerm((t1, t2)) marked_terms = () self.assertEqual(t3.to_string(marked_terms=marked_terms), '(a(A),b(B))') marked_terms = (t1,) self.assertEqual(t3.to_string(marked_terms=marked_terms), '([0]a(A),b(B))') marked_terms = (t2,) self.assertEqual(t3.to_string(marked_terms=marked_terms), '(a(A),[0]b(B))') marked_terms = (t2,t1) self.assertEqual(t3.to_string(marked_terms=marked_terms), '([1]a(A),[0]b(B))')
def test_fragments1(self): term = terms.from_string('(a,b,c,d,e)') gold = ['a', '(a,b)', '(a,b,c)', '(a,b,c,d)', '(a,b,c,d,e)', 'b', '(b,c)', '(b,c,d)', '(b,c,d,e)', 'c', '(c,d)', '(c,d,e)', 'd', '(d,e)', 'e'] pred = [f.to_string() for f in term.fragments()] self.assertEqual(pred, gold)
def test_augment1(self): lex = lexicon.read_lexicon('lexicon.txt') t = terms.from_string( 'answer(A,longest(A,(river(A),traverse(A,B),state(B),next_to(B,C),most(C,D,(state(C),next_to(C,D),state(D))))))' ) alex = augment.AugmentingLexicon(lex, t) word = ('longest', ) meanings = [m.to_string() for m in alex.meanings(word)] self.assertEqual(meanings, ['longest_1(A,B)']) word = ('river', ) meanings = [m.to_string() for m in alex.meanings(word)] self.assertEqual(meanings, ['river_1(A)']) word = ('passes', ) meanings = [m.to_string() for m in alex.meanings(word)] self.assertEqual(meanings, ['traverse_1(A,B)']) word = ('states', ) meanings = [m.to_string() for m in alex.meanings(word)] self.assertEqual(meanings, ['state_1(A)', 'state_2(A)', 'state_3(A)']) word = ('border', ) meanings = [m.to_string() for m in alex.meanings(word)] self.assertEqual(meanings, ['next_to_1(A,B)', 'next_to_2(A,B)']) word = ('state', ) meanings = [m.to_string() for m in alex.meanings(word)] self.assertEqual(meanings, ['state_1(A)', 'state_2(A)', 'state_3(A)']) word = ('borders', ) meanings = [m.to_string() for m in alex.meanings(word)] self.assertEqual(meanings, ['next_to_1(A,B)', 'next_to_2(A,B)']) word = ('most', ) meanings = [m.to_string() for m in alex.meanings(word)] self.assertEqual(meanings, ['most_1(A,B,C)'])
def unaugment(action): if action[0] == 'shift': lst = terms.from_string(action[2]) if isinstance(lst, terms.ComplexTerm): lst.functor_name = augment.unaugment(lst.functor_name) action = (action[0], action[1], lst.to_string()) return action
def test_example1(self): words = ('what', 'is', 'the', 'capital', 'of', 'the', 'state', 'with', 'the', 'largest', 'population') target_mr = terms.from_string( 'answer(C, (capital(S, C), largest(P, (state(S), population(S, P)))))' ) actions_gold = [ ('skip', ), ('skip', ), ('skip', ), ('shift', 1, 'capital_1(A,B)'), ('coref', 0, 1, 0, 2), ('drop', 2), ('skip', ), ('skip', ), ('shift', 1, 'state_1(A)'), ('coref', 0, 1, 0, 1), ('skip', ), ('skip', ), ('shift', 1, 'largest_1(A,B)'), ('lift', 2), ('shift', 1, 'population_1(A,B)'), ('coref', 0, 1, 0, 1), ('coref', 1, 1, 0, 2), ('sdrop', ), ('pop', ), ('pop', ), ('sdrop', ), ('pop', ), ('pop', ), ('finish', ), ] actions_oracle = oracle.action_sequence(words, target_mr) self.assertEqual(actions_gold, actions_oracle)
def test_example3(self): words = ('how', 'many', 'big', 'cities', 'are', 'in', 'pennsylvania', '?') actions = [ ('skip', ), ('shift', 1, 'count_1(A,B,C)'), ('coref', 0, 1, 0, 3), ('drop', 2), ('shift', 1, 'major_1(A)'), ('coref', 0, 1, 0, 1), ('drop', 2), ('shift', 1, 'city_1(A)'), ('coref', 0, 1, 0, 1), ('sdrop', ), ('skip', ), ('shift', 1, 'loc_1(A,B)'), ('coref', 0, 1, 0, 1), ('sdrop', ), ('shift', 1, 'const_1(A,stateid(pennsylvania))'), ('coref', 0, 2, 0, 1), ('sdrop', ), ('skip', ), ('pop', ), ('pop', ), ('pop', ), ('pop', ), ('pop', ), ('finish', ), ('idle', ), ] target_mr = terms.from_string( 'answer(A,count(B,(major(B),city(B),loc(B,C),const(C,stateid(pennsylvania))),A))' ) self._test_action_sequence(words, actions, target_mr)
def read_geoquery_file(path): result = [] with open(path) as f: for line in f: term = terms.from_string(line) words = [str(w) for w in term.args[0].elements] mr = term.args[1] result.append((words, mr)) return result
def test_example7(self): words = ('what', 'are', 'the', 'major', 'cities', 'in', 'the', 'states', 'through', 'which', 'the', 'major', 'river', 'in', 'virginia', 'runs', '?') target_mr = terms.from_string( 'answer(A,(major(A),city(A),loc(A,B),state(B),river(C),loc(C,D),const(D,stateid(virginia)),traverse(C,B)))' ) actions = [ ('skip', ), ('skip', ), ('skip', ), ('shift', 1, 'major_1(A)'), ('coref', 0, 1, 0, 1), ('drop', 2), ('shift', 1, 'city_1(A)'), ('coref', 1, 1, 0, 1), ('sdrop', ), ('shift', 1, 'loc_1(A,B)'), ('coref', 0, 1, 0, 1), ('sdrop', ), ('skip', ), ('shift', 1, 'state_1(A)'), ('coref', 0, 2, 0, 1), ('sdrop', ), ('skip', ), ('skip', ), ('skip', ), ('skip', ), ('shift', 1, 'river_1(A)'), ('sdrop', ), ('shift', 1, 'loc_2(A,B)'), ('coref', 0, 1, 0, 1), ('sdrop', ), ('shift', 1, 'const_1(A,stateid(virginia))'), ('coref', 0, 2, 0, 1), ('sdrop', ), ('shift', 1, 'traverse_1(A,B)'), ('pop', ), ('coref', 0, 1, 0, 1), ('pop', ), ('pop', ), ('coref', 0, 1, 0, 2), ('sdrop', ), ('skip', ), ('pop', ), ('pop', ), ('pop', ), ('pop', ), ('pop', ), ('finish', ), ('idle', ), ] self._test_action_sequence(words, actions, target_mr)
def test_address_conjunctive(self): t = terms.from_string('(capital(S, C), largest(P, (state(S), population(S, P))))') s = t.at_address(()) self.assertTrue(s.equivalent(t)) s = t.at_address((1,)) self.assertTrue(s.equivalent(terms.from_string('capital(S, C)'))) s = t.at_address((2,)) self.assertTrue(s.equivalent(terms.from_string('largest(P, (state(S), population(S, P)))'))) s = t.at_address((2, 1, 1)) self.assertTrue(s.equivalent(terms.from_string('P'))) s = t.at_address((2, 2, 1)) self.assertTrue(s.equivalent(terms.from_string('state(S)'))) s = t.at_address((2, 2, 1, 1, 1)) self.assertTrue(s.equivalent(terms.from_string('S'))) s = t.at_address((2, 2, 2)) self.assertTrue(s.equivalent(terms.from_string('population(S, P)'))) s = t.at_address((2, 2, 2, 1, 1)) self.assertTrue(s.equivalent(terms.from_string('S'))) s = t.at_address((2, 2, 2, 2, 1)) self.assertTrue(s.equivalent(terms.from_string('P')))
def test_example4(self): words = ('how', 'many', 'rivers', 'do', 'not', 'traverse', 'the', 'state', 'with', 'the', 'capital', 'albany', '?') target_mr = terms.from_string( 'answer(A,count(B,(river(B),\+ (traverse(B,C),state(C),loc(D,C),capital(D),const(D,cityid(albany,_)))),A))' ) actions = [ ('skip', ), ('shift', 1, 'count_1(A,B,C)'), ('coref', 0, 1, 0, 3), ('drop', 2), ('shift', 1, 'river_1(A)'), ('coref', 0, 1, 0, 1), ('drop', 2), ('skip', ), ('shift', 1, '\+_1A'), ('sdrop', ), ('shift', 1, 'traverse_1(A,B)'), ('coref', 1, 1, 0, 1), ('drop', 1), ('skip', ), ('shift', 1, 'state_1(A)'), ('coref', 0, 2, 0, 1), ('sdrop', ), ('shift', 1, 'loc_1(A,B)'), ('coref', 0, 1, 0, 2), ('sdrop', ), ('skip', ), ('shift', 1, 'capital_1(A)'), ('coref', 0, 1, 0, 1), ('sdrop', ), ('shift', 1, 'const_1(A,cityid(albany,B))'), ('coref', 0, 1, 0, 1), ('sdrop', ), ('skip', ), ('pop', ), ('pop', ), ('pop', ), ('pop', ), ('pop', ), ('pop', ), ('pop', ), ('pop', ), ('finish', ), ('idle', ), ] self._test_action_sequence(words, actions, target_mr)
def test_example8(self): words = 'what texas city has the largest population ?'.split() target_mr = terms.from_string( 'answer(A,largest(B,(const(C,stateid(texas)),city(A),loc(A,C),population(A,B)))))' ) actions = [ ('skip', ), ('shift', 1, 'const_1(A,stateid(texas))'), ('shift', 1, 'city_(A)'), ('sdrop', ), ('coref', 1, 1, 0, 2), ('coref', 0, 1, 0, 1), ('pop', ), ('pop', ), ('sdrop', ), ] # FIXME But what now! self._test_action_sequence(words, actions, target_mr)
def test_example6(self): words = ('of', 'the', 'states', 'washed', 'by', 'the', 'mississippi', 'river', 'which', 'has', 'the', 'lowest', 'point', '?') target_mr = terms.from_string( 'answer(A,lowest(B,(state(A),traverse(C,A),const(C,riverid(mississippi)),loc(B,A),place(B))))' ) actions = [ ('skip', ), ('skip', ), ('shift', 1, 'state_1(A)'), ('coref', 0, 1, 0, 1), ('shift', 1, 'traverse_1(A,B)'), ('coref', 0, 1, 0, 2), ('sdrop', ), ('skip', ), ('skip', ), ('shift', 1, "const_1(A,riverid(mississippi))"), ('coref', 0, 1, 0, 1), ('sdrop', ), ('skip', ), ('skip', ), ('shift', 1, 'loc_1(A,B)'), ('pop', ), ('pop', ), ('coref', 0, 1, 0, 2), ('sdrop', ), ('skip', ), ('shift', 1, 'lowest_1(A,B)'), ('coref', 0, 1, 0, 1), ('pop', ), ('lift', 2), ('shift', 1, 'place_1(A)'), ('coref', 1, 1, 0, 1), ('sdrop', ), ('pop', ), ('pop', ), ('drop', 2), ('pop', ), ('skip', ), ('finish', ), ('idle', ), ] self._test_action_sequence(words, actions, target_mr)
def test_example8(self): words = 'which is the lowest point of the states that the mississippi runs through ?'.split( ) target_mr = terms.from_string( 'answer(A,lowest(B,(place(B),loc(B,A),state(A),const(C,riverid(mississippi)),traverse(C,A))))' ) actions = [ ('skip', ), ('skip', ), ('skip', ), ('shift', 1, 'lowest_1(A,B)'), ('drop', 2), ('shift', 1, 'place_1(A)'), ('coref', 0, 1, 0, 1), ('drop', 2), ('shift', 1, 'loc_1(A,B)'), ('coref', 0, 1, 0, 1), ('coref', 2, 1, 0, 2), ('sdrop', ), ('skip', ), ('shift', 1, 'state_1(A)'), ('coref', 0, 2, 0, 1), ('sdrop', ), ('skip', ), ('skip', ), ('shift', 1, 'const_1(A,riverid(mississippi))'), ('sdrop', ), ('shift', 1, 'traverse_1(A,B)'), ('coref', 0, 1, 0, 1), ('pop', ), ('coref', 0, 1, 0, 2), ('sdrop', ), ('skip', ), ('skip', ), ('pop', ), ('pop', ), ('pop', ), ('pop', ), ('pop', ), ('finish', ), ('idle', ), ] self._test_action_sequence(words, actions, target_mr)
def test_example2(self): words = ('could', 'you', 'tell', 'me', 'what', 'is', 'the', 'highest', 'point', 'in', 'the', 'state', 'of', 'oregon', '?') target_mr = terms.from_string( 'answer(A,highest(A,(place(A),loc(A,B),state(B),const(B,stateid(oregon)))))' ) actions = [ ('skip', ), ('skip', ), ('skip', ), ('skip', ), ('skip', ), ('skip', ), ('skip', ), ('shift', 1, 'highest_1(A,B)'), ('coref', 0, 1, 0, 1), ('drop', 2), ('shift', 1, 'place_1(A)'), ('coref', 0, 1, 0, 1), ('drop', 2), ('shift', 1, 'loc_1(A,B)'), ('coref', 0, 1, 0, 1), ('sdrop', ), ('skip', ), ('shift', 1, 'state_1(A)'), ('coref', 0, 2, 0, 1), ('sdrop', ), ('skip', ), ('shift', 1, 'const_1(A,stateid(oregon))'), ('coref', 0, 1, 0, 1), ('sdrop', ), ('skip', ), ('pop', ), ('pop', ), ('pop', ), ('pop', ), ('pop', ), ('finish', ), ('idle', ), ] self._test_action_sequence(words, actions, target_mr)
def test_augment2(self): lex = lexicon.read_lexicon('lexicon.txt') t = terms.from_string( 'answer(A,lowest(B,(state(A),traverse(C,A),const(C,riverid(mississippi)),loc(B,A),place(B))))' ) alex = augment.AugmentingLexicon(lex, t) word = ('states', ) meanings = [m.to_string() for m in alex.meanings(word)] self.assertEqual(meanings, ['state_1(A)']) word = ('washed', ) meanings = [m.to_string() for m in alex.meanings(word)] self.assertEqual(meanings, ['traverse_1(A,B)']) word = ('mississippi', ) meanings = [m.to_string() for m in alex.meanings(word)] self.assertEqual(meanings, ['const_1(A,riverid(mississippi))']) word = ('has', ) meanings = [m.to_string() for m in alex.meanings(word)] self.assertEqual(meanings, ['loc_1(A,B)']) word = ('lowest', ) meanings = [m.to_string() for m in alex.meanings(word)] self.assertEqual(meanings, ['lowest_1(A,B)']) word = ('point', ) meanings = [m.to_string() for m in alex.meanings(word)] self.assertEqual(meanings, ['place_1(A)'])
def test_example5(self): words = ('in', 'what', 'state', 'is', 'mount', 'mckinley', '?') target_mr = terms.from_string( "answer(A,(loc(B,A),state(A),const(B,placeid('mount mckinley'))))") actions = [ ('shift', 1, 'loc_1(A,B)'), ('coref', 0, 1, 0, 2), ('drop', 2), ('skip', ), ('shift', 1, 'state_1(A)'), ('coref', 0, 2, 0, 1), ('sdrop', ), ('skip', ), ('shift', 2, "const_1(A,placeid('mount mckinley'))"), ('pop', ), ('coref', 0, 1, 0, 1), ('sdrop', ), ('skip', ), ('pop', ), ('pop', ), ('finish', ), ('idle', ), ] self._test_action_sequence(words, actions, target_mr)
def test_subterms(self): term = terms.from_string( 'answer(C, (capital(S, C), largest(P, (state(S), population(S, P)))))') subterms = list(term.subterms()) self.assertEqual(len(subterms), 14)
def test_equivalent(self): self.assertTrue( terms.from_string('X').equivalent( terms.from_string('Y'))) self.assertFalse( terms.from_string('X').equivalent( terms.from_string('a'))) self.assertFalse( terms.from_string('a').equivalent( terms.from_string('X'))) self.assertTrue( terms.from_string('a').equivalent( terms.from_string('a'))) self.assertFalse( terms.from_string('a').equivalent( terms.from_string('b'))) self.assertFalse( terms.from_string('a(A, B)').equivalent( terms.from_string('a(C, C)'))) self.assertFalse( terms.from_string('a(C, C)').equivalent( terms.from_string('a(A, B)'))) self.assertFalse( terms.from_string('a(A, (b(B), C))').equivalent( terms.from_string('a(X, (b(X), c(X)))'))) self.assertFalse( terms.from_string('a(X, (b(X), c(X)))').equivalent( terms.from_string('a(D, (b(D), C))'))) self.assertTrue( terms.from_string('a(A, (b(B), C))').equivalent( terms.from_string('a(D, (b(E), F))'))) self.assertFalse( terms.from_string('answer(A,(capital(B,A),largest(C,(state(A),population(D,E)))))').equivalent( terms.from_string('answer(C,(capital(S,C),largest(P,(state(S),population(S,P)))))')))
def test_subsumes(self): self.assertTrue( terms.from_string('X').subsumes( terms.from_string('Y'))) self.assertTrue( terms.from_string('X').subsumes( terms.from_string('a'))) self.assertFalse( terms.from_string('a').subsumes( terms.from_string('X'))) self.assertTrue( terms.from_string('a').subsumes( terms.from_string('a'))) self.assertFalse( terms.from_string('a').subsumes( terms.from_string('b'))) self.assertTrue( terms.from_string('a(A, B)').subsumes( terms.from_string('a(C, C)'))) self.assertFalse( terms.from_string('a(C, C)').subsumes( terms.from_string('a(A, B)'))) self.assertTrue( terms.from_string('a(A, (b(B), C))').subsumes( terms.from_string('a(X, (b(X), c(X)))'))) self.assertFalse( terms.from_string('a(X, (b(X), c(X)))').subsumes( terms.from_string('a(D, (b(D), C))'))) self.assertFalse( terms.from_string('a(A)').subsumes( terms.from_string('(a(A), b(B))'))) self.assertFalse( terms.from_string('(a(A), b(B))').subsumes( terms.from_string('(a(A), b(B), c(C))'))) self.assertFalse( terms.from_string('(a(A), b(B))').subsumes( terms.from_string('(a(A), c(B))'))) self.assertFalse( terms.from_string('(a(A), b(B))').subsumes( terms.from_string('a(A)'))) self.assertFalse( terms.from_string('answer(A,(capital(B,A),largest(C,(state(A),population(D,E)))))').subsumes( terms.from_string('answer(C,(capital(S,C),largest(P,(state(S),population(S,P)))))'))) self.assertFalse( terms.from_string('answer(A,(state(A),population(D,E)))').subsumes( terms.from_string('answer(C,(state(S),population(S,P)))')))
def __init__(self, lex, target_mr): self.lex = lex mr = target_mr.augment() self.lst = list(lexicon.lexical_subterms(mr)) self.lst = [terms.from_string(l.to_string()) for l in self.lst] # undo variable bindings
def test_fragments5(self): target = terms.from_string('answer(A,lowest(B,(state(A),traverse(C,A),const(C,riverid(mississippi)),loc(B,A),place(B))))') subterm = terms.from_string('lowest(C,(traverse(D,A),const(D,riverid(mississippi)),loc(C,E)))') self.assertTrue(any(subterm.subsumes(f) for s in target.subterms() for f in s.fragments()))
def test_fragments5(self): term = terms.from_string('a(A,(b,c))') gold = ['a(A,B)', 'a(A,b)', 'a(A,(b,c))', 'a(A,c)'] pred = [f.to_string() for f in term.fragments()] self.assertEqual(pred, gold)
def test_string_roundtrip2(self): t1 = terms.from_string('\+_1a(b)') self.assertEqual(t1.to_string(), '\+_1a(b)')
import augment import fileinput import json import oracle import sys import terms def unaugment(action): if action[0] == 'shift': lst = terms.from_string(action[2]) if isinstance(lst, terms.ComplexTerm): lst.functor_name = augment.unaugment(lst.functor_name) action = (action[0], action[1], lst.to_string()) return action if __name__ == '__main__': for line in fileinput.input(): term = terms.from_string(line) words = tuple(str(w) for w in term.args[0].elements) mr = term.args[1] try: actions = oracle.action_sequence(words, mr) except: print('FAILED:', words) sys.exit(1) print(json.dumps({'words': words, 'actions': [unaugment(a) for a in actions]}))
def meanings(self, word): """Returns the known meanings of a word. """ if word in self.word_term_map: return (terms.from_string(s) for s in self.word_term_map[word]) return ()
def test_fragments2(self): term = terms.from_string('a(a(d),(b(e),c(f)))') gold = ['a(A,B)', 'a(A,b(e))', 'a(A,(b(e),c(f)))', 'a(A,c(f))', 'a(a(d),A)', 'a(a(d),b(e))', 'a(a(d),(b(e),c(f)))', 'a(a(d),c(f))'] pred = [f.to_string() for f in term.fragments()] self.assertEqual(pred, gold)