Wednesday, September 18, 2013

SKOS Ontology with sample SPARQL

SKOS Simple Knowledge Organization System 

Select the first 3 concepts that have a skos:altLabel
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?concept  ?label
WHERE { ?concept skos:altLabel ?label . } LIMIT 3
Select the first 3 concepts that have a skos:altLabel, as RDF
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
CONSTRUCT   { ?concept skos:altLabel ?label  }
WHERE { ?concept skos:altLabel ?label . } LIMIT 3
Construct an RDF graph of terms with narrower terms but no broader terms, including a narrower terms plus the LCC number. Negation is in a state of flux (see http://www.w3.org/2009/sparql/wiki/Feature:Negation).
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX terms: <http://purl.org/dc/terms/>
CONSTRUCT   { 
    ?concept skos:narrower ?child .
    ?concept terms:LCC ?lcc.
}  WHERE { 
    ?concept skos:narrower ?child . 
    ?concept terms:LCC ?lcc.
    OPTIONAL { ?concept skos:broader ?parent }
    FILTER (!bound(?parent))
} LIMIT 3
Construct an RDF graph of terms with no broader terms in science:
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX terms: <http://purl.org/dc/terms/>
CONSTRUCT   { 
    ?concept terms:LCC ?lcc.
}  WHERE {  
    ?concept terms:LCC ?lcc.
    ?concept terms:created ?date
    OPTIONAL { ?concept skos:broader ?broader }
    FILTER (!bound(?broader))
    FILTER regex(?lcc, "^QA")
} LIMIT 50
Get all of the information about a concept. Check both ends of the relation, in case there are no inverseOf-def defined. In this case narrower is matched by broader, but that’s not the case for all RDF vocabularies. The many PREFIX clauses are here so that we get sane/readable prefixes in the output, but they’re unnecessary when generating RDF for machine consumption.
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX terms: <http://purl.org/dc/terms/>
CONSTRUCT   { 
    ?first ?relationOne <http://lcsubjects.org/subjects/sh2002000569#concept> .
    <http://lcsubjects.org/subjects/sh2002000569#concept> ?relationTwo ?second .
}  WHERE {  
    ?first ?relationOne <http://lcsubjects.org/subjects/sh2002000569#concept> .
   <http://lcsubjects.org/subjects/sh2002000569#concept> ?relationTwo ?second .   
} 
This example shows how a SPARQL endpoint might be used to get the data for AJAXy autocomplete functionality. In this example, the letters “Comp” have been typed. The str(…) is required to strip the language tags and the “i” indicates that case-insensitive matching should be done.
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT 
    ?label 
WHERE {  
   {  ?concept  skos:altLabel ?label . } UNION {  ?concept  skos:prefLabel ?label . }
   FILTER regex(str(?label), "^Comp", "i") .
} LIMIT 10

0 comments:

Post a Comment