关于统计DBpedia资源总量及SPARQL查询有效性的技术咨询
Hey there! Great question—totally get the confusion when you're starting out with SPARQL and DBpedia. Let's break this down clearly:
Your initial query doesn't equal total DBpedia resources
The query you shared:
SELECT (count(distinct ?sub ) as ?amount) WHERE { ?sub ?predic ?obj . }
counts all unique URIs that appear as a subject in DBpedia triples. That includes way more than just the http://dbpedia.org/resource/ entities you’re targeting. For example, it will count:
- Properties from the
http://dbpedia.org/property/namespace - Ontology classes from
http://dbpedia.org/ontology/ - External URIs from linked datasets
- Even internal system-related URIs
So this number is significantly larger than the actual total of DBpedia’s core resources.
The correct way to count DBpedia resources
To get only the resources under the resource/ namespace, use the STRSTARTS function to filter subjects by their URI prefix. Here’s a precise query:
SELECT (COUNT(DISTINCT ?sub) AS ?totalDBpediaResources) WHERE { ?sub ?pred ?obj . # Filter to only include resources in the DBpedia resource namespace FILTER (STRSTARTS(STR(?sub), "http://dbpedia.org/resource/")) # Optional: Exclude category pages if you don't want them counted FILTER (!STRSTARTS(STR(?sub), "http://dbpedia.org/resource/Category:")) }
This works because:
- The first filter ensures we only look at URIs starting with the DBpedia resource prefix
- The optional second filter removes category pages (these live in the resource namespace but are metadata, not core entities like people or places)
A quick precision tip
If you want an even stricter count of "entities" (things like people, cities, or organizations), you could add a filter for entities with an ontology type (e.g., ?sub rdf:type dbo:Thing), but the namespace filter is usually sufficient for a general "total resources" count.
内容的提问来源于stack exchange,提问作者Xyz1337

