def GET(self): query_string = web.input() query_type = query_string.type return_model = ReturnModel("", None) if query_type == 'sparql': dbpedia = quepy.install("dbpedia") return_model = dbpedia.get_query(query_string.q) elif query_type == 'mql': freebase = quepy.install("freebase") return_model = freebase.get_query(query_string.q) web.header('Content-Type', 'application/json') return demjson.encode(return_model.toJSON())
def setUp(self): multi_dbs = quepy.install("testapp") self.subqueries = [ {'db': 'http://dbpedia.org/sparql', 'query': u"""\ ?x0 rdf:type foaf:Person. ?x0 rdfs:label "Akira Takarada"@en. ?movie rdf:type dbpedia-owl:Film. ?movie dbpprop:starring ?x0. """}, {'db': 'http://dbpedia.org/sparql', 'query': u"""\ ?x20 rdf:type skos:Concept . ?x20 rdfs:label "Giant monster films"@en . ?movie rdf:type dbpedia-owl:Film . ?movie dcterms:subject ?subject . """}, {'db': 'http://data.linkedmdb.org/sparql', 'query': u""" ?x30 owl:sameAs ?movie. ?x30 movie:sequel ?movie2 . """}, {'db': 'http://data.linkedmdb.org/sparql', 'query': u""" ?movie2 rdfs:label ?output. """}] self.subquestion = \ Subquestion(self.subqueries, multi_dbs.rules)
def GET(self): query_string = web.input() dbpedia = quepy.install("dbpedia") return_model = dbpedia.get_query(query_string.q) web.header('Content-Type', 'application/json') return demjson.encode(return_model.toJSON())
def __init__(self, host, user, passwd, db): self.connection = MySQLdb.connect(host = host, user = user, passwd = passwd, db = db) self.dbbase = quepy.install("imdb") self.cursor = self.connection.cursor()
def question_process(text): #_ajax if text is None: return False else: quepy_stibd = quepy.install("quepy_stibd") target, query, metadata = quepy_stibd.get_query(text) print target, query, metadata target, query, metadata = quepy_stibd.get_query( "where in the world is the Eiffel Tower?") print target, query, metadata target, query, metadata = quepy_stibd.get_query( "List Microsoft software" ) #Which is Cindy's email? Who is Jon Snow? print target, query, metadata return True
def question(question): try: # Variable global para mantener la conexion a quepy.. Tal vez no sea necesario quepy_stibd = None if quepy_stibd is None: quepy_stibd = quepy.install("quepy_stibd") target, query, metadata = quepy_stibd.get_query(question) print query # run query try: #pass a, b = sparql_call(query, target) #sparql_call(query,target) except Exception as e: print e.message raise Http404 except Exception as e: print "errorrr", e.message raise Http404
def __init__(self, app_name): settings = importlib.import_module( 'neuroarch_nlp.{}.settings'.format(app_name)) grammar = importlib.import_module( 'neuroarch_nlp.{}.grammar'.format(app_name)) modifiers_and_regions = grammar.modifiers_and_regions arborization_regions = grammar.arborization_regions self.na_unigrams = { unigram for term in list(modifiers_and_regions.keys()) + list(colors_values.keys()) + list(arborization_regions.keys()) for unigram in term.split() } self.na_unigrams.update(na_unigrams) settings.PARSER = 'spaCy' self.translator = quepy.install('neuroarch_nlp.{}'.format( app_name)) #quepy.install('neuroarch_nlp.quepy_analysis') self.translate = self.translator.get_query
def setUp(self): multi_dbs = quepy.install("testapp") self.subqueries = [{ 'db': 'http://dbpedia.org/sparql', 'query': u"""\ ?x0 rdf:type foaf:Person. ?x0 rdfs:label "Akira Takarada"@en. ?movie rdf:type dbpedia-owl:Film. ?movie dbpprop:starring ?x0. """ }, { 'db': 'http://dbpedia.org/sparql', 'query': u"""\ ?x20 rdf:type skos:Concept . ?x20 rdfs:label "Giant monster films"@en . ?movie rdf:type dbpedia-owl:Film . ?movie dcterms:subject ?subject . """ }, { 'db': 'http://data.linkedmdb.org/sparql', 'query': u""" ?x30 owl:sameAs ?movie. ?x30 movie:sequel ?movie2 . """ }, { 'db': 'http://data.linkedmdb.org/sparql', 'query': u""" ?movie2 rdfs:label ?output. """ }] self.subquestion = \ Subquestion(self.subqueries, multi_dbs.rules)
def quepyProcess(question=None): success = True nlq_answer = "" query = "" results = "" sparql = SPARQLWrapper("http://115.146.84.150/annex/openimpact/sparql/query") dbpedia = quepy.install("dbpedia") # quepy.set_loglevel("DEBUG") target, query, metadata = dbpedia.get_query(urllib.unquote(question)) # Quepy does its thing. try: if target.startswith("?"): target = target[1:] if query: sparql.setQuery(query) sparql.setReturnFormat(JSON) results = sparql.query().convert() nlq_answer = print_define(results, target, metadata) success=True except: if query is None: query= "" nlq_answer="I have no clue" alert="I'm sorry, Dave. I'm afraid I can't do that. " success=False pass timestamp = datetime.utcnow() sparql_id = urllib.quote_plus(str(query)) return [ Query( sparql_id = sparql_id, sparql_code = query, question = question, votes = None, tags = [], timestamp = timestamp, asks = 1, success = success), nlq_answer]
def query(question): dbpedia = quepy.install("app") print_handlers = { "define": print_define, "enum": print_enum, "time": print_time, "literal": print_literal, "age": print_age, } target, query, metadata = dbpedia.get_query(question) if isinstance(metadata, tuple): query_type = metadata[0] metadata = metadata[1] else: query_type = metadata metadata = None if query is None: print "Query not generated :(\n" return DEFAULT_RESPONSE print query if target.startswith("?"): target = target[1:] if query: sparql.setQuery(query) sparql.setReturnFormat(JSON) results = sparql.query().convert() if not results["results"]["bindings"]: print "No answer found :(" return DEFAULT_RESPONSE #return results return print_handlers[query_type](results, target, metadata)
def setUp(self): self.app = quepy.install("testapp")
# coding: utf-8 """ Main script for prototype1 quepy. """ import quepy import argparse from SPARQLWrapper import SPARQLWrapper, JSON parser = argparse.ArgumentParser(description='Quepy prototype number 1') parser.add_argument("question", help='Question à poser sous la forme "question"') args = parser.parse_args() prototype1 = quepy.install("prototype1") target, query, metadata = prototype1.get_query(args.question) print query sparql = SPARQLWrapper("http://dbpedia.org/sparql") sparql.setQuery(str(query)) sparql.setReturnFormat(JSON) results = sparql.query().convert() for result in results["results"]["bindings"]: print(result["label"]["value"])
# coding: utf-8 """ Main script for test quepy. """ import quepy test = quepy.install("test")
def setUp(self): self.app = quepy.install("dbpedia")
# coding: utf-8 """ Main script for lov quepy. """ import sys import argparse import quepy from SPARQLWrapper import SPARQLWrapper, JSON import lov.printHandlers as printHandlers # using directly the LOV endpoint to query sparql = SPARQLWrapper("http://lov.okfn.org/dataset/lov/sparql") lov = quepy.install("lov") def vocaburi(vocab_url): """ Give the uri of a vocabulary """ query = """ PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT * WHERE { <%s> foaf:primaryTopic ?uri . } """ % vocab_url sparql.setQuery(query) sparql.setReturnFormat(JSON) results = sparql.query().convert()
""" Main script for Freebase quepy. """ import sys import time import random import datetime import json import os sys.path.insert(0, '/Users/simister/quepy') import quepy freebase = quepy.install("freebase") #quepy.set_loglevel("DEBUG") if __name__ == "__main__": default_questions = [ "List movies directed by Martin Scorsese", "which movies did Mel Gibson star in?", "When was Gladiator released?", "who directed Pocahontas?", "actors of Fight Club", ] if "-d" in sys.argv: quepy.set_loglevel("DEBUG") sys.argv.remove("-d")
# coding: utf-8 """ Main script for nl2sparql quepy. """ import quepy nl2sparql = quepy.install("nl2sparql")
def setUp(self): self.app = quepy.install("multi_base")
def question_ajax(request): if request.is_ajax(): question = request.POST.get('question', None) user_id = request.user.id user = User.objects.get(pk=user_id) print str(question) try: # Variable global para mantener la conexion a quepy.. Tal vez no sea necesario #if QUEPY_STIBD is None: QUEPY_STIBD = quepy.install("quepy_stibd") if question is not None: target, query, metadata = QUEPY_STIBD.get_query(question) else: return HttpResponse(json.JSONEncoder().encode( {"error": "No se obtuvo la pregunta"}), mimetype="application/json") # run query print query try: pregunta = Pregunta.objects.get( descripcion__icontains=str(question)) pregunta.ocurrencias = pregunta.ocurrencias + 1 pregunta.save() except Exception as e: print "que paso ", e.message pregunta = Pregunta(descripcion=str(question)) pregunta.save() print "almacenada pregunta" try: pe = PreguntaEstudiante(usuario=user, descripcion=str(question), fecha=date.today(), pregunta=pregunta) pe.save() except Exception as e: print "aca ", e.message pe = PreguntaEstudiante(usuario=user, descripcion=str(question), fecha=date.today(), pregunta=pregunta) pe.save() try: first, results = sparql_call(query, target) try: respuesta = Respuesta.objects.get( descripcion__icontains=first) except Exception as e: respuesta = Respuesta(descripcion=first, pregunta=pregunta) respuesta.save() ''' try: respuesta = Respuesta(descripcion = first,pregunta = pregunta) respuesta.save() except Exception as e: print e.message ''' return HttpResponse(json.JSONEncoder().encode({ "exito": 1, "respuesta": first }), mimetype="application/json") except Exception as e: print e.message raise Http404 except Exception as e: print " quepy install fails ", repr(e), e.message return HttpResponse(json.JSONEncoder().encode( {"error": "No se pudo procesar la pregunta"}), mimetype="application/json") #raise Http404 else: print " ajax request fails" raise Http404
# coding: utf-8 """ Main script for multi_base quepy. """ import quepy multi_dbs = quepy.install("multi_base") question = 'which movie is sequel of a movie about Giant Monster starring' \ ' Akira Takarada' # question = 'what is a blowtorch?' # question = 'about Giant Monster starring Akira Takarada' # question = 'about Giant Monster' # question = 'sequel of a movie' target, query, metadata = multi_dbs.get_query(question) print query
# coding: utf-8 """ Main script for swpedia quepy. """ import quepy swpedia = quepy.install("swpedia")
# coding: utf-8 """ Main script for sample_quepy_project quepy. """ import quepy sample_quepy_project = quepy.install("sample_quepy_project") if __name__ == "__main__": target, query, metadata = sample_quepy_project.get_query( "what is a blowtorch?") print query
print query print ("Metadata: ") print metadata ''' import sys import time import random import datetime import quepy from SPARQLWrapper import SPARQLWrapper, JSON sparql = SPARQLWrapper("http://dbpedia.org/sparql") db = quepy.install("db") # quepy.set_loglevel("DEBUG") def print_define(results, target, metadata=None): for result in results["results"]["bindings"]: if result[target]["xml:lang"] == "en": print result[target]["value"] print def print_enum(results, target, metadata=None): used_labels = [] for result in results["results"]["bindings"]:
__author__ = 'coreyesj' # Set up quepy api from os import sys, path qtestPath = path.dirname(path.dirname(path.abspath(__file__))) + "/quepy_test/quepy_api" sys.path.append(qtestPath) from main import query import quepy from SPARQLWrapper import SPARQLWrapper, JSON sparql = SPARQLWrapper("http://dbpedia.org/sparql") dbpedia = quepy.install("app") def QA(question): # default_questions = [ # "Who killed Abraham Lincoln?" # ] # default_questions = [ # "What is a car?", # "Who is Tom Cruise?", # "Who is George Lucas?", # "Who is Mirtha Legrand?", # # "List Microsoft software", # "Name Fiat cars", # "time in argentina", # "what time is it in Chile?", # "List movies directed by Martin Scorsese", # "How long is Pulp Fiction", # "which movies did Mel Gibson starred?", # "When was Gladiator released?", # "who directed Pocahontas?",
Main script for freebase quepy. Usage: main.py [options] <question> ... Options: -r --request Queries the online database and prints the results """ import json import quepy import urllib from docopt import docopt service_url = 'https://www.googleapis.com/freebase/v1/mqlread' freebase = quepy.install("freebase") def request(query): params = {'query': query} url = service_url + '?' + urllib.urlencode(params) responses = json.loads(urllib.urlopen(url).read()) return responses def result_from_responses(responses, target): if responses: to_explore = responses["result"] for key in target: _to_explore = [] for elem in to_explore:
#!/usr/bin/python # coding: utf-8 """ Main script for ocgquerygen quepy. """ import sys import time import random import datetime import quepy import os import subprocess ocgquerygen = quepy.install("ocgquerygen") def get_query(query): target, query, metadata = ocgquerygen.get_query(query) if query: cmd = "d2rq/d2r-query -f json mapping.n3 \' " + query + "\' > out.json" os.system(cmd) f = open('out.json', 'r') result = f.read() f.close() return result #else: # return "Not Query Generated. :(" #return query
""" import quepy import re import sys from SPARQLWrapper import SPARQLWrapper from spelling import correct from xml_converter import print_cause, print_define_for_error_nlg, print_file_location # regex for validate inputs and outputs regex_for_error_code = re.compile(r'\b ora\s?\W?\d{5}\b', re.I | re.M) regex_for_oracle_file = re.compile(r'\b .*\W?(ora|log)\b', re.I | re.M) # onto url sparql = SPARQLWrapper("http://localhost:3030/ds/query") nova = quepy.install("nova") root_type = "" query_type = "" file_name = "" error_no = "" target_keyword = "" question_type = "" def has_numbers(input_String): return any(char.isdigit() for char in input_String) if __name__ == "__main__": """
# coding: utf-8 """ Main script for Seq2Sql_prototype quepy. """ import signal import quepy seq2sql_prototype = quepy.install("seq2sql_prototype") done = False # The commandline tool is running def signal_handler(signum, frame): print "\n\t\t\t!!! Thank you for using the application !!!\n\n" exit() if __name__ == "__main__": # start the application from here: print "\n\t\t\t!!! Welcome To the NL2SPARQL Prototype !!!\n" # define the signal to exit the program. signal.signal(signal.SIGINT, signal_handler) while not done: # keep looping until you are done: nl_question = str(raw_input("\n\nask>>>> ")) target, query, metadata = seq2sql_prototype.get_query(nl_question) if query != None:
import ConfigParser import os import quepy from app.query import * import time from slackclient import SlackClient app = quepy.install("app") # Get the Slack credentials config = ConfigParser.ConfigParser() config.read('credentials.cfg') SLACK_BOT_TOKEN = config.get('default', 'SLACK_BOT_TOKEN') BOT_ID = config.get('default', 'BOT_ID') # constants AT_BOT = "<@" + BOT_ID + ">" # instantiate Slack & Twilio clients slack_client = SlackClient(SLACK_BOT_TOKEN) def handle_command(command, channel): """ Receives commands directed at the bot and determines if they are valid commands. If so, then acts on the commands. If not, returns back what it needs for clarification. """ #target, query, metadata = app.get_query("Where is the office of cmpe 273 instructor located?") #print "Query ", query
__author__ = 'coreyesj' # Set up quepy api from os import sys, path qtestPath = path.dirname(path.dirname( path.abspath(__file__))) + "/quepy_test/quepy_api" sys.path.append(qtestPath) from main import query import quepy from SPARQLWrapper import SPARQLWrapper, JSON sparql = SPARQLWrapper("http://dbpedia.org/sparql") dbpedia = quepy.install("app") def QA(question): # default_questions = [ # "Who killed Abraham Lincoln?" # ] # default_questions = [ # "What is a car?", # "Who is Tom Cruise?", # "Who is George Lucas?", # "Who is Mirtha Legrand?", # # "List Microsoft software", # "Name Fiat cars", # "time in argentina", # "what time is it in Chile?", # "List movies directed by Martin Scorsese", # "How long is Pulp Fiction",
def GET(self): query_string = web.input() freebase = quepy.install("freebase") return_model = freebase.get_query(query_string.q) return demjson.encode(return_model.toJSON())
""" import quepy import re import sys from SPARQLWrapper import SPARQLWrapper from spelling import correct from xml_converter import print_cause, print_define_for_error_nlg,print_file_location # regex for validate inputs and outputs regex_for_error_code = re.compile(r'\b ora\s?\W?\d{5}\b', re.I | re.M) regex_for_oracle_file = re.compile(r'\b .*\W?(ora|log)\b', re.I | re.M) # onto url sparql = SPARQLWrapper("http://localhost:3030/ds/query") nova = quepy.install("nova") root_type = "" query_type = "" file_name = "" error_no = "" target_keyword = "" question_type = "" def has_numbers(input_String): return any(char.isdigit() for char in input_String) if __name__ == "__main__": """
""" Main script for dbpedia quepy. """ import sys import time import random import datetime import quepy from SPARQLWrapper import SPARQLWrapper, JSON from ex.exception import NoResultsFoundException sparql = SPARQLWrapper("http://dbpedia.org/sparql") dbpedia = quepy.install("actions.dbpedia") class DBPedia: def __init__(self, tts): self.tts = tts def process(self, job): if job.get_is_processed(): return False try: self.query(job.raw()) job.is_processed = True except NoResultsFoundException: print "failed to get reponse from dbpedia"
def __init__(self, query_generator, output_processor): super(QPyDataFinder, self).__init__(query_generator, output_processor) self._sparql = SPARQLWrapper("http://dbpedia.org/sparql") self._dbpedia = quepy.install("dbpedia")
""" Main script for nihreporter quepy. """ import sys import logging import quepy import time import datetime import random import urllib import json from SPARQLWrapper import SPARQLWrapper, JSON sparql = SPARQLWrapper("http://localhost:2020/sparql") nihreporter = quepy.install("nihreporter") service_url = 'http://localhost:2020/sparql' #quepy.set_loglevel("DEBUG") target, query, metadata = nihreporter.get_query("What is lambert?") def print_define(results, target, metadata=None): for result in results["results"]["bindings"]: if result[target]["xml:lang"] == "en": print result[target]["value"] #print def print_enum(results, target, metadata=None):
def requeteExecution(request): sparql.setQuery(request) print(request) sparql.setReturnFormat(JSON) results = sparql.query().convert() result = print_define(results, "x0Label") return result if __name__ == "__main__": parser = argparse.ArgumentParser(description='Quepy prototype number 1') parser.add_argument("question", help='Question à poser sous la forme "question"') args = parser.parse_args() prototype1 = quepy.install("wikidata") target, query, metadata = prototype1.get_query(args.question) query, target = query_change(query, target) print("metadata: %s" % metadata) print query sparql = SPARQLWrapper("https://query.wikidata.org/bigdata/namespace/wdq/sparql") if target.startswith("?"): target = target[1:] if query: sparql.setQuery(query) sparql.setReturnFormat(JSON) #print(sparql.query().info()) results = sparql.query().convert()
# coding: utf-8 """ Main script for nihreporter quepy. """ import sys import logging import quepy import time import datetime import random import urllib import json from SPARQLWrapper import SPARQLWrapper, JSON sparql = SPARQLWrapper("http://localhost:2020/sparql") nihreporter = quepy.install("nihreporter") service_url = 'http://localhost:2020/sparql' #quepy.set_loglevel("DEBUG") target, query, metadata = nihreporter.get_query("What is lambert?") def print_define(results, target, metadata=None): for result in results["results"]["bindings"]: if result[target]["xml:lang"] == "en": print result[target]["value"] #print
""" Main script for lov quepy. """ import sys import argparse import quepy from SPARQLWrapper import SPARQLWrapper, JSON import lov.printHandlers as printHandlers # using directly the LOV endpoint to query sparql = SPARQLWrapper("http://lov.okfn.org/dataset/lov/sparql") lov = quepy.install("lov") def vocaburi(vocab_url): """ Give the uri of a vocabulary """ query = """ PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT * WHERE { <%s> foaf:primaryTopic ?uri . } """ % vocab_url sparql.setQuery(query) sparql.setReturnFormat(JSON) results = sparql.query().convert()
from flask import Flask, request, jsonify from flask_cors import CORS, cross_origin app = Flask(__name__) cors = CORS(app) app.config['CORS_HEADERS'] = 'Content-Type' import sys import os import time import random import datetime import quepy from SPARQLWrapper import SPARQLWrapper, JSON sparql = SPARQLWrapper("http://dbpedia.org/sparql") dbpedia = quepy.install("dbpedia") @app.route('/', methods=['POST']) @cross_origin() def hello(): data = request.json["question"] sparql, result, error = answer_question(data) response = {"query": sparql, "result": result, "error": error} return jsonify(response) def print_define(results, target, metadata=None): response = [] for result in results["results"]["bindings"]: if result[target]["xml:lang"] == "en":
class SPARQLQuery(object): _quepy_query = quepy.install("wikidata") _sparql = SPARQLWrapper( "https://query.wikidata.org/bigdata/namespace/wdq/sparql") @staticmethod def query_change(query, target): query = query.replace(target, "?x0", 1).replace("?x0 ToGenerate ?x1.", "", 1) target = "?x0" return query, target @classmethod def get_data_from_question(cls, question): target, query, metadata = cls._quepy_query.get_query(question) if not query: return None, "Il n'est pas possible de traiter la question suivante : '%s'." query, target = cls.query_change(query, target) return cls.execute_query(query, metadata) @classmethod def execute_query(cls, query, metadata=False): cls._sparql.setQuery(query) cls._sparql.setReturnFormat(JSON) results = cls._sparql.query().convert() data = cls.get_data(results) if not data: return None, "Aucun résultat n'a été trouvé pour la question suivante : '%s'." if not metadata: return data else: return data, metadata @staticmethod def create_query(uri_id, field): query = "SELECT DISTINCT ?x0Label WHERE{\n wd:" + uri_id + " " + field + " ?x0\n" + 'SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr". }' + "\n}\n" data = SPARQLQuery.execute_query(query) return data @staticmethod def get_data(results): data = [] head = results['head']['vars'] for result in results['results']['bindings']: if len(head) == 1: temp = result[head[0]]['value'] else: temp = {} for key in head: temp[key] = result[key]['value'] data.append(temp) if len(data) == 1: return data[0] return data
partial_rules_matches.py <input_filename> <output_filename> The input file must have a pickled list of dictionaries with keys 'questions'. Each question must be a list of quepy.Word that can be match with a quepy regex. The output file will have the same structure replacing each questions by a tuple where the first element is the original question and the second one a list of the partially matched rules. """ import pickle import refo from docopt import docopt from quepy import install freebase_app = install('quepyapp_freebase') _EOL = None def process_corpus(input_filename, output_filename): input_f = open(input_filename, 'r') original_corpus = pickle.load(input_f) input_f.close() for instance in original_corpus: words = instance['question'] rules = [] for regex in freebase_app.partial_rules: match = refo.match(regex + refo.Literal(_EOL), words + [_EOL]) if match: rules.append(repr(regex))
# coding: utf-8 """ Main script for testQuepy quepy. """ import quepy testquepy = quepy.install("dbpedia") target, query, metadata = testquepy.get_query("what is a blowtorch?") print query
# coding: utf-8 """ Main script for dbpedia quepy. """ import quepy dbpedia = quepy.install("dbpedia") target, query, metadata = dbpedia.get_query("what is a blowtorch?") print query
#!/usr/bin/python # coding: utf-8 """ Main script for ocgquerygen quepy. """ import sys import time import random import datetime import quepy import os import subprocess ocgquerygen = quepy.install("ocgquerygen") def get_query(query): target, query, metadata = ocgquerygen.get_query(query) if query: cmd = "d2rq/d2r-query -f json mapping.n3 ' " + query + "' > out.json" os.system(cmd) f = open("out.json", "r") result = f.read() f.close() return result # else: # return "Not Query Generated. :(" # return query