<jupyter_start><jupyter_text>Foodle Maps This notebook gives practical examples of the potentiality of Foodle Maps.First, load the necessary packages to perform SPARQL queries:<jupyter_code>from SPARQLWrapper import SPARQLWrapper, JSON, DIGEST, POST

sparql = SPARQLWrapper("https://sparql.alexmv12.xyz/FoodleMaps")<jupyter_output><empty_output><jupyter_text>Then, we can start with a simple query: show all the restaurants in the KB.<jupyter_code>sparql.setQuery("""
    PREFIX schema: <http://schema.org/>
    
    SELECT ?restaurant 
    WHERE { ?restaurant a schema:FoodEstablishment  }
""")

sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
    print(result["restaurant"]["value"])<jupyter_output>https://alexmv12.github.io/FoodleMaps/#La_Cantinetta_caprese
https://alexmv12.github.io/FoodleMaps/#Tavernacolo
https://alexmv12.github.io/FoodleMaps/#Trattoria_Sole_Milano
https://alexmv12.github.io/FoodleMaps/#Trattoria_dell_orsa<jupyter_text>... or, we want to see some of the available dishes.<jupyter_code>sparql.setQuery("""
    PREFIX schema: <http://schema.org/>
    
    SELECT ?dish 
    WHERE { ?dish a schema:MenuItem  }
    LIMIT 5
""")

sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
    print(result["dish"]["value"])<jupyter_output>https://alexmv12.github.io/FoodleMaps/#Pasta_alla_carbonara
https://alexmv12.github.io/FoodleMaps/#Baba_al_rum
https://alexmv12.github.io/FoodleMaps/#Bruschettone_gorgonzola_noci_e_salame
https://alexmv12.github.io/FoodleMaps/#Caprese_di_bufala
https://alexmv12.github.io/FoodleMaps/#Cotoletta_alla_bolognese<jupyter_text>Examples - Show me all the restaurants serving dishes with tomatoes.- Find the nearest restaurant which serves at least a “light” dish.- Show me a restaurant which does not serve any dish with eggs.- Show me a restaurant which serves a dish which can be prepared in less than 5 minutes.  Dishes with tomatoesLet's find all restaurants which serve dishes which contain fresh tomatoes.<jupyter_code>sparql.setQuery("""
    PREFIX schema: <http://schema.org/>
    PREFIX foodlemaps: <https://alexmv12.github.io/FoodleMaps/#>
    
    SELECT ?restaurant
    WHERE { ?restaurant schema:hasMenu ?menu.
            ?menu foodlemaps:servesRecipe ?recipe.
            ?recipe foodlemaps:hasIngredient foodlemaps:Tomato .}
""")

sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
    print(result["restaurant"]["value"])<jupyter_output>https://alexmv12.github.io/FoodleMaps/#La_Cantinetta_caprese
https://alexmv12.github.io/FoodleMaps/#Tavernacolo
https://alexmv12.github.io/FoodleMaps/#Trattoria_Sole_Milano<jupyter_text>Fresh dishes for summer Or, maybe we want a "fresh" dish for this hot summer. What are some dishes with that property?<jupyter_code>sparql.setQuery("""
    PREFIX schema: <http://schema.org/>
    PREFIX foodlemaps: <https://alexmv12.github.io/FoodleMaps/#>
    
    SELECT ?dish
    WHERE { ?dish foodlemaps:hasCharacteristic foodlemaps:Fresh}
""")

sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
    print(result["dish"]["value"])<jupyter_output>https://alexmv12.github.io/FoodleMaps/#Insalata_greca
https://alexmv12.github.io/FoodleMaps/#Vitello_tonnato<jupyter_text>Great! Which restaurants serve "Insalata greca"?<jupyter_code>sparql.setQuery("""
    PREFIX schema: <http://schema.org/>
    PREFIX foodlemaps: <https://alexmv12.github.io/FoodleMaps/#>
    
    SELECT ?restaurant
    WHERE { ?restaurant schema:hasMenu ?menu.
            ?menu foodlemaps:servesRecipe ?recipe.
            foodlemaps:Insalata_greca foodlemaps:hasRecipe ?recipe .}
""")

sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
    print(result["restaurant"]["value"])<jupyter_output>https://alexmv12.github.io/FoodleMaps/#Tavernacolo<jupyter_text>We'd like the address and telephone number of "Tavernacolo", so we could book a table.<jupyter_code>sparql.setQuery("""
    PREFIX schema: <http://schema.org/>
    PREFIX foodlemaps: <https://alexmv12.github.io/FoodleMaps/#>
    
    SELECT ?address ?telephoneNumber
    WHERE { foodlemaps:Tavernacolo schema:address ?address ;
        			               schema:telephone ?telephoneNumber .
}
""")

sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
    print(result["address"]["value"])
    print(result["telephoneNumber"]["value"])<jupyter_output>Via Bassini, 45, Milano, Italy
02 1234 4321<jupyter_text>Eggs-free restaurantsWe want to find restaurants which do not serve eggs at all. Maybe we are allergic, so we prefere to not risk any contamination.<jupyter_code>sparql.setQuery("""
    PREFIX schema: <http://schema.org/>
    PREFIX foodlemaps: <https://alexmv12.github.io/FoodleMaps/#>
    
    SELECT DISTINCT ?restaurant
    WHERE { 
            ?restaurant a schema:FoodEstablishment
            
            FILTER NOT EXISTS { 
                ?restaurant schema:hasMenu ?menu.
                ?menu foodlemaps:servesRecipe ?recipe.
                ?recipe foodlemaps:hasIngredient foodlemaps:Egg 
            }
    }
