コード例 #1
0
    def testTraverseNoException(self):
        def pred(node):
            return node

        Union([List([Resource('foo')]),
               List([Resource('bar')])]).traverse(pred)
        first(List([Resource('foo'), Resource('bar')])).traverse(pred)
        Exists(List([Resource('foo'), Resource('bar')])).traverse(pred)
        Exists(Resource('foo')).traverse(pred)
コード例 #2
0
 def testEquality(self):
     self.assertEqual(List([R('foo'), R('bar')]), List([R('foo'),
                                                        R('bar')]))
     self.assertNotEqual(List([R('foo'), R('bar')]),
                         List([R('foo'), R('baz')]))
     self.assertEqual(List([R('foo')]), List([R('foo')]))
     self.assertEqual(List([R('foo')]), R('foo'))
     self.assertNotEqual(List([R('foo')]), R('bar'))
     self.assertEqual(R('foo'), List([R('foo')]))
     self.assertNotEqual(R('bar'), List([R('foo')]))
コード例 #3
0
 def testInclusionTriple(self):
     tree1 = Triple(Resource('foo'), List([Resource('a'),
                                           Resource('b')]), Missing())
     tree2 = Triple(List([Resource('bar'), Resource('foo')]),
                    List([Resource('a'),
                          Resource('d'),
                          Resource('b')]), Missing())
     self.assertIncluded(tree1, tree2)
     with self.assertRaises(AssertionError):
         self.assertIncluded(tree2, tree1)
コード例 #4
0
 def testHash(self):
     o1 = List([Resource('foo'), Resource('bar')])
     o2 = List([Resource('foo'), Resource('bar')])
     h1 = hash(o1)
     h2 = hash(o2)
     self.assertEqual(h1, h2)
     o1.list.append(Resource('baz'))
     self.assertNotEqual(hash(o1), h2)
     hash(Union([o1, o2]))
     o1.as_json()
コード例 #5
0
    def testTraverseContract(self):
        f = Resource('foo')

        def pred(tree):
            if isinstance(tree, List):
                return Resource('foo')
            elif isinstance(tree, Intersection):
                self.assertEqual(tree, Intersection([f, f]))
                return tree
            elif isinstance(tree, Resource):
                return tree
            else:
                raise AssertionError(tree)

        tree = Intersection([List([Resource('1')]), List([Resource('2')])])
        self.assertEqual(tree.traverse(pred), Intersection([f, f]))
コード例 #6
0
ファイル: test_definition.py プロジェクト: ProjetPP/PPP-OEIS
 def testMultiplePredicates(self):
     q = Request(
         '1', 'en',
         Triple(Resource('1 2 4 8'),
                List([Resource('definition'),
                      Resource('foo')]), Missing()), {}, [])
     r = self.request(q)
     self.assertGreater(len(r), 1, r)
コード例 #7
0
 def testInclusionTriple2(self):
     tree1 = Triple(Resource('a'), Resource('b'), Resource('c'),
                    Resource('d'))
     tree2 = Triple(Resource('a'), Resource('b'), Resource('c'),
                    List([Resource('d'), Resource('e')]))
     self.assertIncluded(tree1, tree2)
     with self.assertRaises(AssertionError):
         self.assertIncluded(tree2, tree1)
コード例 #8
0
 def testInclusionIntersectionUnionAndOr(self):
     tree1 = Triple(Resource('foo'), List([Resource('a'),
                                           Resource('b')]), Missing())
     tree2 = Triple(List([Resource('bar'), Resource('foo')]),
                    List([Resource('a'),
                          Resource('d'),
                          Resource('b')]), Missing())
     tree3 = Missing()
     for op in [Intersection, Union, And, Or]:
         self.assertIncluded(op([tree1]), op([tree2]))
         with self.assertRaises(AssertionError):
             self.assertIncluded(op([tree2]), op([tree1]))
         self.assertIncluded(op([tree1, tree3]), op([tree1, tree3]))
         self.assertIncluded(op([tree1, tree3]), op([tree3, tree1]))
         with self.assertRaises(AssertionError):
             self.assertIncluded(op([tree1, tree3]), op([tree1]))
         with self.assertRaises(AssertionError):
             self.assertIncluded(op([tree1]), op([tree3]))
