def testBasicNamedEntityChildParent(self): tagList = ['LOCATION', 'PERSON', 'NUMBER', 'MONEY', 'MISC'] for tag in tagList: parent = DependenciesTree('parent', 1, namedEntityTag = tag) child = DependenciesTree('child', 2, parent = parent, namedEntityTag = tag) parent.child.append(child) child.dependency = 'conj_and' NamedEntityMerging(parent).merge() self.assertEqual(parent.wordList, [Word('parent', 1)]) self.assertEqual(parent.child, [child]) self.assertEqual(child.parent, parent) child.dependency = 'foo' NamedEntityMerging(parent).merge() self.assertIn(Word('parent', 1), parent.wordList) self.assertIn(Word('child', 2), parent.wordList) self.assertEqual(parent.child, []) for (tag1, tag2) in itertools.permutations(tagList, 2): parent = DependenciesTree('parent', 1, namedEntityTag = tag1) child = DependenciesTree('child', 2, parent = parent, namedEntityTag = tag2) parent.child.append(child) child.dependency = 'conj_and' NamedEntityMerging(parent).merge() self.assertEqual(parent.wordList, [Word('parent', 1)]) self.assertEqual(parent.child, [child]) self.assertEqual(child.parent, parent) child.dependency = 'foo' NamedEntityMerging(parent).merge() self.assertEqual(parent.wordList, [Word('parent', 1)]) self.assertEqual(parent.child, [child]) self.assertEqual(child.parent, parent)
def testNoQW(self): tree = computeTree(data.birth_date()) NamedEntityMerging(tree).merge() PrepositionMerging(tree).merge() simplify(tree) tree.sort() root = tree # Root self.assertEqual(root.wordList, [Word("ROOT", 0)]) self.assertEqual(root.namedEntityTag, 'undef') self.assertEqual(root.dependency, 'R0') self.assertEqual(root.parent, None) self.assertEqual(len(root.child), 1) self.assertEqual(root.subtreeType, 'undef') self.assertEqual(root.dfsTag, 0) # president president = root.child[0] self.assertEqual(president.wordList, [Word("President", 1, 'NNP')]) self.assertEqual(president.namedEntityTag, 'undef') self.assertEqual(president.dependency, 'R0') self.assertEqual(president.parent, root) self.assertEqual(len(president.child), 1) self.assertEqual(president.subtreeType, 'undef') self.assertEqual(president.dfsTag, 0) # france france = president.child[0] self.assertEqual(france.wordList, [Word("France", 3, 'NNP')]) self.assertEqual(france.namedEntityTag, 'LOCATION') self.assertEqual(france.dependency, 'R2') self.assertEqual(france.parent, president) self.assertEqual(len(france.child), 0) self.assertEqual(france.subtreeType, 'undef') self.assertEqual(france.dfsTag, 0)
def testQuestionInfo(self): tree = computeTree(data.birth_place()) NamedEntityMerging(tree).merge() PrepositionMerging(tree).merge() simplify(tree) tree.sort() root = tree # Root self.assertEqual(root.wordList, [Word("ROOT", 0)]) self.assertEqual(root.namedEntityTag, 'undef') self.assertEqual(root.dependency, 'R0') self.assertEqual(root.parent, None) self.assertEqual(len(root.child), 1) self.assertEqual(root.subtreeType, 'DATE') self.assertEqual(root.dfsTag, 0) # birth date birth = root.child[0] self.assertEqual(birth.wordList, [Word("born", 3, 'VBN')]) self.assertEqual(birth.namedEntityTag, 'undef') self.assertEqual(birth.dependency, 'R0') self.assertEqual(birth.parent, root) self.assertEqual(len(birth.child), 1) self.assertEqual(birth.subtreeType, 'DATE') self.assertEqual(birth.dfsTag, 0) # obama obama = birth.child[0] self.assertEqual(obama.wordList, [Word("Obama", 4, 'NNP')]) self.assertEqual(obama.namedEntityTag, 'PERSON') self.assertEqual(obama.dependency, 'R3') self.assertEqual(obama.parent, birth) self.assertEqual(len(obama.child), 0) self.assertEqual(obama.subtreeType, 'undef') self.assertEqual(obama.dfsTag, 0)
def testStr2(self): tree=computeTree(data.give_john_smith()) NamedEntityMerging(tree).merge() PrepositionMerging(tree).merge() self.maxDiff=None tree.sort() self.assertEqual(str(tree), data.give_john_smith_stringMerge())
def testnormalFormProductionSuperl2(self): tree = computeTree(data.car()) NamedEntityMerging(tree).merge() PrepositionMerging(tree).merge() qw = simplify(tree) result = normalFormProduction(tree, qw) self.assertEqual( result, { "list": { "list": { "subject": { "value": "world", "type": "resource" }, "predicate": { "value": "car", "type": "resource" }, "object": { "type": "missing" }, "type": "triple" }, "predicate": { "value": "cost", "type": "resource" }, "type": "sort" }, "index": -1, "type": "nth" })
def testnormalFormProduction3(self): tree = computeTree(data.give_obama_president_usa()) NamedEntityMerging(tree).merge() PrepositionMerging(tree).merge() qw = simplify(tree) result = normalFormProduction(tree, qw) self.assertEqual( result, { "type": "intersection", "list": [{ "predicate": { "type": "list", "list": [{ "type": "resource", "value": "been" }, { "type": "resource", "value": "identity" }] }, "type": "triple", "subject": { "type": "resource", "value": "Obama" }, "inverse-predicate": { "type": "resource", "value": "identity" }, "object": { "type": "missing" } }, { "predicate": { "type": "list", "list": [{ "type": "resource", "value": "been" }, { "type": "resource", "value": "identity" }] }, "type": "triple", "subject": { "type": "resource", "value": "United States president" }, "inverse-predicate": { "type": "resource", "value": "identity" }, "object": { "type": "missing" } }] })
def testYesNoQuestion(self): tree = computeTree(data.give_born()) NamedEntityMerging(tree).merge() PrepositionMerging(tree).merge() simplify(tree) tree.sort() root = tree # Root self.assertEqual(root.wordList, [Word("ROOT", 0)]) self.assertEqual(root.namedEntityTag, 'undef') self.assertEqual(root.dependency, 'R0') self.assertEqual(root.parent, None) self.assertEqual(len(root.child), 1) self.assertEqual(root.subtreeType, 'undef') self.assertEqual(root.dfsTag, 0) # birth birth = root.child[0] self.assertEqual(birth.wordList, [Word("born in", 2, 'VBN')]) self.assertEqual(birth.namedEntityTag, 'undef') self.assertEqual(birth.dependency, 'R0') self.assertEqual(birth.parent, root) self.assertEqual(len(birth.child), 1) self.assertEqual(birth.subtreeType, 'undef') self.assertEqual(birth.dfsTag, 0) # date date = birth.child[0] self.assertEqual(date.wordList, [Word("1900", 4, 'CD')]) self.assertEqual(date.namedEntityTag, 'DATE') self.assertEqual(date.dependency, 'R3') self.assertEqual(date.parent, birth) self.assertEqual(len(date.child), 0) self.assertEqual(date.subtreeType, 'undef') self.assertEqual(date.dfsTag, 0)
def testPassIdentity(self): tree = computeTree(data.mickey()) NamedEntityMerging(tree).merge() PrepositionMerging(tree).merge() NamedEntityMerging(tree).merge() PrepositionMerging(tree).merge() simplify(tree) tree.sort() root = tree # Root self.assertEqual(root.wordList, [Word("ROOT", 0)]) self.assertEqual(root.namedEntityTag, 'undef') self.assertEqual(root.dependency, 'R0') self.assertEqual(root.parent, None) self.assertEqual(len(root.child), 1) self.assertEqual(root.subtreeType, 'DATE') self.assertEqual(root.dfsTag, 0) # identity identity = root.child[0] self.assertEqual(identity.wordList, [Word("is", 2, 'VBZ')]) self.assertEqual(identity.namedEntityTag, 'undef') self.assertEqual(identity.dependency, 'R0') self.assertEqual(identity.parent, root) self.assertEqual(len(identity.child), 1) self.assertEqual(identity.subtreeType, 'DATE') self.assertEqual(identity.dfsTag, 0) # birthday birthday = identity.child[0] self.assertEqual(birthday.wordList, [Word("birthday", 4, 'NN')]) self.assertEqual(birthday.namedEntityTag, 'undef') self.assertEqual(birthday.dependency, 'R2') self.assertEqual(birthday.parent, identity) self.assertEqual(len(birthday.child), 1) self.assertEqual(birthday.subtreeType, 'undef') self.assertEqual(birthday.dfsTag, 0) # mickey mickey = birthday.child[0] self.assertEqual(mickey.wordList, [Word("Mickey", 6, 'NNP'), Word("Mouse", 7, 'NNP')]) self.assertEqual(mickey.namedEntityTag, 'PERSON') self.assertEqual(mickey.dependency, 'R2') self.assertEqual(mickey.parent, birthday) self.assertEqual(len(mickey.child), 0) self.assertEqual(mickey.subtreeType, 'undef') self.assertEqual(root.dfsTag, 0)
def testNamedEntity2(self): tree=computeTree(data.give_obama_president_usa()) NamedEntityMerging(tree).merge() tree.sort() root=tree # Root self.assertEqual(root.wordList, [Word("ROOT", 0)]) self.assertEqual(root.namedEntityTag, 'undef') self.assertEqual(root.dependency, 'undef') self.assertEqual(root.parent, None) self.assertEqual(len(root.child), 1) self.assertEqual(root.subtreeType, 'undef') self.assertEqual(root.dfsTag, 0) # Is is_=root.child[0] self.assertEqual(is_.wordList, [Word("is", 2, 'VBZ')]) self.assertEqual(is_.namedEntityTag, 'undef') self.assertEqual(is_.dependency, 'ROOT') self.assertEqual(is_.parent, tree) self.assertEqual(len(is_.child), 2) self.assertEqual(is_.subtreeType, 'undef') self.assertEqual(is_.dfsTag, 0) # Obama obama=is_.child[0] self.assertEqual(obama.wordList, [Word("Obama", 1, 'NNP')]) self.assertEqual(obama.namedEntityTag, 'PERSON') self.assertEqual(obama.dependency, 'nsubj') self.assertEqual(obama.parent, is_) self.assertEqual(len(obama.child), 0) self.assertEqual(obama.subtreeType, 'undef') self.assertEqual(obama.dfsTag, 0) # president president =is_.child[1] self.assertEqual(president.wordList, [Word("president", 6, 'NN')]) self.assertEqual(president.namedEntityTag, 'undef') self.assertEqual(president.dependency, 'xcomp') self.assertEqual(president.parent, is_) self.assertEqual(len(president.child), 2) self.assertEqual(president.subtreeType, 'undef') self.assertEqual(president.dfsTag, 0) # The the=president.child[0] self.assertEqual(the.wordList, [Word("the", 3, 'DT')]) self.assertEqual(the.namedEntityTag, 'undef') self.assertEqual(the.dependency, 'det') self.assertEqual(the.parent, president) self.assertEqual(len(the.child), 0) self.assertEqual(the.subtreeType, 'undef') self.assertEqual(the.dfsTag, 0) # United States united=president.child[1] self.assertEqual(united.wordList, [Word("United", 4, 'NNP'), Word("States", 5, 'NNPS')]) self.assertEqual(united.namedEntityTag, 'LOCATION') self.assertEqual(united.dependency, 'compound') self.assertEqual(united.parent, president) self.assertEqual(len(united.child), 0) self.assertEqual(united.subtreeType, 'undef') self.assertEqual(united.dfsTag, 0)
def testBasicNamedEntitySisterBrother(self): tagList = ['LOCATION', 'PERSON', 'NUMBER', 'MONEY', 'MISC'] for tag in tagList: parent = DependenciesTree('parent', 1, namedEntityTag = 'undef') child1 = DependenciesTree('child1', 2, parent = parent, dependency = 'conj_and', namedEntityTag = tag) child2 = DependenciesTree('child2', 3, parent = parent, dependency = 'conj_and', namedEntityTag = tag) parent.child += [child1, child2] NamedEntityMerging(parent).merge() self.assertEqual(parent.wordList, [Word('parent', 1)]) self.assertEqual(parent.child, [child1, child2]) self.assertEqual(child1.parent, parent) self.assertEqual(child2.parent, parent) child1.dependency = 'foo' child2.dependency = 'foo' NamedEntityMerging(parent).merge() self.assertEqual(parent.wordList, [Word('parent', 1)]) self.assertEqual(len(parent.child), 1) self.assertIn(Word('child1', 2), parent.child[0].wordList) self.assertIn(Word('child2', 3), parent.child[0].wordList) self.assertEqual(parent.child[0].parent, parent) for (tag1, tag2) in itertools.permutations(tagList, 2): parent = DependenciesTree('parent', 1, namedEntityTag = 'undef') child1 = DependenciesTree('child1', 2, parent = parent, dependency = 'conj_and', namedEntityTag = tag1) child2 = DependenciesTree('child2', 3, parent = parent, dependency = 'conj_and', namedEntityTag = tag2) parent.child += [child1, child2] NamedEntityMerging(parent).merge() self.assertEqual(parent.wordList, [Word('parent', 1)]) self.assertEqual(parent.child, [child1, child2]) self.assertEqual(child1.parent, parent) self.assertEqual(child2.parent, parent) child1.dependency = 'foo' child2.dependency = 'foo' NamedEntityMerging(parent).merge() self.assertEqual(parent.wordList, [Word('parent', 1)]) self.assertEqual(parent.child, [child1, child2]) self.assertEqual(child1.parent, parent) self.assertEqual(child2.parent, parent)
def testNamedEntity1(self): tree=computeTree(data.give_john_smith()) NamedEntityMerging(tree).merge() tree.sort() root=tree # Root self.assertEqual(root.wordList, [Word("ROOT", 0)]) self.assertEqual(root.namedEntityTag, 'undef') self.assertEqual(root.dependency, 'undef') self.assertEqual(root.parent, None) self.assertEqual(len(root.child), 1) self.assertEqual(root.subtreeType, 'undef') self.assertEqual(root.dfsTag, 0) # Lives lives=root.child[0] self.assertEqual(lives.wordList, [Word("lives", 3, 'VBZ')]) self.assertEqual(lives.namedEntityTag, 'undef') self.assertEqual(lives.dependency, 'ROOT') self.assertEqual(lives.parent, tree) self.assertEqual(len(lives.child), 2) self.assertEqual(lives.subtreeType, 'undef') self.assertEqual(lives.dfsTag, 0) # John Smith smith=lives.child[0] self.assertEqual(smith.wordList, [Word("John", 1, 'NNP'), Word("Smith", 2, 'NNP')]) self.assertEqual(smith.namedEntityTag, 'PERSON') self.assertEqual(smith.dependency, 'nsubj') self.assertEqual(smith.parent, lives) self.assertEqual(len(smith.child), 0) self.assertEqual(smith.subtreeType, 'undef') self.assertEqual(smith.dfsTag, 0) # United Kingdom kingdom=lives.child[1] self.assertEqual(kingdom.wordList, [Word("United", 6, 'NNP'), Word("Kingdom", 7, 'NNP')]) self.assertEqual(kingdom.namedEntityTag, 'LOCATION') self.assertEqual(kingdom.dependency, 'prep_in') self.assertEqual(kingdom.parent, lives) self.assertEqual(len(kingdom.child), 1) self.assertEqual(kingdom.subtreeType, 'undef') self.assertEqual(kingdom.dfsTag, 0) # The the=kingdom.child[0] self.assertEqual(the.wordList, [Word("the", 5, 'DT')]) self.assertEqual(the.namedEntityTag, 'undef') self.assertEqual(the.dependency, 'det') self.assertEqual(the.parent, kingdom) self.assertEqual(len(the.child), 0) self.assertEqual(the.subtreeType, 'undef') self.assertEqual(the.dfsTag, 0)
def testnormalFormProductionR8(self): tree = computeTree(data.mistake()) NamedEntityMerging(tree).merge() PrepositionMerging(tree).merge() qw = simplify(tree) result = normalFormProduction(tree, qw) self.assertEqual( result, { "type": "triple", "subject": { "type": "resource", "value": "mistake" }, "object": { "type": "missing" }, "predicate": { "type": "list", "list": [{ "type": "resource", "value": "place" }, { "type": "resource", "value": "location" }, { "type": "resource", "value": "residence" }, { "type": "resource", "value": "country" }, { "type": "resource", "value": "city" }, { "type": "resource", "value": "town" }, { "type": "resource", "value": "state" }, { "type": "resource", "value": "locality" }] } })
def testAndnormalFormProduction(self): tree = computeTree(data.give_chief()) NamedEntityMerging(tree).merge() PrepositionMerging(tree).merge() qw = simplify(tree) result = normalFormProduction(tree, qw) self.assertEqual( result, { "list": [{ "type": "triple", "subject": { "type": "resource", "value": "chief" }, "object": { "type": "missing" }, "predicate": { "list": [{ "type": "resource", "value": "identity" }], "type": "list" } }, { "type": "triple", "subject": { "type": "resource", "value": "prime minister" }, "object": { "type": "missing" }, "predicate": { "list": [{ "type": "resource", "value": "identity" }], "type": "list" } }], "type": "intersection" })
def testHierarchySimplification(self): tree = computeTree(data.give_president_of_USA()) NamedEntityMerging(tree).merge() PrepositionMerging(tree).merge() simplify(tree) tree.sort() root = tree # Root self.assertEqual(root.wordList, [Word("ROOT", 0)]) self.assertEqual(root.namedEntityTag, 'undef') self.assertEqual(root.dependency, 'R0') self.assertEqual(root.parent, None) self.assertEqual(len(root.child), 1) self.assertEqual(root.subtreeType, 'PERSON') self.assertEqual(root.dfsTag, 0) # Is is_ = root.child[0] self.assertEqual(is_.wordList, [Word("is", 2, 'VBZ')]) self.assertEqual(is_.namedEntityTag, 'undef') self.assertEqual(is_.dependency, 'R0') self.assertEqual(is_.parent, root) self.assertEqual(len(is_.child), 1) self.assertEqual(is_.subtreeType, 'PERSON') self.assertEqual(is_.dfsTag, 0) # President president = is_.child[0] self.assertEqual(president.wordList, [Word("president", 4, 'NN')]) self.assertEqual(president.namedEntityTag, 'undef') self.assertEqual(president.dependency, 'R2') self.assertEqual(president.parent, is_) self.assertEqual(len(president.child), 1) self.assertEqual(president.subtreeType, 'undef') self.assertEqual(president.dfsTag, 0) # United States us = president.child[0] self.assertEqual(us.wordList, [Word("United", 7, 'NNP'), Word("States", 8, 'NNPS')]) self.assertEqual(us.namedEntityTag, 'LOCATION') self.assertEqual(us.dependency, 'R2') self.assertEqual(us.parent, president) self.assertEqual(len(us.child), 0) self.assertEqual(us.subtreeType, 'undef') self.assertEqual(us.dfsTag, 0)
def testSemiQuestionWord1(self): tree = computeTree(data.roald()) NamedEntityMerging(tree).merge() PrepositionMerging(tree).merge() qw = simplify(tree) result = normalFormProduction(tree, qw) self.assertEqual( result, { "subject": { "value": "Roald Dahl", "type": "resource" }, "type": "triple", "predicate": { "value": "book", "type": "resource" }, "object": { "type": "missing" } })
def testSemiQuestionWord5(self): tree = computeTree(data.capital2()) NamedEntityMerging(tree).merge() PrepositionMerging(tree).merge() qw = simplify(tree) result = normalFormProduction(tree, qw) self.assertEqual( result, { "predicate": { "type": "resource", "value": "capital" }, "type": "triple", "subject": { "type": "resource", "value": "France" }, "object": { "type": "missing" } })
def testnormalFormProduction1(self): tree = computeTree(data.give_president_of_USA()) NamedEntityMerging(tree).merge() PrepositionMerging(tree).merge() qw = simplify(tree) result = normalFormProduction(tree, qw) self.assertEqual( result, { "object": { "type": "missing" }, "subject": { "type": "resource", "value": "United States" }, "predicate": { "type": "resource", "value": "president" }, "type": "triple" })
def testSuperlativenormalFormProduction(self): tree = computeTree(data.give_opera()) NamedEntityMerging(tree).merge() PrepositionMerging(tree).merge() qw = simplify(tree) result = normalFormProduction(tree, qw) self.assertEqual( result, { "list": [{ "list": { "predicate": { "value": "default", "type": "resource" }, "list": { "value": "Gilbert", "type": "resource" }, "type": "sort" }, "index": 0, "type": "nth" }, { "list": { "predicate": { "value": "default", "type": "resource" }, "list": { "value": "Sullivan opera", "type": "resource" }, "type": "sort" }, "index": 0, "type": "nth" }], "type": "intersection" })
def testExists(self): tree = computeTree(data.king_england()) NamedEntityMerging(tree).merge() PrepositionMerging(tree).merge() qw = simplify(tree) result = normalFormProduction(tree, qw) self.assertEqual( result, { "list": { "predicate": { "type": "resource", "value": "king" }, "subject": { "type": "resource", "value": "England" }, "type": "triple", "object": { "type": "missing" } }, "type": "exists" })
def testIgnore(self): tree = computeTree(data.give_how_old()) NamedEntityMerging(tree).merge() PrepositionMerging(tree).merge() simplify(tree) tree.sort() root = tree # Root self.assertEqual(root.wordList, [Word("ROOT", 0)]) self.assertEqual(root.namedEntityTag, 'undef') self.assertEqual(root.dependency, 'R0') self.assertEqual(root.parent, None) self.assertEqual(len(root.child), 1) self.assertEqual(root.subtreeType, 'NUMBER') self.assertEqual(root.dfsTag, 0) # Are are = root.child[0] self.assertEqual(are.wordList, [Word("are", 3, 'VBP')]) self.assertEqual(are.namedEntityTag, 'undef') self.assertEqual(are.dependency, 'R0') self.assertEqual(are.parent, root) self.assertEqual(len(are.child), 0) self.assertEqual(are.subtreeType, 'NUMBER') self.assertEqual(are.dfsTag, 0)
def testHierarchyConnectors2(self): tree = computeTree(data.give_chief()) NamedEntityMerging(tree).merge() PrepositionMerging(tree).merge() simplify(tree) tree.sort() root = tree # Root self.assertEqual(root.wordList, [Word("ROOT", 0)]) self.assertEqual(root.namedEntityTag, 'undef') self.assertEqual(root.dependency, 'R0') self.assertEqual(root.parent, None) self.assertEqual(len(root.child), 1) self.assertEqual(root.subtreeType, 'PERSON') self.assertEqual(root.dfsTag, 0) # and andw = root.child[0] self.assertEqual(andw.wordList, [Word("and", 1000, None)]) self.assertEqual(andw.namedEntityTag, 'undef') self.assertEqual(andw.dependency, 'R0') self.assertEqual(andw.parent, root) self.assertEqual(len(andw.child), 2) self.assertEqual(andw.subtreeType, 'PERSON') self.assertEqual(andw.dfsTag, 0) # identity identity1 = andw.child[0] self.assertEqual(identity1.wordList, [Word("is", 2, 'VBZ')]) self.assertEqual(identity1.namedEntityTag, 'undef') self.assertEqual(identity1.dependency, 'RconjT') self.assertEqual(identity1.parent, andw) self.assertEqual(len(identity1.child), 1) self.assertEqual(identity1.subtreeType, 'PERSON') self.assertEqual(identity1.dfsTag, 0) # identity identity2 = andw.child[1] self.assertEqual(identity2.wordList, [Word("is", 2, 'VBZ')]) self.assertEqual(identity2.namedEntityTag, 'undef') self.assertEqual(identity2.dependency, 'RconjB') self.assertEqual(identity2.parent, andw) self.assertEqual(len(identity2.child), 1) self.assertEqual(identity2.subtreeType, 'PERSON') self.assertEqual(identity2.dfsTag, 0) # chief chief = identity1.child[0] self.assertEqual(chief.wordList, [Word("chief", 4, 'NN')]) self.assertEqual(chief.namedEntityTag, 'undef') self.assertEqual(chief.dependency, 'R2') self.assertEqual(chief.parent, identity1) self.assertEqual(len(chief.child), 0) self.assertEqual(chief.subtreeType, 'undef') self.assertEqual(chief.dfsTag, 0) # prime minister prime = identity2.child[0] self.assertEqual(prime.wordList, [Word("prime", 6, 'JJ'), Word("minister", 7, 'NN')]) self.assertEqual(prime.namedEntityTag, 'undef') self.assertEqual(prime.dependency, 'R2') self.assertEqual(prime.parent, identity2) self.assertEqual(len(prime.child), 0) self.assertEqual(prime.subtreeType, 'undef') self.assertEqual(prime.dfsTag, 0)
def testCop(self): tree = computeTree(data.black()) NamedEntityMerging(tree).merge() PrepositionMerging(tree).merge() self.assertRaises(GrammaticalError, lambda: simplify(tree))
def testQuestion(self): tree = computeTree(data.give_president_of_USA()) NamedEntityMerging(tree).merge() PrepositionMerging(tree).merge() self.assertEqual(simplify(tree), 'who')
def testHierarchyConnectors1(self): tree = computeTree(data.give_opera()) NamedEntityMerging(tree).merge() PrepositionMerging(tree).merge() simplify(tree) tree.sort() root = tree # Root self.assertEqual(root.wordList, [Word("ROOT", 0)]) self.assertEqual(root.namedEntityTag, 'undef') self.assertEqual(root.dependency, 'R0') self.assertEqual(root.parent, None) self.assertEqual(len(root.child), 1) self.assertEqual(root.subtreeType, 'undef') self.assertEqual(root.dfsTag, 0) # identity identity = root.child[0] self.assertEqual(identity.wordList, [Word("was", 2, 'VBD')]) self.assertEqual(identity.namedEntityTag, 'undef') self.assertEqual(identity.dependency, 'R0') self.assertEqual(identity.parent, root) self.assertEqual(len(identity.child), 1) self.assertEqual(identity.subtreeType, 'undef') self.assertEqual(identity.dfsTag, 0) # and andw = identity.child[0] self.assertEqual(andw.wordList, [Word("and", 1000, None)]) self.assertEqual(andw.namedEntityTag, 'undef') self.assertEqual(andw.dependency, 'R2') self.assertEqual(andw.parent, identity) self.assertEqual(len(andw.child), 2) self.assertEqual(andw.subtreeType, 'undef') self.assertEqual(andw.dfsTag, 0) # first1 first1 = andw.child[0] self.assertEqual(first1.wordList, [Word("first", 4, 'JJ')]) self.assertEqual(first1.namedEntityTag, 'ORDINAL') self.assertEqual(first1.dependency, 'RconjT') self.assertEqual(first1.parent, andw) self.assertEqual(len(first1.child), 1) self.assertEqual(first1.subtreeType, 'undef') self.assertEqual(first1.dfsTag, 0) # first2 first2 = andw.child[1] self.assertEqual(first2.wordList, [Word("first", 4, 'JJ')]) self.assertEqual(first2.namedEntityTag, 'ORDINAL') self.assertEqual(first2.dependency, 'RconjB') self.assertEqual(first2.parent, andw) self.assertEqual(len(first2.child), 1) self.assertEqual(first2.subtreeType, 'undef') self.assertEqual(first2.dfsTag, 0) # gilbert gilbert = first1.child[0] self.assertEqual(gilbert.wordList, [Word("Gilbert", 5, 'NNP')]) self.assertEqual(gilbert.namedEntityTag, 'PERSON') self.assertEqual(gilbert.dependency, 'Rspl') self.assertEqual(gilbert.parent, first1) self.assertEqual(len(gilbert.child), 0) self.assertEqual(gilbert.subtreeType, 'undef') self.assertEqual(gilbert.dfsTag, 0) # sullivan sullivan = first2.child[0] self.assertEqual(sullivan.wordList, [Word("Sullivan", 7, 'NNP'), Word("opera", 8, 'NN')]) self.assertEqual(sullivan.namedEntityTag, 'undef') self.assertEqual(sullivan.dependency, 'Rspl') self.assertEqual(sullivan.parent, first2) self.assertEqual(len(sullivan.child), 0) self.assertEqual(sullivan.subtreeType, 'undef') self.assertEqual(sullivan.dfsTag, 0)
def testQuestion2(self): tree = computeTree(data.give_how_old()) NamedEntityMerging(tree).merge() PrepositionMerging(tree).merge() self.assertEqual(simplify(tree), 'how old')
def testnormalFormProduction2(self): handler = QuotationHandler('foo') sentence = 'Who wrote "Lucy in the Sky with Diamonds" and "Let It Be"?' nonAmbiguousSentence = handler.pull(sentence) result = data.give_LSD_LIB() tree = computeTree(result) handler.push(tree) NamedEntityMerging(tree).merge() PrepositionMerging(tree).merge() qw = simplify(tree) result = normalFormProduction(tree, qw) self.assertEqual( result, { "list": [{ "inverse-predicate": { "list": [{ "value": "author", "type": "resource" }, { "value": "writer", "type": "resource" }], "type": "list" }, "subject": { "type": "missing" }, "object": { "value": "Lucy in the Sky with Diamonds", "type": "resource" }, "type": "triple", "predicate": { "list": [{ "value": "written", "type": "resource" }, { "value": "literary works", "type": "resource" }, { "value": "bibliography", "type": "resource" }, { "value": "notable work", "type": "resource" }], "type": "list" } }, { "inverse-predicate": { "list": [{ "value": "author", "type": "resource" }, { "value": "writer", "type": "resource" }], "type": "list" }, "subject": { "type": "missing" }, "object": { "value": "Let It Be", "type": "resource" }, "type": "triple", "predicate": { "list": [{ "value": "written", "type": "resource" }, { "value": "literary works", "type": "resource" }, { "value": "bibliography", "type": "resource" }, { "value": "notable work", "type": "resource" }], "type": "list" } }], "type": "intersection" })