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)
Exemplo n.º 2
0
 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 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 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)
Exemplo n.º 5
0
 def testBasicWordConstructor1(self):
     w = Word('foo', 1, 'bar')
     self.assertEqual(w.word, 'foo')
     self.assertEqual(w.index, 1)
     self.assertEqual(w.pos, 'bar')
     self.assertEqual(str(w), "(foo, 1, bar)")
     w.append('aaa')
     self.assertEqual(Word('foo aaa', 1, 'bar'), w)
 def testBasicWordConstructor1(self):
     w=Word('foo', 1, 'bar')
     self.assertEqual(w.word, 'foo')
     self.assertEqual(w.index, 1)
     self.assertEqual(w.pos, 'bar')
     self.assertEqual(str(w), "(foo, 1, bar)")
     w.append('aaa')
     self.assertEqual(Word('foo aaa', 1, 'bar'), w)
Exemplo n.º 7
0
 def testTreePos(self):
     n = DependenciesTree('foo', 1)
     n.wordList += [Word('eat', 2, 'VB'), Word('bar', 3)]
     self.assertTrue(n.isVerb())
     self.assertFalse(n.isNoun())
     n = DependenciesTree('foo', 1)
     n.wordList += [Word('broomstick', 2, 'NN'), Word('bar', 3)]
     self.assertFalse(n.isVerb())
     self.assertTrue(n.isNoun())
Exemplo n.º 8
0
 def testQuotationMerge(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)
     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)
     # Wrote
     wrote = root.child[0]
     self.assertEqual(wrote.wordList, [Word("wrote", 2, 'VBD')])
     self.assertEqual(wrote.namedEntityTag, 'undef')
     self.assertEqual(wrote.dependency, 'ROOT')
     self.assertEqual(wrote.parent, root)
     self.assertEqual(len(wrote.child), 2)
     self.assertEqual(wrote.subtreeType, 'undef')
     self.assertEqual(wrote.dfsTag, 0)
     # Who
     who = wrote.child[0]
     self.assertEqual(who.wordList, [Word("Who", 1, 'WP')])
     self.assertEqual(who.namedEntityTag, 'undef')
     self.assertEqual(who.dependency, 'nsubj')
     self.assertEqual(who.parent, wrote)
     self.assertEqual(len(who.child), 0)
     self.assertEqual(who.subtreeType, 'undef')
     self.assertEqual(who.dfsTag, 0)
     # Lucy in the Sky with Diamondss
     lucy = wrote.child[1]
     self.assertEqual(lucy.wordList,
                      [Word("Lucy in the Sky with Diamonds", 3, 'QUOTE')])
     self.assertEqual(lucy.namedEntityTag, 'QUOTATION')
     self.assertEqual(lucy.dependency, 'dobj')
     self.assertEqual(lucy.parent, wrote)
     self.assertEqual(len(lucy.child), 1)
     self.assertEqual(lucy.subtreeType, 'undef')
     self.assertEqual(lucy.dfsTag, 0)
     # Let it be
     let = lucy.child[0]
     self.assertEqual(let.wordList, [Word("Let It Be", 5, 'QUOTE')])
     self.assertEqual(let.namedEntityTag, 'QUOTATION')
     self.assertEqual(let.dependency, 'conj_and')
     self.assertEqual(let.parent, lucy)
     self.assertEqual(len(let.child), 0)
     self.assertEqual(let.subtreeType, 'undef')
     self.assertEqual(let.dfsTag, 0)
Exemplo n.º 9
0
 def testPOS(self):
     for pos in {'VB', 'VBD', 'VBG', 'VBN', 'VBP', 'VBZ'}:
         w = Word('foo', 1, pos)
         self.assertTrue(w.isVerb())
         self.assertFalse(w.isNoun())
     for pos in {'NN', 'NNS', 'NNP', 'NNPS'}:
         w = Word('foo', 1, pos)
         self.assertFalse(w.isVerb())
         self.assertTrue(w.isNoun())
