migrants/map/step-02.rb
Daniel Hernandez d2481d6e80 Add Step 2: SPARQL UPDATE queries to transform literals into objects.
19 queries in updates/ convert categorical columns (continent, country,
city, gender, profession, etc.) from literals to typed RDF objects with
rdfs:label. map/step-02.rb applies them to produce data/graph-02.ttl.
Also fix step-01.rb to sanitize column names with spaces and avoid
prefix serialization issues with fragment IRIs.
2026-02-26 19:45:08 +01:00

38 lines
1.1 KiB
Ruby

#!/usr/bin/env ruby
# frozen_string_literal: true
# Step 2: Transform literal values into RDF objects using SPARQL UPDATE queries
require 'rdf'
require 'rdf/turtle'
require 'sparql'
input_path = File.expand_path('../data/graph-01.ttl', __dir__)
output_path = File.expand_path('../data/graph-02.ttl', __dir__)
updates_dir = File.expand_path('../updates', __dir__)
puts "Loading graph from #{input_path}..."
graph = RDF::Graph.load(input_path)
puts "Loaded #{graph.count} triples."
Dir.glob(File.join(updates_dir, '*.rq')).sort.each do |query_file|
query = File.read(query_file)
name = File.basename(query_file)
before = graph.count
SPARQL.execute(query, graph, update: true)
after = graph.count
puts "Applied #{name}: #{before} -> #{after} triples (#{after - before >= 0 ? '+' : ''}#{after - before})"
end
puts "Writing #{graph.count} triples to #{output_path}..."
RDF::Turtle::Writer.open(output_path, prefixes: {
rdf: RDF.to_uri,
rdfs: RDF::RDFS.to_uri,
xsd: RDF::XSD.to_uri,
base: RDF::URI.new('http://example.org/migrants/')
}) do |writer|
graph.each_statement { |stmt| writer << stmt }
end
puts "Done."