Exemple #1
0
 def testRecursion(self):
     q = Request('1', 'en', Resource('one'), {}, [])
     with HTTMock(my_module_mock, my_module2_mock, my_module3_mock):
         answers = self.request(q)
         self.assertEqual(len(answers), 2, answers)
         self.assertEqual(answers[0].tree, Resource('three'))
         self.assertEqual(answers[1].tree, Resource('two'))
 def testFromJson(self):
     r = {
         'language':
         'en',
         'measures': {},
         'trace': [{
             'module': 'foo',
             'tree': {
                 'type': 'missing'
             },
             'measures': {},
             'times': {}
         }],
         'tree': {
             'type': 'resource',
             'value': 'foo'
         }
     }
     t = [TraceItem('foo', Missing(), {}, {})]
     self.assertEqual(Response('en', Resource(value='foo'), {}, t),
                      Response.from_json(json.dumps(r)))
     self.assertEqual(Response('en', Resource(value='foo'), {}, t),
                      Response.from_dict(r))
     self.assertEqual(Response('en', Resource(value='foo'), {}, t),
                      Response.from_json(json.dumps(r)))
     self.assertEqual(json.loads(Response.from_dict(r).as_json()), r)
Exemple #3
0
 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)
Exemple #4
0
 def testList(self):
     r1 = {'type': 'resource', 'value': 'foo'}
     r2 = {'type': 'resource', 'value': 'bar'}
     d = {'type': 'list', 'list': [r1, r2]}
     o = AbstractNode.from_dict(d)
     self.assertEqual(o.list, [Resource('foo'), Resource('bar')])
     self.assertIsInstance(o.list[1], Resource)
     self.assertEqual(o.as_dict(), d)
Exemple #5
0
 def testEq(self):
     self.assertEqual(Missing(), Missing())
     self.assertEqual(Resource('foo'), Resource('foo'))
     self.assertNotEqual(Missing(), Resource('foo'))
     self.assertEqual(Missing(), {'type': 'missing'})
     self.assertNotEqual(Missing(), {'type': 'missing', 'f': 'b'})
     self.assertEqual(Resource('foo'), {'type': 'resource', 'value': 'foo'})
     self.assertNotEqual(Missing(), '')
Exemple #6
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
 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
Exemple #8
0
 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')
 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()
 def pred(node):
     if isinstance(node, Exists):
         self.assertEqual(node.list, Resource('baz'))
         return Resource('qux')
     elif isinstance(node, List):
         self.assertEqual(node.list, [Resource('bar')])
         return Resource('baz')
     elif isinstance(node, Resource):
         return Resource('bar')
     else:
         assert False, node
Exemple #11
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)
Exemple #12
0
 def testValueType(self):
     d = {'type': 'resource', 'value': 'foo', 'value-type': 'bar',
          'extra': 'baz'}
     o = AbstractNode.from_dict(d)
     self.assertEqual(o.value_type, 'bar')
     self.assertFalse(hasattr(o, 'extra'))
     self.assertRaises(AttributeError, o.get, 'extra')
     self.assertEqual(o.get('extra', strict=False), 'baz')
     self.assertEqual(o.as_dict(), d)
     self.assertEqual(Resource('foo').traverse(lambda x:x),
             Resource('foo'))
     self.assertEqual(Resource('type').traverse(lambda x:x),
             Resource('type'))
    def testTraverseReturnTriple(self):
        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

        self.assertEqual(
            Exists(Resource('foo')).traverse(pred), Resource('qux'))
Exemple #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'
     })
    def testTraverseOrder(self):
        def pred(node):
            if isinstance(node, Exists):
                self.assertEqual(node.list, Resource('baz'))
                return Resource('qux')
            elif isinstance(node, List):
                self.assertEqual(node.list, [Resource('bar')])
                return Resource('baz')
            elif isinstance(node, Resource):
                return Resource('bar')
            else:
                assert False, node

        self.assertEqual(
            Exists(Resource('foo')).traverse(pred), Resource('qux'))
    def testVerboseLog(self):
        q = Request('1', 'en', Resource('one'), {}, [])
        with HTTMock(my_module_mock):
            answers = self.request(q)

        time.sleep(0.5)

        conn = sqlite3.connect(self.fd.name)
        with conn:
            r = conn.execute(
                'SELECT request_handling_start_time, request_handling_end_time, request_answers_json FROM requests;'
            ).fetchall()
            fields = ('start', 'end', 'answers')
            zipper = lambda x: {
                'start': x[0],
                'end': x[1],
                'answers': json.loads(x[2])
            }
            r = list(map(zipper, r))

        self.assertEqual(len(r), 1, r)
        self.assertAlmostEqual(r[0]['start'], time.time(), delta=1.)
        self.assertAlmostEqual(r[0]['end'], time.time(), delta=1.)
        self.assertEqual(len(r[0]['answers']), 1, r[0]['answers'])
        self.assertEqual(set(r[0]['answers'][0]),
                         {'language', 'tree', 'measures', 'trace'})
 def testWhoAreYou(self):
     t = {
         'type': 'triple',
         'subject': {
             'type': 'resource',
             'value': 'you'
         },
         'predicate': {
             'type': 'resource',
             'value': 'identity'
         },
         'object': {
             'type': 'missing'
         }
     }
     q = {
         'id': '1',
         'language': 'en',
         'measures': {},
         'trace': [],
         'tree': t
     }
     answers = self.request(q)
     self.assertEqual(len(answers), 1, answers)
     answer = answers[0].tree
     self.assertEqual(answer, Resource('A question-answering tool.'))
