Validate language code for Wikimedia languages [cl

2019-07-29 07:24发布

I have a shell script that uses Wikidata Query Service (WDQS) to get required data. The SPARQL query that run WDQS takes input parameter language code.

Is there a way that I can check in shell script if the input language code is a valid Wikimedia language code as the first column data in below link https://www.wikidata.org/wiki/Help:Wikimedia_language_codes/lists/all

1条回答
甜甜的少女心
2楼-- · 2019-07-29 08:19

These codes are possible values of 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).

You could retrieve all these codes using the following simple SPARQL query:

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

Try it!

In fact, the list you have linked to is periodically generated by a bot. The full query 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
}

You could:

  • paste the list of valid codes into your script, or
  • preload the list at your script startup, or
  • execute an ASK SPARQL query at every user input.

I would prefer the third option:

#!/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
查看更多
登录 后发表回答