Example #1
0
    def get_queries(self, question):
        """
        Given `question` in natural language, it returns
        three things:

        - the target of the query in string format
        - the query
        - metadata given by the regex programmer (defaults to None)

        The queries returned corresponds to the regexes that match in
        weight order.
        """
        question = encoding_flexible_conversion(question)
        for expression, userdata in self._iter_compiled_forms(question):
            target, sparql_query = expression_to_sparql(expression)
            logger.debug(u"Semantics {1}: {0}".format(str(expression),
                         expression.rule_used))
            logger.debug(u"Query generated: {0}".format(sparql_query))
            yield target, sparql_query, userdata
Example #2
0
    def get_queries(self, question):
        """
        Given `question` in natural language, it returns
        three things:

        - the target of the query in string format
        - the query
        - metadata given by the regex programmer (defaults to None)

        The queries returned corresponds to the regexes that match in
        weight order.
        """
        question = encoding_flexible_conversion(question)
        for expression, userdata in self._iter_compiled_forms(question):
            target, sparql_query = expression_to_sparql(expression)
            logger.debug(u"Semantics {1}: {0}".format(str(expression),
                                                      expression.rule_used))
            logger.debug(u"Query generated: {0}".format(sparql_query))
            yield target, sparql_query, userdata
Example #3
0
def expressions_to_html(expressions):
    rows = ""
    for expression in expressions:
        dot_string = expression_to_dot(expression)
        target, query = expression_to_sparql(expression)

        dot_path = "/tmp/quepy_graph.dot"
        cmdline = "dot -Tpng %s" % dot_path

        with open(dot_path, "w") as filehandler:
            filehandler.write(dot_string)

        try:
            call = subprocess.Popen(cmdline.split(), stdout=subprocess.PIPE)
            output, _ = call.communicate()
        except OSError:
            msg = "Error running '{}': the program 'dot' was not found."
            print msg.format(cmdline)
            sys.exit(1)

        image_base64 = base64.b64encode(output)

        query = "\n".join([x for x in query.split("\n")
                           if not x.startswith("PREFIX")])
        query = escape(query)
        code = expression.source_code.split("\n")
        name = code[0].replace("def", "").replace("_", " ").strip(" :()")
        code = "\n".join(code[1:-2])
        rows += QUERY_TEMPLATE.format(name=name.capitalize(),
                                      code=code,
                                      sparql=query,
                                      image_base64=image_base64)

    html = HTML_TEMPLATE.format(rows=rows)
    with open("expression_inform.html", "w") as filehandler:
        filehandler.write(html)
Example #4
0
 def test_sparql_takes_unicode(self):
     e = gen_fixedtype(u"·̣─@łæßð~¶½")
     e += gen_datarel(u"tµŧurułej€", u"←ðßðæßđæßæđßŋŋæ @~~·ŋŋ·¶·ŋ“¶¬@@")
     _, s = expression_to_sparql(e)
     self._standard_check(s, e)
Example #5
0
 def test_sparql_takes_unicode(self):
     e = gen_fixedtype(u"·̣─@łæßð~¶½")
     e += gen_datarel(u"tµŧurułej€", u"←ðßðæßđæßæđßŋŋæ @~~·ŋŋ·¶·ŋ“¶¬@@")
     _, s = expression_to_sparql(e)
     self._standard_check(s, e)