Skip to content

Instantly share code, notes, and snippets.

@tmtmtmtm
Created May 21, 2018 09:54
Show Gist options
  • Save tmtmtmtm/f0aa6428f5421cf730393e13c296e825 to your computer and use it in GitHub Desktop.
Save tmtmtmtm/f0aa6428f5421cf730393e13c296e825 to your computer and use it in GitHub Desktop.
#!/bin/env ruby
require 'csv'
require 'rest-client'
#---------------------------------------------------------------------------
# Find all P4100s from the ALDE disambiguation item to the proper one
#---------------------------------------------------------------------------
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:Q23919488 .
}
SPARQL
sparql(memberships_query).map(&:to_h).each do |row|
puts [row[:item].split('/').last, 'P39', row[:ps].split('/').last, '-P4100', 'Q23919488'].join("\t")
puts [row[:item].split('/').last, 'P39', row[:ps].split('/').last, 'P4100', 'Q26934816'].join("\t")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment