Fork me on GitHub


Accessing FoodKG

FoodKG is a comprehensive food knowledge graph that consolidates information from diverse food-related data sources. It includes recipes, nutrition data, and ingredient information, all linked into a unified ontology with provenance maintained for each data point.

Endpoint Access

To interact with FoodKG, use the following SPARQL endpoint:

https://inciteprojects.idea.rpi.edu/foodkg

You must use SPARQL to extract the FoodKG triples.

Introduction to SPARQL

SPARQL (SPARQL Protocol and RDF Query Language) is specifically designed for retrieving and manipulating data stored in RDF (Resource Description Framework) format. It is similar to SQL but is used for querying graph-based data structures rather than relational databases. In SPARQL, data is represented as a graph of triples (subject-predicate-object).

Key Features of SPARQL:

  • Triple Pattern Matching: SPARQL queries are structured as patterns of triples, allowing users to retrieve specific data relationships.
  • Filtering and Constraints: Supports filtering results, joining multiple patterns, and applying constraints on values.
  • Aggregations and Grouping: Similar to SQL, SPARQL supports aggregations (e.g., COUNT, AVG) and grouping data.

Getting Started with SPARQL:

To help you get started, here are some beginner-friendly and advanced tutorials that cover the basics and advanced features of SPARQL:

  1. W3C SPARQL 1.1 Query Language - Official Specification
    The W3C provides an official and comprehensive specification for SPARQL. This is a great resource to refer to for the formal definitions and capabilities of the language.
  2. SPARQL By Example - Cambridge Semantics - Practical Examples
    This tutorial uses practical examples to teach SPARQL, covering topics like querying for specific data, handling optional data, and performing aggregations.
  3. Learning SPARQL - Book by Bob DuCharme - Comprehensive Guide
    This book is an in-depth guide to SPARQL. It provides real-world examples and is a great resource for both beginners and advanced users looking to deepen their SPARQL skills.

Example SPARQL Queries to run on FoodKG Endpoint:

Here is the code to find five recipes with a particular label, i.e., "Vegetable Soup".
        
            PREFIX recipe-kg: 
            SELECT ?recipe
            WHERE {
                ?recipe rdfs:label "Vegetable Soup" .
            }
            LIMIT 5
        
    
To count the number of ingredients for each recipe, you can use the COUNT function along with GROUP BY to group the results by each recipe. Here’s how to modify the query:
        
        PREFIX recipe-kg: 
        SELECT ?recipe (COUNT(?ingredient) AS ?ingredientCount)
        WHERE {
            ?recipe rdfs:label "Vegetable Soup";
                    recipe-kg:uses ?ingredient.
        }
        GROUP BY ?recipe
        
    

Python Code Snippets for Accessing the FoodKG

Below are some useful Python code snippets for querying FoodKG using SPARQL.

Prerequisite

To interact with FoodKG using Python, you need to install the SPARQLWrapper library. You can install it by running:

        pip install SPARQLWrapper
    

1. Setting up the SPARQL Endpoint

        
        from SPARQLWrapper import SPARQLWrapper, JSON
        from pprint import pprint

        # Initialize the FoodKG endpoint
        sparql = SPARQLWrapper("https://inciteprojects.idea.rpi.edu/foodkg/namespace/kb")
        
    

2. Run the Query

        
        def fetch_recipes():
            query = '''
            PREFIX recipe-kg: 
            SELECT ?recipe
            WHERE {
                ?recipe rdfs:label "Vegetable Soup" .
            }
            LIMIT 5'''

            sparql.setQuery(query)
            sparql.setReturnFormat(JSON)
            results = sparql.query().convert()
            recipe = [x['recipe']['value'] for x in results['results']['bindings']]
            return recipes

        # Example usage
        recipes = fetch_recipes()
        pprint(recipes)