コード例 #9
0
 def testInclusionFirstLastSort(self):
     tree1 = Triple(Resource('foo'), List([Resource('a'),
                                           Resource('b')]), Missing())
     tree2 = Triple(List([Resource('bar'), Resource('foo')]),
                    List([Resource('a'),
                          Resource('d'),
                          Resource('b')]), Missing())
     for op in (last, first):
         self.assertIncluded(op(tree1), op(tree2))
         with self.assertRaises(AssertionError):
             self.assertIncluded(op(tree2), op(tree1))
     self.assertIncluded(Sort(tree1, Resource('pred')),
                         Sort(tree2, Resource('pred')))
     with self.assertRaises(AssertionError):
         self.assertIncluded(Sort(tree2, Resource('pred')),
                             Sort(tree1, Resource('pred')))
     with self.assertRaises(AssertionError):
         self.assertIncluded(Sort(tree1, Resource('pred')),
                             Sort(tree2, Resource('derp')))
def enhanceTriple(nf, w, addMap=questionAdd, wisMap=questionWIs):
    """
        Add info into the triple depending on the question word
    """
    predList = extractPredicates(nf)
    try:
        if 'identity' in predList:
             if w in strongQuestionWord or isinstance(nf.subject, Resource) or isinstance(nf.object, Resource): # strong qw or triple of depth 1
                 return Triple(nf.subject, List([Resource(x) for x in wisMap[w]]), nf.object) # !! Other info lost (type...) (inverse_predicate: not relevant)
             else: # delete the first level
                if isinstance(nf.subject, Missing):
                    return nf.object
                else:
                    return nf.subject
        elif not 'instance of' in predList: # add info into the predicates list (except for instance_of predicate)
             return Triple(nf.subject, List([Resource(x) for x in predList] + [Resource(x+' '+y) for x in predList for y in addMap[w]]), nf.object, nf.inverse_predicate) # !! Other info lost (type...) (reverse_predicate not enhance?)
        else:
            return nf
    except KeyError:
         return nf
コード例 #11
0
 def testInversePredicateEmpty(self):
     self.assertEqual(T(M(), M(), M()).inverse_predicate, List([]))
     self.assertEqual(T(M(), M(), M(), M()).inverse_predicate, M())
     self.assertEqual(T(M(), M(), M()),
             {'type': 'triple', 'object': {'type': 'missing'},
              'subject': {'type': 'missing'},
              'predicate': {'type': 'missing'}})
     self.assertEqual(T(M(), M(), M(), M()),
             {'type': 'triple', 'object': {'type': 'missing'},
              'subject': {'type': 'missing'},
              'predicate': {'type': 'missing'},
              'inverse-predicate': {'type': 'missing'}})
コード例 #12
0
def buildPredicateVerb(tree):
    """
        Produce a predicate from the root of tree, assume that wordList is a verb v
        Return a couple (a, b) where a must be the predicate, and b the inverse predicate
        (b = None if there is no inverse predicate)
    """
    lem = verbStandardize(
        tree
    )  # (v1, v2) where v1=lemmatize(v) and v2 is the past participle of v (v = verb of wordList)
    lDirect = [lem[1]]  # the past participle is always a predicate
    lInverse = []
    if nManual.exists(lem[0]):  # try to nounify the whole verb v...
        lDirect += nManual.directNouns(lem[0])
        lInverse += nManual.inverseNouns(lem[0])
    elif len(lem[0].split()) > 1 and nManual.exists(lem[0].split(
    )[0]):  # ...otherwise, try to nounify the verb withouts its particles...
        lDirect += nManual.directNouns(lem[0].split()[0])
        lInverse += nManual.inverseNouns(lem[0].split()[0])
    elif nAuto.exists(
            lem[0].split()[0]):  # ...otherwise use the automatic nounification
        lDirect += nAuto.directNouns(lem[0].split()[0])
    # Production of the resource
    if len(lDirect) == 1:  # at least 1 predicate (past part always added)
        if len(lInverse) == 0:  # no inverse predicate
            return (Resource(lDirect[0]), None)
        elif len(lInverse) == 1:  # 1 inverse predicate
            return (Resource(lDirect[0]), Resource(lInverse[0]))
        else:  # >1 inverse predicates
            return (Resource(lDirect[0]), List([Resource(x)
                                                for x in lInverse]))
    else:  # len(lDirect) > 1
        if len(lInverse) == 0:
            return (List([Resource(x) for x in lDirect]), None)
        elif len(lInverse) == 1:
            return (List([Resource(x)
                          for x in lDirect]), Resource(lInverse[0]))
        else:
            return (List([Resource(x) for x in lDirect]),
                    List([Resource(x) for x in lInverse]))
