Awesome Open Source
Awesome Open Source

🤖 Neural SPARQL Machines

Python 3.7

A Machine-Translation Approach for Question Answering over Knowledge Graphs.

What does a NSpM do?


If you are looking for the code for papers "SPARQL as a Foreign Language" and "Neural Machine Translation for Query Construction and Composition" please checkout tag v0.1.0-akaha or branch v1.


Via pip

Coming soon!

Local setup

Clone the repository.

pip install -r requirements.txt

Example of usage

The Generator module

Pre-generated data

You can extract pre-generated data and model checkpoints from here in folders having the respective names.

Manual Generation (Alternative to using pre-generated data)

The template used in the paper can be found in a file such as Annotations_F30_art.csv. data/art_30 will be the ID of the working dataset used throughout the tutorial. To generate the training data, launch the following command.

mkdir -p data/art_30
python --templates data/templates/Annotations_F30_art.csv --output data/art_30

Launch the command if you want to build dataset seprately else it will internally be called while training.

python --input data/art_30 --output data/art_30

The Learner module

Now go back to the initial directory and launch to train the model. Currently the epochs and batch_size is not parametrized for that you can change the epoch is and batch size in (recommended batch size for large 64, medium 32 and small like art_30 is 16) also epochs varies with batch size for art 30 its 40.

python --input data/art_30 --output data/art_30

This command will create a model checkpoints in data/art_30.

The Interpreter module

Predict the SPARQL query for a given question it will store the detailed output in output_query.

python --input data/art_30 --output data/art_30 --inputstr "yuncken freeman has architected in how many cities?"

Use cases & integrations


SPARQL as a Foreign Language (2017)

    author = "Tommaso Soru and Edgard Marx and Diego Moussallem and Gustavo Publio and Andr\'e Valdestilhas and Diego Esteves and Ciro Baron Neto",
    title = "{SPARQL} as a Foreign Language",
    year = "2017",
    journal = "13th International Conference on Semantic Systems (SEMANTiCS 2017) - Posters and Demos",
    url = "",

Neural Machine Translation for Query Construction and Composition (2018)

    author = "Tommaso Soru and Edgard Marx and Andr\'e Valdestilhas and Diego Esteves and Diego Moussallem and Gustavo Publio",
    title = "Neural Machine Translation for Query Construction and Composition",
    year = "2018",
    journal = "ICML Workshop on Neural Abstract Machines \& Program Induction (NAMPI v2)",
    url = "",

Exploring Sequence-to-Sequence Models for SPARQL Pattern Composition (2020)

    author = "Anand Panchbhai and Tommaso Soru and Edgard Marx",
    title = "Exploring Sequence-to-Sequence Models for {SPARQL} Pattern Composition",
    year = "2020",
    journal = "First Indo-American Knowledge Graph and Semantic Web Conference",
    url = "",

Liber AI on Medium (2020)



Follow us

Liber AI logo

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
python (53,503
deep-learning (3,917
lstm (266
knowledge-graph (122
question-answering (105
seq2seq (104
rdf (84
machine-translation (65
neural-machine-translation (49
sparql (45
linked-data (41