如何使用标签查询Wikidata条目?

22

我该如何查询Wikidata以获取所有标签中包含某个词的项?我尝试了这个方法,但没有起作用;它什么都没检索到。

SELECT ?item ?itemLabel WHERE {
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en".
    ?item rdfs:label ?itemLabel.  
  }
FILTER(CONTAINS(LCASE(?itemLabel), "keyword"))
}
LIMIT 1000

wikibase:label是什么?没有前缀很难说出问题出在哪里。 - UninformedUser
PREFIX wikibase: http://wikiba.se/ontology# - fattah.safa
感谢AKSW的回答。我尝试了一下,但是出现了“查询超时错误:SPARQL-QUERY:queryStr=SELECT ?item ?itemLabel WHERE { SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } ?item rdfs:label ?itemLabel.
FILTER(CONTAINS(LCASE(?itemLabel), "palestine")) }”。
- fattah.safa
1
我的意思是相反的。我以为你想使用在SERVICE子句中指定的本体图http://wikiba.se/ontology-1.0.owl#中的标签。而这个本体图不包含属性wikibase:language,因此你应该将它放在SERVICE子句之外,而不是另一个子句之内。但说实话,你的查询想要得到什么并不清楚。特别是,你用一个前缀URI表示属性,但也许你只想要本体。 - UninformedUser
1
你在哪里运行查询?为什么不能直接在Wikidata SPARQL端点上执行此操作,而不使用SERVICE子句? - UninformedUser
显示剩余2条评论
4个回答

15

是的,你可以通过标签进行搜索,例如:

SELECT distinct ?item ?itemLabel ?itemDescription WHERE{  
  ?item ?label "Something"@en.  
  ?article schema:about ?item .
  ?article schema:inLanguage "en" .
  ?article schema:isPartOf <https://en.wikipedia.org/>. 
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }    
}

查询页面中查看。


3
这个答案适用于2022年。被接受的答案不适用。 - DrMcCleod
itemLabel and itemDescription are implicitly defined properties in SPARQL. They are not weird extensions but rather a feature of the language. - undefined

12

在阅读了您的问题和有用的评论后,我最终得出了这个查询。

SELECT ?item ?itemLabel
WHERE { 
  ?item rdfs:label ?itemLabel. 
  FILTER(CONTAINS(LCASE(?itemLabel), "city"@en)). 
} limit 10

我得到了这些结果的原因

item          itemLabel
wd:Q515       city
wd:Q7930989   city
wd:Q15253706  city
wd:Q532039    The Eternal City
wd:Q1969820   The Eternal City
wd:Q3986838   The Eternal City
wd:Q7732543   The Eternal City
wd:Q7737016   The Golden City
wd:Q5119      capital city
wd:Q1555      Guatemala City

在这里尝试一下


4
更新一下,大多数情况下(适用于其他标签),这个查询由于超时异常而被终止。 - fattah.safa
@fattah.safa 我刚刚再次尝试了一下,它只用了不到2秒钟就完成了。 - innovimax
正如@fattah.safa所说,它在许多情况下会为其他标签引发超时。我尝试将“city”更改为其他小写字符串,有些可以工作,但有些不行:例如,“washington”或“oslo”可以工作,但例如“curitiba”或“ourense” 以超时结束,尽管这些地方都有英文的维基数据维基百科条目。有什么线索吗? - abu

3
截至2020年6月,目前最好的方法似乎是使用这些CirrusSearch扩展。以下内容对所有英文标签进行子字符串搜索,并在不到20秒内返回10000个结果。我相信它还会搜索别名和描述。
SELECT DISTINCT ?item ?label
WHERE
{
  SERVICE wikibase:mwapi
  {
    bd:serviceParam wikibase:endpoint "www.wikidata.org";
                    wikibase:api "Generator";
                    mwapi:generator "search";
                    mwapi:gsrsearch "inlabel:city"@en;
                    mwapi:gsrlimit "max".
    ?item wikibase:apiOutputItem mwapi:title.
  }
  ?item rdfs:label ?label. FILTER( LANG(?label)="en" )

  # … at this point, you have matching ?item(s) 
  # and can further restrict or use them
  # as in any other SPARQL query

  # Example: the following restricts the matches
  # to college towns (Q1187811) only

  ?item wdt:P31 wd:Q1187811 .
}

链接到此查询


看起来快多了,但我不明白如何扩展查询,例如如果标签必须包含城市并且实体是地方的一个实例,而不是电影等等。 - G M
1
@GM 我已编辑示例以显示如何进一步限制实体。 - Matthias Winkelmann

0
如上所述,在SPARQL查询服务中,使用不区分大小写和截断的查询非常缓慢。我在Github上找到了这个项目:https://github.com/inventaire/entities-search-engine。它建立了一个ElasticSearch索引,可以快速查询用于自动完成等用例。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接