如何修改SPARQL查询以在DBpedia中搜索指定文章?
解决DBpedia中搜索文章的SPARQL查询问题
你的原查询仅针对dbo:Person类型的资源,自然无法匹配文章类条目。要搜索《God Created All Things》这类文章,需要调整查询的资源类型、属性匹配逻辑,以下是可行方案:
核心调整方向
- 指定文章类资源类型:DBpedia中普通文章对应的本体类型是
dbo:Article;如果目标是书籍类内容,可替换为dbo:Book。 - 匹配文章的标题属性:文章的标题通常使用
dc:title(Dublin Core标准属性)或rdfs:label,而非人物类用的foaf:name,需调整属性绑定逻辑。
示例查询
方案1:模糊匹配文章标题
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX dbo: <http://dbpedia.org/ontology/> PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?title ?article ?description WHERE { ?article rdf:type dbo:Article. # 兼容不同属性的标题,优先取dc:title,无则用rdfs:label OPTIONAL { ?article dc:title ?title } OPTIONAL { ?article rdfs:label ?title } ?article rdfs:comment ?description . # 忽略大小写模糊匹配标题关键词 FILTER (regex(?title, 'God Created All Things', 'i')) . # 限定描述语言为印尼语(需英文结果可改为'en') FILTER (lang(?description) = 'in') } ORDER BY ?title
方案2:同时匹配标题与描述
若需扩大搜索范围,同时在标题和描述中查找关键词,可调整过滤逻辑:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX dbo: <http://dbpedia.org/ontology/> PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?title ?article ?description WHERE { ?article rdf:type dbo:Article. OPTIONAL { ?article dc:title ?title } OPTIONAL { ?article rdfs:label ?title } ?article rdfs:comment ?description . # 匹配标题或描述中的目标关键词 FILTER (regex(?title, 'Birth of Jesus', 'i') || regex(?description, 'Birth of Jesus', 'i')) . FILTER (lang(?description) = 'in') } ORDER BY ?title
额外说明
- 若目标内容是书籍而非普通文章,将
dbo:Article替换为dbo:Book即可。 OPTIONAL关键字用于兼容不同资源的标题属性,避免因资源仅使用rdfs:label而被过滤。
内容的提问来源于stack exchange,提问作者Arif Nur Rohman
相关产品推荐
相关产品推荐

