def testFromJson(self):
     j = {'id': '1', '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)
Exemplo n.º 2
0
 def testBlacklist(self):
     with HTTMock(my_module1_mock, my_module2_mock):
         q = Request('1', 'en', Sentence('foo'), {}, [])
         answers = self.request(q)
         self.assertEqual(len(answers), 2, answers)
         q = Request('1', 'en', Missing(), {}, [])
         answers = self.request(q)
         self.assertEqual(len(answers), 1, answers)
         self.assertEqual(answers[0].tree.value, 'one')
Exemplo n.º 3
0
    def testSentence(self):
        q = Request('1', 'en', Sentence('1, 2, 4, 8'), {}, [])
        r = self.request(q)
        self.assertGreater(len(r), 1, r)
        self.assertEqual(r[0].tree.value, 'Powers of 2: a(n) = 2^n.')

        q = Request('1', 'en', Sentence('1, 2, 4, 8?'), {}, [])
        r = self.request(q)
        self.assertGreater(len(r), 1, r)
        self.assertEqual(r[0].tree.value, 'Powers of 2: a(n) = 2^n.')
Exemplo n.º 4
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'))
Exemplo n.º 5
0
def my_module2_mock(url, request):
    body = Request.from_json(request.body)
    m = {'relevance': 0.3, 'accuracy': 1}
    response = Response('en', body.tree, m,
                        [TraceItem('module2', body.tree, m)])
    return {'status_code': 200,
            'content': '[%s]' % response.as_json()}
Exemplo n.º 6
0
    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 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)
Exemplo n.º 8
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)
Exemplo n.º 9
0
 def testNoDuplicate(self):
     q = Request('1', 'en', Missing(), {}, [])
     with HTTMock(my_module3_mock, my_module3b_mock):
         answers = self.request(q)
         self.assertNotEqual(len(answers), 20, answers)
         self.assertNotEqual(len(answers), 10, answers)
         self.assertNotEqual(len(answers), 2, answers)
         self.assertEqual(len(answers), 1, answers)
Exemplo n.º 10
0
 def testQueriesMultipleModuleWithFail(self):
     self.config_file.write(one_valid_module_config)
     self.config_file.seek(0)
     q = Request('1', 'en', Missing(), {}, [])
     m = {'relevance': 0.5, 'accuracy': 0.5}
     with HTTMock(my_module_mock, my_module4_mock):
         self.assertResponse(q, [
             Response('en', Missing(), m,
                      [TraceItem('module1', Missing(), m)])])
Exemplo n.º 11
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')
Exemplo n.º 12
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': '[]'}
Exemplo n.º 13
0
 def testIntersection(self):
     q = Request('1', 'en', Intersection([
         Triple(
             Resource('A Hierarchical Resource Reservation Algorithm for Network Enabled Servers of the french department of research.'),
             Resource('author'),
             Missing()),
         Triple(
             Resource('Deployment of a hierarchical middleware'),
             Resource('author'),
             Missing())]))
     q.__class__.from_dict(q.as_dict())
     r = self.request(q)
     self.assertEqual(len(r), 1, r)
     self.assertEqual({x.value for x in r[0].tree.list},
             {'Eddy Caron', 'Frédéric Desprez'})
     ec = JsonldResource('EC', graph={'@id': EC_ID})
     fd = JsonldResource('FD', graph={'@id': FD_ID})
     self.assertIn(ec, r[0].tree.list)
     self.assertIn(fd, r[0].tree.list)
     self.assertIn(r[0].tree.list, ([ec, fd], [fd, ec]))
Exemplo n.º 14
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': '[]'}
Exemplo n.º 15
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'
     })
Exemplo n.º 16
0
 def testPython(self):
     with HTTMock(my_module1_mock):
         q = Request('1', 'en', Sentence('foo'), {}, [])
         (router.logger, logger) = (FakeLogger(), router.logger)
         try:
             answers = self.request(q)
         finally:
             (fakelogger, router.logger) = (router.logger, logger)
         self.assertEqual(len(answers), 3, answers)
         self.assertEqual(
             len(list([x for x in answers if x.tree.value == 'foo'])), 1)
         self.assertEqual(
             len(list([x for x in answers if x.tree.value == 'one'])), 2)
         self.assertEqual(len(fakelogger._errors), 4, fakelogger._errors)
 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'))
Exemplo n.º 18
0
 def testQueriesMultipleModule(self):
     self.config_file.write(three_modules_config)
     self.config_file.seek(0)
     q = Request('1', 'en', Missing(), {}, [])
     m1 = {'relevance': 0.5, 'accuracy': 0.5}
     m2 = {'relevance': 0.3, 'accuracy': 1}
     m3 = {'relevance': 0.55, 'accuracy': 0.5}
     with HTTMock(my_module_mock, my_module2_mock, my_module3_mock):
         self.assertResponse(q, [
             Response('en', Missing(), m3,
                      [TraceItem('module3', Missing(), m3)]),
             Response('en', Missing(), m1,
                      [TraceItem('module1', Missing(), m1)]),
             Response('en', Missing(), m2,
                      [TraceItem('module2', Missing(), m2)]),
             ])
