Last active
September 26, 2019 07:30
-
-
Save masao/08a14cb14a005ca26e183a7b9b410f07 to your computer and use it in GitHub Desktop.
Convert AP template Excel file to SHACL (RDF/Turtle) format.
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 | |
require "roo" | |
xlsx = Roo::Spreadsheet.open(ARGV[0]) | |
sheets = xlsx.sheets | |
puts <<-EOF | |
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>. | |
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. | |
@prefix owl: <http://www.w3.org/2002/07/owl#>. | |
@prefix dct: <http://purl.org/dc/terms/>. | |
@prefix sh: <http://www.w3.org/ns/shacl#>. | |
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>. | |
@prefix ex: <http://example.org/>. | |
EOF | |
prev_entity_id = nil | |
sheets.each do |sheet_name| | |
props = [] | |
xlsx.sheet(sheet_name).parse(headers: true) do |row| | |
# cardinality property description name value kind | |
#p row | |
min, max = nil | |
shapes = [] | |
if row["property"] =~ /:/ | |
shapes << "sh:path #{row["property"]}" | |
else | |
shapes << "sh:path ex:#{row["property"]}" | |
end | |
shapes << "sh:name #{row["name"].inspect}" if row["name"] | |
shapes << "sh:description #{row["description"].inspect}" if row["description"] | |
case row["value kind"] | |
when "string" | |
shapes << "sh:datatype xsd:string" | |
when "URI" | |
shapes << "sh:nodeKind sh:IRI" | |
when /^EntityRef:(\w+)$/ | |
shapes << "sh:class ex:#$1" | |
end | |
if row[:cardinality] | |
min, max, = row[:cardinality].split(/-/) | |
min = min.to_i if min | |
max = nil if max == "n" | |
end | |
props << shapes | |
end | |
puts "ex:#{sheet_name.capitalize}Shape a sh:NodeShape;" | |
puts " sh:property [" | |
puts " " + props.map{|e| e.join(";\n ") }.join("\n ];\n sh:property [\n ") | |
puts " ]." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment