Skip to content

Instantly share code, notes, and snippets.

@tmtmtmtm
Created June 20, 2018 09:09
Show Gist options
  • Save tmtmtmtm/3c5369a5e1465edecda8d04be50753a9 to your computer and use it in GitHub Desktop.
Save tmtmtmtm/3c5369a5e1465edecda8d04be50753a9 to your computer and use it in GitHub Desktop.
#!/bin/env ruby
# Find everyone who has a P39 with a party of the "USR" disambiguation page
# and change it to the "Save Romania Union" item.
require 'csv'
require 'rest-client'
WIKIDATA_SPARQL_URL = 'https://query.wikidata.org/sparql'
def sparql(query)
result = RestClient.get WIKIDATA_SPARQL_URL, accept: 'text/csv', params: { query: query }
CSV.parse(result.body, headers: true, header_converters: :symbol)
rescue RestClient::Exception => e
raise "Wikidata query #{query} failed: #{e.message}"
end
memberships_query = <<SPARQL
SELECT ?item ?ps
{
?item p:P39 ?ps .
?ps ps:P39/wdt:P279* wd:Q17556530 ; pq:P4100 wd:Q1177696 .
}
SPARQL
sparql(memberships_query).map(&:to_h).each do |row|
puts [row[:item].split('/').last, "P39", row[:ps].split('/').last, '-P4100', 'Q1177696'].join("\t")
puts [row[:item].split('/').last, "P39", row[:ps].split('/').last, 'P4100', 'Q27108508'].join("\t")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment