验证维基媒体语言的语言代码

Validate language code for Wikimedia languages

我有一个 shell 脚本,它使用维基数据查询服务 (WDQS) 来获取所需的数据。 运行WDQS作为输入参数语言代码的SPARQL查询。

如果输入语言代码是有效的维基媒体语言代码作为下方第一列数据,我有没有办法签入 shell 脚本 link https://www.wikidata.org/wiki/Help:Wikimedia_language_codes/lists/all

这些代码是 wdt:P424. From the property proposal 的可能值:

— Is there a big difference to ISO 639-1?
— Many of them are the same as ISO, but it is not done in a consistent way. Some language codes have two letters, some three, and a few even more. And there are also a few cases where it is completely different (als: ISO: tosk Albanian, Wikimedia: Alemannic).

您可以使用以下简单的 SPARQL 查询检索所有这些代码:

SELECT DISTINCT ?code { [] wdt:P424 ?code } ORDER BY ?code

Try it!

事实上,您链接到的列表是由机器人定期生成的。完整查询 is:

SELECT ?item ?c
(CONCAT("{","{#language:",?c,"}","}") as ?display)
(CONCAT("{","{#language:",?c,"|","en}","}") as ?displayEN)
(CONCAT("{","{#language:",?c,"|","fr}","}") as ?displayFR)
{
  ?item wdt:P424 ?c .
  MINUS{?item wdt:P31/wdt:P279* wd:Q14827288} #--exclude Wikimedia projects
  MINUS{?item wdt:P31/wdt:P279* wd:Q17442446} #--exclude Wikimedia internal stuff
}

你可以:

  • 将有效代码列表粘贴到您的脚本中,
  • 在脚本启动时预加载列表,
  • 在每次用户输入时执行 ASK SPARQL 查询。

我更喜欢第三个选项:

#!/bin/sh
echo "Enter language code:"
read code
request="curl -g -s https://query.wikidata.org/sparql?query=ASK{?lang%20wdt:P424%20\"$code\"}"

if $request | grep -q "true"; then
    echo "Valid code";
else 
    echo "Invalid code";
fi