Skip to content

๐Ÿ A Python wrapper to transform SPARQL queries into ARQ algebra S-expressions.

License

Notifications You must be signed in to change notification settings

LiberAI/arq-algebra

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

5 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ arq-algebra

Python 3.7

A Python wrapper to transform SPARQL queries into ARQ algebra S-expressions.

Requirements

  • Java SDK 1.8+

Supported queries

  • SELECT
  • ASK
  • CONSTRUCT (unstable)

Install

$ ./install.sh
$ pip install -r requirements.txt

Usage

From command line:

$ python arqalgebra.py 'select ?p (count(*) as ?c) where { ?s ?p ?o } group by ?p order by desc(?c)'

The expected output is the S-expression:

(project (?p ?c)
  (order ((desc ?c))
    (extend ((?c ?.0))
      (group (?p) ((?.0 (count)))
        (bgp (triple ?s ?p ?o))))))

Parse to string

From Python, one can also append oneline=True:

import arqalgebra as aa

s_exp = aa.parse('select ?p (count(*) as ?c) where { ?s ?p ?o } group by ?p order by desc(?c)', oneline=True)
# (project (?p ?c) (order ((desc ?c)) (extend ((?c ?.0)) (group (?p) ((?.0 (count))) (bgp (triple ?s ?p ?o))))))

Parse to tree

Return a SPARQL S-expression tree using the sexpdata library.

s_exp_tree = aa.parse_to_tree('select ?p (count(*) as ?c) where { ?s ?p ?o } group by ?p order by desc(?c)')
# [Symbol('project'), [Symbol('?p'), Symbol('?c')], [Symbol('order'), [[Symbol('desc'), Symbol('?c')]], [Symbol('extend'), [[Symbol('?c'), Symbol('?.0')]], [Symbol('group'), [Symbol('?p')], [[Symbol('?.0'), [Symbol('count')]]], [Symbol('bgp'), [Symbol('triple'), Symbol('?s'), Symbol('?p'), Symbol('?o')]]]]]]

Search term

Search for a term in the parsed tree and return the parent object.

aa.search(s_exp_tree, 'bgp')
# [Symbol('bgp'), [Symbol('triple'), Symbol('?s'), Symbol('?p'), Symbol('?o')]]

Tests

$ py.test

About

๐Ÿ A Python wrapper to transform SPARQL queries into ARQ algebra S-expressions.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published