コード例 #13
0
 def testInverse(self):
     self.assertEqual(T(R('foo'), R('bar'), R('baz'), R('qux')).inverse(),
             T(R('baz'), R('qux'), R('foo'), R('bar')))
     self.assertEqual(T(R('foo'), R('bar'), R('baz')).inverse(),
             T(R('baz'), List([]), R('foo'), R('bar')))
コード例 #14
0
 def testPredicateSet(self):
     self.assertEqual(T(M(), R('foo'), M()).predicate_set,
             {R('foo')})
     self.assertEqual(T(M(), List([R('foo'), R('bar')]), M()).predicate_set,
             {R('foo'), R('bar')})
コード例 #15
0
 def testInclusionBasic(self):
     self.assertIncluded(Missing(), Missing())
     self.assertIncluded(Resource('foo'), Resource('foo'))
     with self.assertRaises(AssertionError):
         self.assertIncluded(Resource('foo'), Resource('bar'))
     self.assertIncluded(List([Resource('foo')]), Resource('foo'))
     with self.assertRaises(AssertionError):
         self.assertIncluded(List([Resource('foo')]), Resource('bar'))
     self.assertIncluded(Resource('foo'), List([Resource('foo')]))
     with self.assertRaises(AssertionError):
         self.assertIncluded(Resource('foo'), List([Resource('bar')]))
     self.assertIncluded(List([Resource('foo')]), List([Resource('foo')]))
     with self.assertRaises(AssertionError):
         self.assertIncluded(List([Resource('foo')]),
                             List([Resource('bar')]))
     self.assertIncluded(List([Resource('foo')]),
                         List([Resource('foo'),
                               Resource('bar')]))
     with self.assertRaises(AssertionError):
         self.assertIncluded(List([Resource('foo'),
                                   Resource('bar')]),
                             List([Resource('foo')]))
コード例 #16
0
    def testTraverse(self):
        def pred(tree):
            if isinstance(tree, Resource):
                return Resource('foo')
            elif isinstance(tree, Triple):
                return Triple(tree.subject, Resource('bar'), tree.object)
            elif isinstance(tree, Missing):
                return Resource('m')
            else:
                return tree

        f = Resource('foo')
        b = Resource('bar')
        m = Resource('m')
        self.assertEqual(Resource('baz').traverse(pred), f)
        tree = Triple(Resource('1'), Resource('2'), Missing())
        self.assertEqual(tree.traverse(pred), Triple(f, b, m))
        tree = List([Resource('4'), Resource('5')])
        self.assertEqual(tree.traverse(pred), List([f, f]))
        tree = Triple(Triple(Resource('1'), Resource('2'), Missing()),
                      Resource('3'), List([Resource('4'),
                                           Resource('5')]))
        self.assertEqual(tree.traverse(pred),
                         Triple(Triple(f, b, m), b, List([
                             f,
                             f,
                         ])))
        tree = Union([List([Resource('1')]), List([Resource('2')])])
        self.assertEqual(tree.traverse(pred), Union([List([f]), List([f])]))
        tree = Intersection([List([Resource('1')]), List([Resource('2')])])
        self.assertEqual(tree.traverse(pred),
                         Intersection([List([f]), List([f])]))
コード例 #17
0
 def testFirst(self):
     first(List([Resource('foo'), Resource('bar')]))