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 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 testYesNoQuestion(self):
     tree = computeTree(data.give_born()["sentences"][0])
     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 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 testQuestionInfo(self):
     tree = computeTree(data.birth_place()["sentences"][0])
     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 testHierarchyConnectors2(self):
     tree = computeTree(data.give_chief()["sentences"][0])
     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 testHierarchySimplification2(self):
     tree = computeTree(data.give_USA_president()["sentences"][0])
     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)
     # US
     us = is_.child[0]
     self.assertEqual(us.wordList, [Word("United", 4, "NNP"), Word("States", 5, "NNPS"), Word("president", 6, "NN")])
     self.assertEqual(us.namedEntityTag, "undef")
     self.assertEqual(us.dependency, "R2")
     self.assertEqual(us.parent, is_)
     self.assertEqual(len(us.child), 0)
     self.assertEqual(us.subtreeType, "undef")
     self.assertEqual(us.dfsTag, 0)
 def testHierarchySimplification2(self):
     tree=computeTree(data.give_USA_president())
     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)
     # US
     us=is_.child[0]
     self.assertEqual(us.wordList, [Word("United", 4, 'NNP'), Word("States", 5, 'NNPS'), Word("president", 6, 'NN')])
     self.assertEqual(us.namedEntityTag, 'undef')
     self.assertEqual(us.dependency, 'R2')
     self.assertEqual(us.parent, is_)
     self.assertEqual(len(us.child), 0)
     self.assertEqual(us.subtreeType, 'undef')
     self.assertEqual(us.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)
 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 testNoQW(self):
     tree = computeTree(data.birth_date()["sentences"][0])
     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 testnormalFormProductionSuperl(self):
        tree = computeTree(data.tanzania()['sentences'][0])
        NamedEntityMerging(tree).merge()
        PrepositionMerging(tree).merge()
        qw = simplify(tree)
        result = normalFormProduction(tree, qw)
        self.assertEqual(result, {
    "list": {
        "list": {
            "object": {
                "type": "missing"
            },
            "predicate": {
                "value": "mountain",
                "type": "resource"
            },
            "subject": {
                "value": "Tanzania",
                "type": "resource"
            },
            "type": "triple"
        },
        "predicate": {
                    "value" : "height",
                    "type"  : "resource"
                },
        "type": "sort"
    },
    "type": "last"
})
    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"
})
Esempio n. 14
0
 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"
         })
Esempio n. 15
0
 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 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)
 def testPassIdentity(self):
     tree = computeTree(data.mickey()["sentences"][0])
     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 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 get_tree():
    nlp = StanfordNLP()
    sentence = eval(input(""))
    handler = ppp_questionparsing_grammatical.QuotationHandler()
    simplifiedSentence = handler.pull(sentence)
    result = nlp.parse(simplifiedSentence)
    tree = ppp_questionparsing_grammatical.computeTree(result)
    handler.push(tree)
    ppp_questionparsing_grammatical.NamedEntityMerging(tree).merge()
    ppp_questionparsing_grammatical.PrepositionMerging(tree).merge()
    qw = ppp_questionparsing_grammatical.simplify(tree)
    return tree
def get_tree():
    nlp = StanfordNLP()
    sentence = input("")
    handler = ppp_questionparsing_grammatical.QuotationHandler()
    simplifiedSentence = handler.pull(sentence)
    result = nlp.parse(simplifiedSentence)
    tree = ppp_questionparsing_grammatical.computeTree(result['sentences'][0])
    handler.push(tree)
    ppp_questionparsing_grammatical.NamedEntityMerging(tree).merge()
    ppp_questionparsing_grammatical.PrepositionMerging(tree).merge()
    qw = ppp_questionparsing_grammatical.simplify(tree)
    return tree
    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 get_answer(sentence=""):
    nlp = StanfordNLP()
    if sentence == "":
        sentence = input("")
    handler = ppp_questionparsing_grammatical.QuotationHandler()
    simplifiedSentence = handler.pull(sentence)
    result = nlp.parse(simplifiedSentence)
    tree = ppp_questionparsing_grammatical.computeTree(result)
    handler.push(tree)
    ppp_questionparsing_grammatical.NamedEntityMerging(tree).merge()
    ppp_questionparsing_grammatical.PrepositionMerging(tree).merge()
    qw = ppp_questionparsing_grammatical.simplify(tree)
    t = ppp_questionparsing_grammatical.normalFormProduction(tree, qw)
    return t
    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 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 testIgnore(self):
     tree = computeTree(data.give_how_old()["sentences"][0])
     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)
Esempio n. 27
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"
                 }]
             }
         })
Esempio n. 28
0
 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 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 testSemiQuestionWord3(self):
        tree = computeTree(data.list_president2()['sentences'][0])
        NamedEntityMerging(tree).merge()
        PrepositionMerging(tree).merge()
        qw = simplify(tree)
        result = normalFormProduction(tree, qw)
        self.assertEqual(result, {
    "type": "triple",
    "object": {
        "type": "missing"
    },
    "predicate": {
        "type": "resource",
        "value": "president"
    },
    "subject": {
        "type": "resource",
        "value": "France"
    }
})
    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 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"
})
Esempio n. 34
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"
             }
         })
Esempio n. 35
0
 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"
             }
         })
Esempio n. 36
0
 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"
         })
Esempio n. 37
0
 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"
})
Esempio n. 39
0
 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 testCop(self):
     tree = computeTree(data.black())
     NamedEntityMerging(tree).merge()
     PrepositionMerging(tree).merge()
     self.assertRaises(GrammaticalError, lambda: simplify(tree))
 def testHierarchyConnectors1(self):
     tree = computeTree(data.give_opera()["sentences"][0])
     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)
Esempio n. 42
0
 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"
         })
    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"
})
Esempio n. 44
0
 def testCop(self):
     tree = computeTree(data.black())
     NamedEntityMerging(tree).merge()
     PrepositionMerging(tree).merge()
     self.assertRaises(GrammaticalError, lambda: simplify(tree))
 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 testQuestion2(self):
     tree = computeTree(data.give_how_old())
     NamedEntityMerging(tree).merge()
     PrepositionMerging(tree).merge()
     self.assertEqual(simplify(tree), 'how old')
 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 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 testQuestion(self):
     tree = computeTree(data.give_president_of_USA())
     NamedEntityMerging(tree).merge()
     PrepositionMerging(tree).merge()
     self.assertEqual(simplify(tree), 'who')
 def testQuestion2(self):
     tree = computeTree(data.give_how_old()["sentences"][0])
     NamedEntityMerging(tree).merge()
     PrepositionMerging(tree).merge()
     self.assertEqual(simplify(tree), "how old")
 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 testQuestion(self):
     tree = computeTree(data.give_president_of_USA()["sentences"][0])
     NamedEntityMerging(tree).merge()
     PrepositionMerging(tree).merge()
     self.assertEqual(simplify(tree), "who")