""")

sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
    print(result["restaurant"]["value"])<jupyter_output>https://alexmv12.github.io/FoodleMaps/#Tavernacolo<jupyter_text>As a counter-proof, we can list all the restaurants which serve dishes with eggs:<jupyter_code>sparql.setQuery("""
    PREFIX schema: <http://schema.org/>
    PREFIX foodlemaps: <https://alexmv12.github.io/FoodleMaps/#>
    
    SELECT ?restaurant ?menuItem
    WHERE { ?restaurant schema:hasMenu ?menu.
            ?menu foodlemaps:servesRecipe ?recipe.
            ?recipe foodlemaps:hasIngredient foodlemaps:Egg .
            ?menuItem foodlemaps:hasRecipe ?recipe .
    }

""")

sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
    print(result["restaurant"]["value"] + " -> " + result["menuItem"]["value"])<jupyter_output>https://alexmv12.github.io/FoodleMaps/#La_Cantinetta_caprese -> https://alexmv12.github.io/FoodleMaps/#Baba_al_rum
https://alexmv12.github.io/FoodleMaps/#Trattoria_dell_orsa -> https://alexmv12.github.io/FoodleMaps/#Cotoletta_alla_bolognese
https://alexmv12.github.io/FoodleMaps/#Trattoria_dell_orsa -> https://alexmv12.github.io/FoodleMaps/#Tortellini_in_brodo
https://alexmv12.github.io/FoodleMaps/#Trattoria_Sole_Milano -> https://alexmv12.github.io/FoodleMaps/#Pasta_alla_carbonara
https://alexmv12.github.io/FoodleMaps/#Trattoria_Sole_Milano -> https://alexmv12.github.io/FoodleMaps/#Tiramisu
https://alexmv12.github.io/FoodleMaps/#La_Cantinetta_caprese -> https://alexmv12.github.io/FoodleMaps/#Vitello_tonnato<jupyter_text>Fast restaurantsWe are in a hurry! We need a restaurant which can serve us a very fast dish. Let's see some of the fastest restaurants in our ontology.<jupyter_code>sparql.setQuery("""
    PREFIX schema: <http://schema.org/>
    PREFIX foodlemaps: <https://alexmv12.github.io/FoodleMaps/#>
    
    SELECT ?restaurant ?menuItem ?preparationTime
    WHERE { ?restaurant schema:hasMenu ?menu.
            ?menu foodlemaps:servesRecipe ?recipe.
            ?recipe foodlemaps:hasIngredient foodlemaps:Egg .
            ?menuItem foodlemaps:hasRecipe ?recipe .
            ?menuItem foodlemaps:hasPreparationTime ?preparationTime .
            FILTER (?preparationTime < 10)
    }

""")

sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
    print(result["restaurant"]["value"] + " can serve " + result["menuItem"]["value"] + " in " + result["preparationTime"]["value"] + " minutes.")<jupyter_output>https://alexmv12.github.io/FoodleMaps/#La_Cantinetta_caprese can serve https://alexmv12.github.io/FoodleMaps/#Baba_al_rum in 3 minutes.
https://alexmv12.github.io/FoodleMaps/#La_Cantinetta_caprese can serve https://alexmv12.github.io/FoodleMaps/#Vitello_tonnato in 6 minutes.<jupyter_text>Extra: user preferencesThe ontology also includes a set of classes which can be used to represent the preferences of a user.  For example, we may create a user which follows a vegetarian diet:<jupyter_code>sparql.setHTTPAuth(DIGEST)
sparql.setCredentials("admin", "insert_sparql_endpoint_password_here")
sparql.setMethod(POST)

sparql.setQuery("""
    PREFIX schema: <http://schema.org/>
    PREFIX foodlemaps: <https://alexmv12.github.io/FoodleMaps/#>
    
    INSERT DATA
    { 
      foodlemaps:user0                       a foodlemaps:User ;
                       foodlemaps:requiresDiet schema:VegetarianDiet .
    }
""")

results = sparql.query()
print(results.response.read())<jupyter_output>b'<html>\n<head>\n</head>\n<body>\n<h1>Success</h1>\n<p>\nUpdate succeeded\n</p>\n</body>\n</html>\n'<jupyter_text>What are some of the dishes we can eat?<jupyter_code>sparql.setQuery("""
    PREFIX schema: <http://schema.org/>
    PREFIX foodlemaps: <https://alexmv12.github.io/FoodleMaps/#>
    
    SELECT ?restaurant ?menuItem
    WHERE { 
            foodlemaps:user0 foodlemaps:requiresDiet ?diet .
            
            ?restaurant schema:hasMenu ?menu .
            ?menu foodlemaps:servesRecipe ?recipe.
            ?menuItem foodlemaps:hasRecipe ?recipe .
            ?recipe schema:suitableForDiet ?diet .            
    }

""")

sparql.setReturnFormat(JSON)
results = sparql.query().convert()

for result in results["results"]["bindings"]:
    print(result["restaurant"]["value"] + " -> " + result["menuItem"]["value"] )<jupyter_output>https://alexmv12.github.io/FoodleMaps/#La_Cantinetta_caprese -> https://alexmv12.github.io/FoodleMaps/#Baba_al_rum
https://alexmv12.github.io/FoodleMaps/#Tavernacolo -> https://alexmv12.github.io/FoodleMaps/#Insalata_greca
{'fraction_non_alphanumeric': 0.1312706518474016, 'fraction_numerical': 0.011414839291078402, 'mean_word_length': 4.36989247311828, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 78, 'https://': 46, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '622839', 'license': 'Apache-2.0', 'n_tokens_mistral': 3633, 'n_tokens_neox': 3392, 'n_words': 598, 'path': 'FoodleMaps.ipynb', 'repo_name': 'AlexMV12/FoodleMaps', 'size': '9987'}