コード例 #1
0
 def testToJsonMissing(self):
     n = Triple(subject=R('s'), predicate=R('p'))
     self.assertEqual(json.loads(n.as_json()), {
         'type': 'triple',
         'subject': r('s'),
         'predicate': r('p')
     })
コード例 #2
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)
コード例 #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 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])]))
コード例 #5
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)
コード例 #6
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]))
コード例 #7
0
 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
コード例 #8
0
 def pred(node):
     if isinstance(node, Exists):
         return Resource('qux')
     elif isinstance(node, List):
         return Triple(Resource('baz'), Missing(), Missing())
     elif isinstance(node, Resource):
         return Resource('bar')
     else:
         assert False, node
コード例 #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')))
コード例 #10
0
ファイル: test_definition.py プロジェクト: ProjetPP/PPP-OEIS
 def testBasics(self):
     q = Request(
         '1', 'en',
         Triple(Resource('1 2 4 8'), Resource('definition'), Missing()), {},
         [])
     r = self.request(q)
     self.assertGreater(len(r), 1, r)
     self.assertEqual(r[0].tree.value, 'Powers of 2: a(n) = 2^n.')
     self.assertEqual(r[0].tree.graph['name'], 'Powers of 2: a(n) = 2^n.')
     self.assertEqual(r[0].tree.graph['@id'], 'http://oeis.org/A000079')
コード例 #11
0
 def testInclusionDifferentType(self):
     l = [Resource('foo'), Missing(), Triple(Resource('foo'),
         Resource('foo'), Resource('foo')),\
         Intersection([Resource('foo')]), Union([Resource('foo')]),\
         And([Resource('foo')]), Or([Resource('foo')]),
         Exists(Resource('foo')), first(Resource('foo')),
         last(Resource('foo')), Sort(Resource('foo'), Resource('pred'))]
     for (t1, t2) in itertools.permutations(l, 2):
         with self.assertRaises(AssertionError):
             self.assertFalse(self.assertIncluded(t1, t2))
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
コード例 #13
0
def normalize(tree):
    """
        Map the tree to a normal form
    """
    if tree.child == []:  # leaf
        return buildValue(tree)
    if tree.child[0].dependency == 'Rexist':
        return Exists(normalize(tree.child[0]))
    if tree.child[0].dependency == 'Rspl':
        return normalizeSuperlative(tree)
    if tree.child[0].dependency.startswith('Rconj'):
        return normalizeConjunction(tree)
    result = []
    for t in tree.child:
        if t.dependency == 'R0':
            result.append(normalize(t))
        if t.dependency == 'R1':
            result.append(buildValue(t))
        if t.dependency == 'R2':
            pred = buildPredicate(tree)
            if pred[1]:
                result.append(Triple(normalize(t), pred[0], Missing(),
                                     pred[1]))
            else:
                result.append(Triple(normalize(t), pred[0], Missing()))
        if t.dependency == 'R3':
            pred = buildPredicate(tree)
            if pred[1]:
                result.append(Triple(Missing(), pred[0], normalize(t),
                                     pred[1]))
            else:
                result.append(Triple(Missing(), pred[0], normalize(t)))
        if t.dependency == 'RinstOf':
            result.append(
                Triple(Missing(), Resource('instance of'), normalize(t)))
    if len(result) == 1:
        return result[0]
    else:
        return Intersection(result)
コード例 #14
0
 def testBasics(self):
     q = Request(
         '1', 'en',
         Triple(Resource('1 2 4 8'), Resource('following'), Missing()), {},
         [])
     r = self.request(q)
     self.assertGreater(len(r), 1, r)
     self.assertTrue(r[0].tree.value.startswith('16, 32, 64'), r[0])
     self.assertEqual(r[0].tree.graph['name'], 'Powers of 2: a(n) = 2^n.')
     self.assertEqual(r[0].tree.graph['@id'], 'http://oeis.org/A000079')
     self.assertEqual(r[0].tree.graph['description'][0], {
         '@value': '2^0 = 1 is the only odd power of 2.',
         '@language': 'en'
     })
コード例 #15
0
 def testToJsonNone(self):
     n = Triple(subject=R('s'), predicate=R('p'), object=Missing())
     self.assertEqual(json.loads(n.as_json()), {'type': 'triple',
         'subject': r('s'), 'predicate': r('p'), 'object': m()})
コード例 #16
0
 def testBasicConstructor(self):
     n = Triple(subject=R('s'), predicate=R('p'), object=R('o'))
     self.assertEqual(n.subject, R('s'))
     self.assertEqual(n['subject'], R('s'))
     self.assertRaises(AttributeError, lambda: n.foobar)
コード例 #17
0
ファイル: test_definition.py プロジェクト: ProjetPP/PPP-OEIS
 def testNoAnswer(self):
     q = Request('1', 'en',
                 Triple(Resource('1 2'), Resource('definition'), Missing()),
                 {}, [])
     r = self.request(q)
     self.assertEqual(r, [])
コード例 #18
0
 def testTripleIntersection(self):
     t = Intersection([
         Triple(Resource('a'), Resource('b'), Missing()),
         Triple(Resource('c'), Resource('d'), Missing())
     ])
     AbstractNode.from_dict(t.as_dict())
コード例 #19
0
 def testSet(self):
     n = Triple(subject=R('s'), predicate=R('p'), object=R('o'))
     with self.assertRaises(TypeError):
         n.predicate = R('test')
     with self.assertRaises(TypeError):
         del n.predicate
コード例 #20
0
 def testEmptyConstructor(self):
     n = Triple()
     self.assertRaises(exceptions.AttributeNotProvided, lambda: n.predicate)
コード例 #21
0
 def testSet(self):
     n = Triple(subject=R('s'), predicate=R('p'), object=R('o'))
     with self.assertRaises(TypeError):
         n.predicate = R('test')
     with self.assertRaises(TypeError):
         del n.predicate
コード例 #22
0
 def testToJsonPerfect(self):
     n = Triple(subject=R('s'), predicate=R('p'), object=R('o'))
     self.assertEqual(json.loads(n.as_json()), {'type': 'triple',
         'subject': r('s'), 'predicate': r('p'), 'object': r('o')})
コード例 #23
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])]))