Justin Abrahms
Justin Abrahms

Reputation: 1421

Expansive path expressions in quoted triples?

I'm trying to query something like wikipedia where objects connect to one another and the edges have the paragraph which links them. I'm hoping to wrap my head around the query portions before I figure out the semantic values for the links and entities.

Data set looks like this currently (may change eventually):

PREFIX : <http://justin.abrah.ms/knowledge_base#>

:SteamCooked :title "Steaming" .

:KoreanFood 
  :title "Korean Food" ;
  :linksTo :SteamCooked
  .

:AlternativeMedicine :title "Alternative Medicine" .

:Cheong
  :title "Cheong (food)";
  :linksTo :KoreanFood;
  :linksTo :AlternativeMedicine .


<< :Cheong :linksTo :KoreanFood >> :inParagraph "Cheong (청; 淸) is a name for various sweetened foods in the form of syrups, marmalades, and fruit preserves. In Korean cuisine, cheong is used as a tea base, as a honey-or-sugar-substitute in cooking, as a condiment, and also as an alternative medicine to treat the common cold and other minor illnesses." .

<< :Cheong :linksTo :AlternativeMedicine >> :inParagraph "Cheong (청; 淸) is a name for various sweetened foods in the form of syrups, marmalades, and fruit preserves. In Korean cuisine, cheong is used as a tea base, as a honey-or-sugar-substitute in cooking, as a condiment, and also as an alternative medicine to treat the common cold and other minor illnesses." .

I can query for the connections with this sparql query:

PREFIX : <http://justin.abrah.ms/knowledge_base#>

SELECT ?midNode WHERE {
    ?am :title "Alternative Medicine" .
    ?steam :title "Steaming" .
    ?am (:linksTo|^:linksTo)* ?midNode .
  ?midNode (:linksTo|^:linksTo)* ?steam .
}

What I can't seem to figure out is how one would query for those nodes alongside the edges. The big hiccup I've found is that path expansion like (:linksTo|^:linksTo)* doesn't work inside quoted triples. Is this a limitation of sparql? Or am I just missing a key concept?

Upvotes: 3

Views: 44

Answers (1)

Justin Abrahms
Justin Abrahms

Reputation: 1421

Credit to uninformeduser above, I ended up with this query which will correctly give me the pathing information with the associated paragraphs.

PREFIX : <http://justin.abrah.ms/knowledge_base#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT ?s ?o ?paragraph WHERE {
    ?am :title "Alternative Medicine" .
    ?kf :title "Korean Food" .
    
    ?term :inParagraph ?paragraph .
    filter(rdf:isTriple(?term)) .
    bind(rdf:subject(?term) as ?s) .
    bind(rdf:object(?term) as ?o) .
    filter(rdf:subject(?term) = ?midNode)
    
    ?am (:linksTo|^:linksTo)* ?midNode .
    ?midNode (:linksTo|^:linksTo)* ?kf .
    
    filter(?midNode = ?s) .
}

In my own understanding, this query says:

Decompose an :inParagraph subject, extracing the subject and object from it's left side.

Find a way to link term A (?am) with term B (?kf) through a yet-to-be-known ?midNode.

Ensure ?midnode is contained in our :inParagraph entries.

Upvotes: 4

Related Questions