Skip to content

Instantly share code, notes, and snippets.

@yuiseki
Last active April 17, 2021 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuiseki/ca02942c588912815880f358568707a9 to your computer and use it in GitHub Desktop.
Save yuiseki/ca02942c588912815880f358568707a9 to your computer and use it in GitHub Desktop.
#!/bin/bash
# wikibase-dump-filter claim transformer
# Requirements
# - jq
# - wikidata-cli
# Usage:
# ./wdfc.sh '国籍:日本&職業:政治家&~死亡年月日'
# cat humans.ndjson | wikibase-dump-filter --claim `./wdfc.sh '国籍:日本&職業:政治家&~死亡年月日'` > politicians_japan.ndjson
# See also:
# wikibase-dump-filter
# https://github.com/maxlath/wikibase-dump-filter
# wikidata-cli
# https://github.com/maxlath/wikibase-cli
# TODO: support or operator
#if [[ $1 == *"|"* ]] || [[ $1 == *","* ]]; then
#fi
declare -a claims=();
claims=$(echo $1 | tr '&' ' ');
transformed=;
wdprop () {
wd props -l ja $1 | jq -r 'to_entries[] | select(.value == "'$1'") | .key'
}
for claim in ${claims[@]};
do
declare -a claim_split=();
declare property;
declare qualifier;
if [[ $claim == *":"* ]]; then
claim_split=($(echo $claim | tr ':' ' '));
property="${claim_split[0]}";
qualifier="${claim_split[1]}";
property_id=`wdprop $property`
qualifier_id=`wd id -l ja $qualifier`
if [ -z "$transformed" ]; then
transformed="$property_id:$qualifier_id"
else
transformed="$transformed&$property_id:$qualifier_id"
fi
else
if [[ $claim == *"~"* ]]; then
property=$(echo $claim | tr -d '~');
property_id=`wdprop $property`
if [ -z "$transformed" ]; then
transformed="~$property_id"
else
transformed="$transformed&~$property_id"
fi
fi
fi
done
echo $transformed
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment