Created
December 12, 2016 07:45
-
-
Save ktym/99c9c64b568a31f0d2397d71e125e0b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby-2.3 | |
=begin | |
# SPARQL endpoint | |
https://stirdf.jglobal.jst.go.jp/sparql | |
# SPARQL query | |
select * | |
where { | |
?term a skos:Concept ; | |
rdfs:seeAlso ?mesh . | |
FILTER strstarts(str(?mesh), "http://id.nlm.nih.gov/mesh/") | |
?term rdfs:label ?label . | |
OPTIONAL { | |
?term dc:subject ?subject ; | |
dct:subject ?subject_uri . | |
} | |
OPTIONAL { ?term skos:narrower ?narrower . } | |
OPTIONAL { ?term skos:broader ?broader . } | |
OPTIONAL { ?term skos:related ?related . } | |
} | |
# Download into CSV | |
OFFSET 0 LIMIT 40000 | |
OFFSET 40000 LIMIT 40000 | |
=end | |
require 'csv' | |
header = CSV.parse(ARGF.gets).shift | |
# ["term", "mesh", "label", "subject", "subject_uri", "narrower", "broader", "related"] | |
# wrap uri | |
def u(str) | |
"<#{str}>" | |
end | |
# quote literal | |
def q(str) | |
%Q("#{str}") | |
end | |
# expand uri | |
def x(predicate) | |
pref, term = predicate.split(':') | |
case pref | |
when 'rdfs' | |
"<http://www.w3.org/2000/01/rdf-schema#" + term + ">" | |
when 'skos' | |
"<http://www.w3.org/2004/02/skos/core#" + term + ">" | |
when 'dc' | |
"<http://purl.org/dc/elements/1.1/" + term + ">" | |
when 'dct' | |
"<http://purl.org/dc/terms/" + term + ">" | |
end | |
end | |
# triple | |
def t(s, p, o) | |
puts [s, p, o, '.'].join("\t") | |
end | |
CSV.parse(ARGF.read).each do |row| | |
term, mesh, label, subject, subject_uri, narrower, broader, related = *row | |
t(u(term), x('rdfs:seeAlso'), u(mesh)) | |
t(u(term), x('rdfs:label'), q(label)) | |
t(u(term), x('dc:subject'), q(subject)) | |
t(u(term), x('dct:subject'), u(subject_uri)) | |
t(u(term), x('skos:narrower'), u(narrower)) | |
t(u(term), x('skos:broader'), u(broader)) | |
t(u(term), x('skos:related'), u(related)) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment