如何通过正则表达式过滤标签?

问题描述 投票:0回答:1

忽略了用正则表达式过滤。FILTER(!REGEX(STR(?aLabel), "^Q[0-9]+$"))... 如何使用 "按标签过滤"?


实际案例

SELECT ?a ?aLabel ?lat ?long WHERE {
  ?a wdt:P31 wd:Q274393 .   # bakery or scholl or etc.
  ?a p:P625 ?statement .    # that has coordinate-location statement

  ?statement psv:P625 ?coordinate_node .
  ?coordinate_node wikibase:geoLatitude ?lat .
  ?coordinate_node wikibase:geoLongitude ?long .

  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en,[AUTO_LANGUAGE]" .
  }
  #FILTER(!REGEX(STR(?aLabel), "^Q[0-9]+$")) # not working, no items !  
  #FILTER(!REGEX(STR(?a), "^Q[0-9]+$")) # not working, ignored !
}
ORDER BY (?aLabel)  # need to eliminate ugly itens with no name

您可以 编在这里.


PS:这不是问题,而是现实生活中的另一种解决方法,很有意思,可以评论一下,是一个检查 "无语言标签 "或 "空标签 "的子句。

sparql wikidata
1个回答
2
投票

正如@不知情的用户所评论的那样。

标签,如你的 ?aLabel 是来自一些特殊的非标准服务的神奇变量,因此,在查询被评估后才会发生。

所以,为了避免魔法,我们可以尝试在子查询中隔离它...... 很好用

SELECT *
WHERE {
  # no constraints here in the main query, bypass the subquery
  { # subquery:
    SELECT ?a ?aLabel ?lat ?long 
    WHERE {
      ?a wdt:P31 wd:Q274393 .   # bakery or scholl or etc.
      ?a p:P625 ?statement .    # that has coordinate-location statement
      ?statement psv:P625 ?coordinate_node .
      ?coordinate_node wikibase:geoLatitude ?lat .
      ?coordinate_node wikibase:geoLongitude ?long .
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en,[AUTO_LANGUAGE]" . }
    }
    ORDER BY (?aLabel)  
  }
  FILTER(!REGEX(STR(?aLabel), "^Q[0-9]+$")) # to eliminate ugly itens with no name
}

查看或 编在这里.

过滤无名氏的可选解决方案

正如题末评论的那样,解决实际问题的另一个办法,就是用一个子句来检查 "无语言标签 "或 "空标签"。不需要regex,也不需要子查询,只需要在上面添加一个 FILTER EXISTS 在原查询。

SELECT ?a ?aLabel ?lat ?long WHERE {
  ?a wdt:P31 wd:Q274393 .   # bakery or scholl or etc.
  ?a p:P625 ?statement .    # that has coordinate-location statement

  ?statement psv:P625 ?coordinate_node .
  ?coordinate_node wikibase:geoLatitude ?lat .
  ?coordinate_node wikibase:geoLongitude ?long .
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en,[AUTO_LANGUAGE]" .
  }
  FILTER EXISTS {
    ?a rdfs:label ?someLabel filter(langmatches(lang(?someLabel), "[AUTO_LANGUAGE]"))
  } 
}
ORDER BY (?aLabel)

见或 编在这里

© www.soinside.com 2019 - 2024. All rights reserved.