Search code examples
sparqlrdfdbpediavirtuoso

Best way to filter DBpedia results and return a specific results using SPARQL


I have a little problem...

From a list on wikipedia Page Ids, i want to return:

  • pageid
  • Abstract ( English)
  • Thumbnail
  • type

Only for company OR university page

I have this simple SPARQL query:

PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
select ?pageid ?abstract ?thumbnail  ?company ?type 
where {
    ?resource dbpedia-owl:wikiPageID ?pageid;
    dbpedia-owl:abstract ?abstract 
. OPTIONAL { 
    ?resource dbpedia-owl:thumbnail ?thumbnail
     } 
 . OPTIONAL { 
    ?resource  dbpedia-owl:type ?type
    } 
. OPTIONAL { 
    ?resource ?company dbpedia-owl:Company 
    }

FILTER( 
?pageid = 14617 || ?pageid = 26989 || ?pageid = 31776 || 
?pageid = 256913 || ?pageid = 342924 || ?pageid = 1785141 || 
?pageid = 3057187 || ?pageid = 7529378 || ?pageid = 18978754 
)
FILTER(langMatches(lang(?abstract),"en"))
}

I have this result: SPARQL Result

I managed to return the data that I wanted except that I cannot filter only that companies and universities.

is there any idea on how to remove pages that are not companies or universities in the dbpedia query results?


Solution

  • @AKSW has provided a query that does what you want, but I think it would be better with some more detailed explanation of why. So, here is that query, with a bunch of whitespace and some inline comments to help clarify --

    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
    PREFIX dbo: <http://dbpedia.org/ontology/> 
    
    SELECT ?pageid ?abstract ?thumbnail ?type 
    WHERE
      { 
    
      # the next couple lines limit rdf:type values
        VALUES ?type
               { dbo:Company dbo:University } 
    
      # the next few lines limit dbo:wikiPageID values
        VALUES ?pageid 
               {   14617     26989      31776
                  256913    342924    1785141
                 3057187   7529378   18978754 } 
    
      # the next few lines get values for the 3 predicates 
      # you required for each subject
                   ?resource  dbo:wikiPageID  ?pageid ; 
                              rdf:type        ?type ; 
                              dbo:abstract    ?abstract . 
    
      # the next line gets thumbnails, if they exist
        OPTIONAL { ?resource  dbo:thumbnail   ?thumbnail } 
    
      # the next line limits the solutions you receive to those 
      # with an abstract langtagged as "en"
        FILTER ( langMatches ( lang ( ?abstract ), "en" ) ) 
    
      }