Using SPARQL to query DBPedia Company Information

假如想象 提交于 2019-12-06 11:49:01

The reason for rdf:type <http://dbpedia.org/class/yago/Company108058098> in a query like the following is because (presumably), that's a class whose instances are companies. Asking for instances of the class is a way of asking for companies.

select * { ?s rdf:type <http://dbpedia.org/class/yago/Company108058098> }
limit 10

SPARQL results

It's the same principle that lets us select Persons with:

select * { ?s a dbpedia-owl:Person }
limit 10

SPARQL results

As to your specific query, a typically good way to query DBpedia data is to start by looking at the data manually and finding the types of values you're interested in. For instance, you might look at Apple, Inc., whose DBpedia resource is

For the kinds of information that you're looking for, important properties seem to be:

You can simply use the resource IRI as the unique identifier. Given all this, you can write a query like the following. It has multiple results, though, because there are multiple possible logos, but so it goes.

select ?iri ?logo ?description {
  ?iri a dbpedia-owl:Company ;
       dbpedia-owl:abstract ?description ;
       rdfs:label "Apple Inc."@en ;
       foaf:depiction|dbpedia-owl:thumbnail ?logo .
  filter( langMatches(lang(?description),"en") )
}

SPARQL results

It would be nice to be able to use

foaf:name|rdfs:label "Apple In."@en

as well, but the endpoint says in that case that the estimated time is too great:

Virtuoso 42000 Error The estimated execution time 9320 (sec) exceeds the limit of 3000 (sec).

I'm not sure how it estimates the time, but you can use some optionals and some values to work around it (but be sure to put distinct into the select):

select distinct ?iri ?logo ?description {
  values ?hasLogo { foaf:depiction dbpedia-owl:thumbnail }
  values ?hasName { foaf:name rdfs:label }
  ?iri a dbpedia-owl:Company ;
       dbpedia-owl:abstract ?description ;
       ?hasLName "Apple Inc."@en ;
       ?hasLogo ?logo .
  filter( langMatches(lang(?description),"en") )
}

Note: At time of writing, DBpedia's endpoint is very sluggish and under maintenance, so I'm not sure yet whether this last permutation actually hits the estimated time cutoff or not. I think that it will go through, though.

To get all companies one have to use LIMIT and OFFSET because usually public endpoints limits number of results per query. Based on @Joshua answer I wrote a small script that can be run to get all companies from public dbpedia endpoint. Here is the gist: https://gist.github.com/szydan/e801fa687587d9eb0f6a

One can also modify the query and use it to get other entities.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!