Recommand · July 30, 2021 0

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.