仅获取英语 属性 值

Getting only english property value

我正在尝试获取包含英文简称的国家/地区列表:

# get a list countries with the corresponding ISO code
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
SELECT ?country ?countryLabel ?shortName (MAX(?pop) as ?population) ?coord ?isocode
WHERE 
{
  # instance of country
  ?country wdt:P31 wd:Q3624078.
  OPTIONAL {
     ?country rdfs:label ?countryLabel filter (lang(?countryLabel) = "en").
   }
  OPTIONAL {
    # https://www.wikidata.org/wiki/Property:P1813
    ?country wdt:P1813 ?shortName.
  }   
  OPTIONAL { 
    # get the population
     # https://www.wikidata.org/wiki/Property:P1082
     ?country wdt:P1082 ?pop. 
  }
  # get the iso countryCode
  { ?country wdt:P297 ?isocode }.
  # get the coordinate
  OPTIONAL { ?country wdt:P625 ?coord }.
} 
GROUP BY ?country ?countryLabel ?shortName ?population ?coord ?isocode 
ORDER BY ?countryLabel

try it!

不幸的是,还返回了标志和非英语版本的“shortName”。我尝试使用子查询但超时了。我想避免使用 wikibase 标签服务,因为我需要 运行 my local wikidata copy which uses Apache Jena

上的查询

如何获取国家的英文简称?例如。中国 People's republic of china and USA for United States of America?

这里有两个问题:

  1. 我们只需要过滤英文短名称,即我们需要在第二个 OPTIONAL 模式中有一个 filter (lang(?shortName) = "en") 子句
  2. 出于某种原因,有些标志带有英语语言标签,因此我们不得不以某种方式忽略它们 - 好东西,这里有一个语句限定符可以提供帮助:实例 (P31) relation to the Wikidata entity emoji flag sequence (Q28840786)

所以,总的来说,我们替换

OPTIONAL {
    # https://www.wikidata.org/wiki/Property:P1813
    ?country wdt:P1813 ?shortName.
} 

来自

OPTIONAL {
  ?country p:P1813 ?shortNameStmt. # get the short name statement
  ?shortNameStmt ps:P1813 ?shortName # the the short name value from the statement
  filter (lang(?shortName) = "en") # filter for English short names only
  filter not exists {?shortNameStmt pq:P31 wd:Q28840786} # ignore flags (aka emojis)
}

不过,由于多个简称,某些国家/地区将有多个条目。解决此问题的一种方法是使用一些聚合函数,如 samplemin/max,并为每个国家/地区选择一个短名称。