简体   繁体   中英

How to get token position in sparql query in rdflib

How to obtain a given token type (uri, literal, ...) and location from a given sparql query using rdflib ?

For instance, given the following query:

SELECT count(DISTINCT ?uri) as ?count
FROM graph WHERE {
   ?uri a ?type;
        graph:predicate_a ?c;
   FILTER(?c = xyz AND ?type != abc)
}

I'd like to implement a function get_token_position such that get_token_position('AND') would return 124 (location) and something like OperatorType .

Reading the documentation, I noticed the library implements utilities to build parsing trees for the queries, therefore I think that come out with this function would be easily done using that library.

Ps.: My motivation is that I need to convert "AND" and "OR" to "&&" and "||" whenever those strings are used as operations. Also, I think using the parsing tree would be a better solution than using a regex pattern.

Can you further explain what you want to do? It seems like you are wanting to implement an extremely complex set of functions for a string replace??

s = """
SELECT count(DISTINCT ?uri) as ?count
FROM graph WHERE {
   ?uri a ?type;
        graph:predicate_a ?c;
   FILTER(?c = xyz AND ?type != abc)
}
"""

s = s.replace(" AND ", " && ")  # spaces around AND to avoid in-word matches

If you want to ensure that the replacement doesn't affect any values that might appear in the query through variable substitution, you could simple do this AND/&& etc replacement first and then the variable substitution second.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM