Пример #1
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 tripleProductionData():
    '''
        Return data corresponding to a tree (root-0)--dep-->(child-1)
    '''
    root = DependenciesTree("root-0")
    child = DependenciesTree("child-1",dependency="dep",parent=root)
    root.child = [child]
    nodeToID = {root:0, child:1}
    bt = TriplesBucket()
    return (root,nodeToID,bt)
 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))
Пример #4
0
 def testAddNamedEntityTag3(self):
     foo1 = DependenciesTree('foo1', 1, namedEntityTag='42')
     foo2 = DependenciesTree('foo2', 3, namedEntityTag='42')
     bar = DependenciesTree('bar',
                            2,
                            namedEntityTag='undef',
                            dependency='amod',
                            parent=foo1)
     generator = TreeGenerator(None)
     generator.nameToNodes = {
         ('foo1', 1): foo1,
         ('bar', 2): bar,
         ('foo2', 3): foo2
     }
     generator._correctTree(foo1)
     self.assertEqual(bar.namedEntityTag, 'undef')
Пример #5
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())
Пример #6
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])
 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())
Пример #8
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))
Пример #9
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 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)
Пример #11
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)
Пример #12
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)