def init(endpoint="http://dbpedia.org/sparql"): LogManager.LogInfo(f"Initializing SPARQLEndpointManager...") try: SPARQLEndpointManager.endpoint = endpoint SPARQLEndpointManager.sparql = SPARQLWrapper(endpoint) except Exception as e: LogManager.LogError(f"Failed to initialize SPARQLEndpointManager") LogManager.LogError(e)
def SendQuery(query, returnFormat=JSON): LogManager.LogInfo( f"Sending query to endpoint {SPARQLEndpointManager.endpoint}") try: SPARQLEndpointManager.sparql.setQuery(query) SPARQLEndpointManager.sparql.setReturnFormat(returnFormat) results = SPARQLEndpointManager.sparql.query().convert() return results except Exception as e: LogManager.LogError( f"Unable to Send query to {SPARQLEndpointManager.endpoint}") LogManager.LogError(e) return None
def GetQueryPrefixes(self, query): LogManager.LogInfo(f"Extracting prefixes from query...") prefixes = [] inlineQuery = query.replace('\n', ' ').replace('\t', '') queryRegex = r'(.*)[Ss][Ee][Ll][Ee][Cc][Tt](.*)' matchPrefixes = re.match(queryRegex, inlineQuery) if not matchPrefixes: LogManager.LogInfo(f"No prefixes found") return None allPrefixes = matchPrefixes.group(1) nPrefixes = [i.lstrip(' ') for i in allPrefixes.split('>') if i != ' '] nPrefixes = [i + '>' for i in nPrefixes if i != ''] for prefix in nPrefixes: prefxRegex = '[Pp][Rr][Ee][Ff][Ii][Xx](.*?):' matchPrefix = re.match(prefxRegex, prefix) if not matchPrefix: LogManager.LogError("Invalid Prefixes") return None prefixes.append((matchPrefix.group(1).lstrip(' '), prefix)) LogManager.LogInfo( f"Prefixes extracted successfully! Prefixes: {prefixes}") return prefixes
def SendQueryForLabel(variable, prefixes='', returnFormat=JSON): LogManager.LogInfo( f"Sending query to endpoint {SPARQLEndpointManager.endpoint} for getting label of {variable}" ) stringPrefixes = [pref[1] for pref in prefixes] if prefixes != '' else [''] query = ' '.join( stringPrefixes ) + ' SELECT ?label WHERE { ' + variable + ' <http://www.w3.org/2000/01/rdf-schema#label> ' + '?label . }' try: SPARQLEndpointManager.sparql.setQuery(query) SPARQLEndpointManager.sparql.setReturnFormat(returnFormat) results = SPARQLEndpointManager.sparql.query().convert() return results["results"]["bindings"][0]["label"]["value"] if len( results["results"]["bindings"]) > 0 else '' except Exception as e: LogManager.LogError( f"Unable to Send query to {SPARQLEndpointManager.endpoint}") LogManager.LogError(e) return None
def __init__(self, query): LogManager.LogInfo(f"Starting Verbalize Manager...") self.answer = None self.parser = SPARQLParserManager(query) if self.parser.queryIsValid: self.answer = self.Verbalize(self.parser.queryVariable[0], self.parser.queryTriples, self.parser.queryAnswer, self.parser.queryPrefixes) else: LogManager.LogError(f"Invalid query syntax for query:\n{query}") self.answer = self.parser.queryError
def init(queriesNum=10): LogManager.LogInfo( f"Initializing XMLManager and getting latest version and queries..." ) try: VersionHistory = ET.parse('VersionHistory.xml') Versions = [ Version for Version in VersionHistory.findall('Version') ] XMLManager.latestVersion = Versions[-1].attrib['ID'] except IOError: LogManager.LogError(f"Unable to get latest version from file") try: XMLManager.sampleQueriesNum = queriesNum SolideQueries = ET.parse('queries.xml') XMLManager.queries = [ (question[0].text, question[1].text) for question in SolideQueries.findall('question') ] XMLManager.labels = [label[0] for label in XMLManager.queries] except IOError: LogManager.LogError(f"Unable to get queries from file")
def SendQueryForType(variable, labelTriple, prefixes='', returnFormat=JSON): LogManager.LogInfo( f"Sending query to endpoint {SPARQLEndpointManager.endpoint} for getting type of {variable}" ) stringLabelTriple = f'{labelTriple[0][0]} {labelTriple[0][1]} "{labelTriple[0][2]}"' stringPrefixes = [pref[1] for pref in prefixes] if prefixes != '' else [''] query = ' '.join( stringPrefixes ) + ' SELECT DISTINCT ?type WHERE { ' + variable + ' <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ' + '?type . ' + stringLabelTriple + ' } LIMIT 1' try: SPARQLEndpointManager.sparql.setQuery(query) SPARQLEndpointManager.sparql.setReturnFormat(returnFormat) results = SPARQLEndpointManager.sparql.query().convert() return results["results"]["bindings"][0]["type"]["value"] if len( results["results"]["bindings"]) > 0 else '' except Exception as e: LogManager.LogError( f"Unable to Send query to {SPARQLEndpointManager.endpoint}") LogManager.LogError(e) return None