Exemple #18
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]))
def buildValue(tree):
    """
        Lemmatize the wordList and build a Resource from it
    """
    if tree.namedEntityTag == 'undef':
        for w in tree.wordList:
            w.word = lemmatize(w.word, w.pos)
    return Resource(tree.getWords())
 def testFromJson(self):
     j = {
         'id': '1',
         'language': 'en',
         'response-language': 'en',
         'measures': {},
         'trace': [],
         'tree': {
             'type': 'resource',
             'value': 'foo'
         }
     }
     self.assertEqual(Request('1', 'en', Resource(value='foo'), {}, []),
                      Request.from_dict(j))
     self.assertEqual(Request('1', 'en', Resource(value='foo'), {}, []),
                      Request.from_json(json.dumps(j)))
     self.assertEqual(json.loads(Request.from_dict(j).as_json()), j)
Exemple #21
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)
Exemple #22
0
 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)
    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)
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
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]))
 def testPEqNp(self):
     t = {'type': 'sentence', 'value': 'P=NP?'}
     q = {
         'id': '1',
         'language': 'en',
         'measures': {},
         'trace': [],
         'tree': t
     }
     answers = self.request(q)
     self.assertEqual(len(answers), 1, answers)
     answer = answers[0].tree
     self.assertEqual(answer, Resource('Maybe.'))
Exemple #27
0
def my_module2_mock(url, request):
    r = Request.from_json(request.body)
    if r.tree == Resource('two'):
        c = '"measures": {"accuracy": 1, "relevance": 2}, "tree": {"type": "resource", "value": "three"}'
        return {
            'status_code':
            200,
            'content':
            '[{"language": "en", %s, '
            '"trace": [{"module": "module1", %s}]}]' % (c, c)
        }
    else:
        return {'status_code': 200, 'content': '[]'}
 def testEquality(self):
     self.assertEqual(Response('en', Resource(value='foo'), {}, []),
                      Response('en', Resource(value='foo'), {}, []))
     self.assertNotEqual(Response('en', Resource(value='foo'), {}, []),
                         Response('en', Resource(value='bar'), {}, []))
     self.assertNotEqual(
         Response('en', Resource(value='foo'), {'accuracy': 0.5}, []),
         Response('en', Resource(value='foo'), {'accuracy': 0.6}, []))
def normalizeSuperlative(tree):
    """
        Handle Rspl dependency (superlative, ordinal)
    """
    order = buildValue(tree).value.split(
        ' ', 1)[0]  # most important > most, deepest > deepest
    predicate = buildValue(tree).value.split(
        ' ', 1)[-1]  # most important > important, deepest > deepest
    if predicate in superlativeNoun:
        if order in superlativeOrder:
            return superlativeOrder[order](Sort(
                normalize(tree.child[0]),
                Resource(superlativeNoun[predicate])))
        else:
            return first(
                Sort(normalize(tree.child[0]),
                     Resource(superlativeNoun[predicate])))  # First by default
    else:
        if order in superlativeOrder:
            return superlativeOrder[order](Sort(normalize(
                tree.child[0]), Resource('default')))  # default predicate
        else:
            return first(Sort(normalize(tree.child[0]), Resource('default')))
 def testEquality(self):
     self.assertEqual(
         Request('1', 'en', Resource(value='foo'), {}, []),
         Request('1', 'en', Resource(value='foo'), {}, [], 'en'))
     self.assertNotEqual(Request('1', 'en', Resource(value='foo'), {}, []),
                         Request('1', 'en', Resource(value='bar'), {}, []))
     self.assertNotEqual(
         Request('1', 'en', Resource(value='foo'), {}, []),
         Request('1', 'en', Resource(value='foo'), {}, [], 'fr'))