Exemplo n.º 19
0
    def triples(self, irc, msg, args, channel, sentence):
        """<request>

        Sends a request to the PPP and returns the triples."""
        r = Request(id='supybot-%s' % uuid.uuid4().hex,
                    language=self.registryValue('language', channel),
                    tree=Sentence(value=sentence))
        responses = self.request(channel, r)
        responses = map(operator.attrgetter('tree'), responses)
        responses = filter(lambda x: isinstance(x, Triple), responses)
        bold = self.registryValue('formats.bold')
        L = [format_triple(x, bold) for x in responses]
        if L:
            irc.replies(L)
        else:
            irc.error(_('No response'))
Exemplo n.º 20
0
    def query(self, irc, msg, args, channel, sentence):
        """<request>

        Sends a request to the PPP and returns answers."""
        r = Request(id='supybot-%s' % uuid.uuid4().hex,
                    language=self.registryValue('language', channel),
                    tree=Sentence(value=sentence))
        format_ = self.registryValue('formats.query', channel)
        responses = (x.tree for x in self.request(channel, r))
        formatter = functools.partial(self.format_response, channel, format_)
        L = list(
            unique(
                filter(
                    bool,
                    itertools.chain.from_iterable(map(formatter, responses)))))
        if L:
            irc.replies(L)
        else:
            irc.error(_('No response'))
Exemplo n.º 21
0
    def process_request(self, request):
        """Processes a request."""
        try:
            request = Request.from_json(request.read().decode())
        except ValueError:
            raise ClientError('Data is not valid JSON.')
        except KeyError:
            raise ClientError('Missing mandatory field in request object.')
        except AttributeNotProvided as exc:
            raise ClientError('Attribute not provided: %s.' % exc.args[0])

        (start_wall_time, start_process_time) = self._get_times()
        answers = self.router_class(request).answer()
        self._add_times_to_answers(answers, start_wall_time, start_process_time)

        answers = [x.as_dict() for x in answers]
        return self.make_response('200 OK',
                                  'application/json',
                                  json.dumps(answers)
                                 )
Exemplo n.º 22
0
 def testNoAnswer(self):
     q = Request('1', 'en',
                 Triple(Resource('1 2'), Resource('definition'), Missing()),
                 {}, [])
     r = self.request(q)
     self.assertEqual(r, [])
Exemplo n.º 23
0
 def request_from_answer(self, answer):
     return Request(self.id, answer.language, answer.tree, answer.measures,
                    answer.trace)
Exemplo n.º 24
0
def get_formula():
    """
        Get English question from the user parse it to Questionparsing module to get Triple
        Parse Triple (Subject, predicate, ?) to FormulaRequestHandler to get Formula from Wikidata
        Return response
    """

    # parse question
    try:
        question = request.form['formula']
        # lowercase first letter of question
        question = question[:1].lower() + question[1:]
        print("Question: " + question)

        # Determine question type

        # identifier symbol question
        if "symbol" in question:
            mode = 'symbol_question'
            print("Symbol question")

            exclude = ["what", "is", "the", "symbol", "for", "?"]
            subject = get_input(question, exclude)

            formula = get_identifier_symbol(identifier_name=subject)

        # relationship question (semantic search)
        elif "relationship" in question or "relation" in question:
            mode = 'relationship_question'
            print("Relationship question")

            exclude = [
                "what", "is", "the", "relationship", "relation", "between",
                "and", "?"
            ]
            input = get_input(question, exclude)

            # check if input is identifier names or symbols
            # symbols if all characters
            #
            #symbols = True
            #for element in input:
            #    if len(element) > 1:
            #        symbols = False
            # names if at least one is word
            #if symbols:
            #    mode_number = 1
            #else:
            #    mode_number = 2
            #
            # results = search_formulae_by_identifier_names\
            #     (identifier_names=identifier_names,catalog="NTCIR-12_arXiv_astro-ph"\
            #      ,inverse=True,multiple=False)
            #results = search_formulae_by_identifiers(input=input,
            #                                                mode_number=mode_number)
            results, subject = search_formulae_by_identifiers_Wikidata(
                identifiers=input)

            formula = list(results.items())[0][0].split(" (")[0]

        # Formula question
        elif "formula" in question:
            mode = 'formula_question'
            print("Formula question")

            exclude = ["what ", "is ", "the ", "formula ", "for ", "?"]
            for word in exclude:
                question = question.replace(word, "")
            # strip whitespace (at beginning) and end
            #subject = question[1:].strip()
            subject = question.strip()

            formula = search_formulae_by_concept_name_Wikidata(subject)

        # General or geometry question
        else:
            mode = 'general_geometry'
            print("General or geometry question")
            meas = {'accuracy': 0.5, 'relevance': 0.5}
            q = RequestHandler(
                Request(language="en",
                        id=1,
                        tree=Sentence(question),
                        measures=meas))
            try:
                query = q.answer()
            except:
                print("Stanford CoreNLP was unable to parse the question")
            reques = FormulaRequestHandler(query)

            subject = reques.request[0]._attributes['tree']._attributes[
                'subject']._attributes['value']

            formula = reques.answer()
            formula = latexformlaidentifiers.prepformula(formula)

        # generate response
        if not (formula.startswith("System")):
            return makeresponse(formula, subject, mode)
        else:
            response = jsonify(formula)
            response.status_code = 202
            print(response)
            return response

    except Exception:
        response = jsonify("System is not able to find the result.")
        response.status_code = 202
        return response
Exemplo n.º 25
0
def getAnswer(sentence=None):
    if not sentence:
        sentence = eval(input(""))
    q = RequestHandler(Request(language="en",id=1,tree=Sentence(sentence)))
    return q.answer()