Search code examples
javasparqlsemantic-webdbpedia

Semantic relations between set of words


If I have a set of words (DBpedia resources) storing in arraylist how can I build a SPARQL query to find all possible direct on indirect relation between these terms? the major problem is I don't know what's the relation type that I'm searching for.

suppose that my arraylist contains 3 words, France,Paris, Europe How can I write a query that returns the direct relation (or indirect relation of 2 hops) between France-Paris ,Paris-Europe and France-Europe

hope that I clear up what I'm looking for


Solution

  • What you can do is to use VALUES to set a variable to your set of resources, but do it twice, once for each end of the relation. Then use a variable as a predicate to find what the relation is. Something like:

    SELECT ?resource1 ?p1 ?intermediary ?p2 ?resource2
    WHERE
    {
      VALUES ?resource1 { :Paris :France :Europe }
      VALUES ?resource2 { :Paris :France :Europe }
      FILTER(?resource1 != ?resource2)
    
      {
        ?resource1 ?p1 ?resource2
      }
      UNION
      {
        ?resource1 ?p1 ?intermediary.
        ?intermediary ?p2 ?resource2.
      }
    }
    

    The results are:

    screenshot of the results table