Skip to content

Instantly share code, notes, and snippets.

@adzkar
Last active February 13, 2021 11:28
Show Gist options
  • Select an option

  • Save adzkar/bf4907f6dc6a033e4782da13c88aa8d9 to your computer and use it in GitHub Desktop.

Select an option

Save adzkar/bf4907f6dc6a033e4782da13c88aa8d9 to your computer and use it in GitHub Desktop.

Revisions

  1. adzkar revised this gist Feb 13, 2021. 1 changed file with 25 additions and 10 deletions.
    35 changes: 25 additions & 10 deletions sparql.py
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,40 @@
    from SPARQLWrapper import SPARQLWrapper, JSON, DIGEST, POST
    import json

    sparql = SPARQLWrapper("http://localhost:3030/phone/sparql")
    with open('contoh.json') as f:
    specs = json.load(f)

    sparql.setHTTPAuth(DIGEST)
    sparql.setCredentials("admin", "admin")
    sparql.setMethod(POST)

    sparql.setQuery("""
    x = ""

    for d in specs['aktivitas di lapangan']:
    x += f"{d}"

    print(x)
    query = """
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    PREFIX myowl: <http://www.risetgroup.org/ontologies/recommendersystem/smartphone#>
    SELECT *
    WHERE {
    ?s myowl:hasSpec myowl:GPS_High.
    ?s myowl:hasSpec myowl:Ukuran_Layar_High.
    ?s myowl:hasSpec myowl:Kecepatan_Jaringan_High.
    """ + x + """
    }
    """)
    """


    print(query)

    sparql = SPARQLWrapper("http://localhost:3030/phone/sparql")

    sparql.setHTTPAuth(DIGEST)
    sparql.setCredentials("admin", "admin")
    sparql.setMethod(POST)

    sparql.setQuery(query)

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

    print(results)
    for result in results["results"]["bindings"]:
    print(result["s"]["value"])
  2. adzkar created this gist Feb 13, 2021.
    25 changes: 25 additions & 0 deletions sparql.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    from SPARQLWrapper import SPARQLWrapper, JSON, DIGEST, POST

    sparql = SPARQLWrapper("http://localhost:3030/phone/sparql")

    sparql.setHTTPAuth(DIGEST)
    sparql.setCredentials("admin", "admin")
    sparql.setMethod(POST)

    sparql.setQuery("""
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    PREFIX myowl: <http://www.risetgroup.org/ontologies/recommendersystem/smartphone#>
    SELECT *
    WHERE {
    ?s myowl:hasSpec myowl:GPS_High.
    ?s myowl:hasSpec myowl:Ukuran_Layar_High.
    ?s myowl:hasSpec myowl:Kecepatan_Jaringan_High.
    }
    """)

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

    print(results)