DbPedia is a structured data version of Wikipedia. It contains a live RESTful api which allows to issue SPARQL queries and receive results in a variety of formats. This python code gets three specific questions in natural language and returns their answers.
The code is consists of three steps:
-
Connect to the API, send a request and get a response: There are two approaches by using two python libraries in this code:
- Requests library:
dbpedia_api_by_requests() - SPARQLWrapper library:
dbpedia_api_by_sparqlwrapper()
- Requests library:
-
Translate natural language question to Sparql query:
- Based on this challenge description we don't need to do any clever parsing, So I have only translated the questions based on two factors: name of the person and the property we are looking for.
make_query() - Generally for this part we can use new AI models to create Sparql queries from natural language question. [HuggingFace Hub](Hugging Face – The AI community building the future.) is a platform to find a proper pretrained AI model to do this. Transformers library provides lots of pretrained ML (deep learning) models for unstructured data such as text and vision. These models can be fine tuned with a dataset for a specific reason in [Amazon Sagemaker](Machine Learning Service, Machine Learning Ide - Amazon SageMaker - AWS) as a serverless environment.
- Based on this challenge description we don't need to do any clever parsing, So I have only translated the questions based on two factors: name of the person and the property we are looking for.
-
Extract output from the API response:
- We get the response from API in
JSONformat and need to find the exact value of the property that asked in the input question. Finally the answer returns as the output.extract_output()
- We get the response from API in
Two python libraries are used for unit testing:
- pytest:
test_do_sparql_query.py - unittest:
test_do_sparql_qyery_unittest.py
Simply run the following command:
make
The output shows the possible actions to install requirements, run the code and test. Makefile Menu:
make install: Install requirementsmake run: Run the scriptmake pytest: Run pytest on all testsmake unittest: Run unittest
Notice: Install the requirements by make install before run or test.