Exemplo n.º 10
0
 def testBasicPrepositionEdge(self):
     for prep in ['in', 'of', 'with', 'by']:
         parent = DependenciesTree('parent', 1)
         parent.wordList[0].pos = 'VB'
         child = DependenciesTree('child', 2, parent = parent, dependency = 'prep_'+prep)
         parent.child.append(child)
         PrepositionMerging(parent).merge()
         self.assertEqual(parent.wordList, [Word('parent '+prep, 1, 'VB')])
         self.assertEqual(parent.child, [child])
         self.assertEqual(child.dependency, 'prep')
     parent = DependenciesTree('parent', 1)
     parent.wordList[0].pos = 'VB'
     child = DependenciesTree('child', 2, parent = parent, dependency = 'agent')
     parent.child.append(child)
     PrepositionMerging(parent).merge()
     self.assertEqual(parent.wordList, [Word('parent by', 1, 'VB')])
     self.assertEqual(parent.child, [child])
Exemplo n.º 11
0
 def testBasicPrepositionNode(self):
     parent = DependenciesTree('parent', 1)
     child = DependenciesTree('child', 2, parent = parent, dependency = 'foo')
     parent.child.append(child)
     PrepositionMerging(parent).merge()
     self.assertEqual(parent.wordList, [Word('parent', 1)])
     self.assertEqual(parent.child, [child])
     self.assertEqual(child.parent, parent)
     for prep in PrepositionMerging.prepositionSet:
         parent = DependenciesTree('parent', 1)
         child = DependenciesTree(prep, 2, parent = parent, dependency = 'foo')
         parent.child.append(child)
         child.dependency = 'conj_and'
         PrepositionMerging(parent).merge()
         self.assertIn(Word('parent', 1), parent.wordList)
         self.assertIn(Word(prep, 2), parent.wordList)
         self.assertEqual(parent.child, [])
 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 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)
Exemplo n.º 14
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)
Exemplo n.º 15
0
 def testMerge(self):
     root1 = DependenciesTree('root', 1)
     root2 = DependenciesTree('root', 2)
     node1 = DependenciesTree('n', 1, 'tag1', 'stype1', 'dep1',
                              [DependenciesTree('childn', 1)])
     node1.parent = root1
     root1.child += [node1]
     node2 = DependenciesTree('n', 2, 'tag2', 'stype2', 'dep2',
                              [DependenciesTree('childn', 2)])
     node2.parent = root2
     root2.child += [node2]
     node1.merge(node2, True)
     self.assertEqual(len(root2.child), 0)
     self.assertEqual(len(root1.child), 1)
     self.assertEqual(len(node1.child), 2)
     self.assertEqual(node1.wordList, [Word('n', 1), Word('n', 2)])
     self.assertEqual(node1.namedEntityTag, 'tag1')
     self.assertEqual(node1.dependency, 'dep1')
     self.assertEqual(node1.parent, root1)
     self.assertEqual(node1.subtreeType, 'stype1')
     self.assertEqual(node1.dfsTag, 0)
Exemplo n.º 16
0
 def testBasicTreeConstructor(self):
     n = DependenciesTree('foo', 1)
     self.assertEqual(n.wordList, [Word('foo', 1)])
     self.assertEqual(n.namedEntityTag, 'undef')
     self.assertEqual(n.dependency, 'undef')
     self.assertEqual(n.child, [])
     self.assertEqual(n.text, "")
     self.assertEqual(n.parent, None)
     self.assertEqual(n.subtreeType, 'undef')
     self.assertEqual(n.dfsTag, 0)
     self.assertFalse(n.isVerb())
     self.assertFalse(n.isNoun())
     n.appendWord('bar')
     self.assertEqual(str(DependenciesTree('foo bar', 1)), str(n))
 def testPOS(self):
     for pos in {'VB', 'VBD', 'VBG', 'VBN', 'VBP', 'VBZ'}:
         w = Word('foo', 1, pos)
         self.assertTrue(w.isVerb())
         self.assertFalse(w.isNoun())
     for pos in {'NN', 'NNS', 'NNP', 'NNPS'}:
         w = Word('foo', 1, pos)
         self.assertFalse(w.isVerb())
         self.assertTrue(w.isNoun())
 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)
Exemplo n.º 19
0
 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)
Exemplo n.º 20
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 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 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)