-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
54 lines (45 loc) · 1.69 KB
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
require 'json'
require 'linkeddata'
# Read JSON data from file
file_path = './dumps/entities.json'
json_data = File.read(file_path)
graph = RDF::Graph.new
base_url = "https://www.laval.ca"
replace_blank_nodes_sparql_file = File.read('./sparql/replace-blank-nodes.sparql')
add_derived_from_sparql_file = File.read('./sparql/add-derived-from.sparql')
# Parse JSON
data = JSON.parse(json_data)
# Initialize a hash to store key-value pairs
location_info = {}
# Iterate through each object in the JSON data
data.each do |event|
if event['Locations'].length != 0
event['Locations'].each do |location|
# Extract Id and TargetUrl and store as key-value pairs
location_info[location['Id']] = location['TargetUrl']
end
end
end
location_info.each do |key, value|
url = base_url + value
id = "https://www.laval.ca/" + key
puts id
place_graph = RDF::Graph.load(url)
replace_blank_node_sparql_file_with_id = replace_blank_nodes_sparql_file.gsub("place_url", id)
place_graph.query(SPARQL.parse(replace_blank_node_sparql_file_with_id, update: true))
add_derived_from_sparql_file_with_id = add_derived_from_sparql_file.gsub("subject_url", url)
place_graph.query(SPARQL.parse(add_derived_from_sparql_file_with_id, update: true))
graph << place_graph
rescue StandardError => e
puts "Error loading RDF from #{url}: #{e.message}"
break
end
File.write('./dumps/places.ttl', graph.dump(:ttl))
file1 = RDF::Turtle::Reader.open("./dumps/entities.ttl")
file2 = RDF::Turtle::Reader.open("./dumps/places.ttl")
combined_statements = file1.to_a + file2.to_a
RDF::Turtle::Writer.open("./dumps/entities.ttl") do |writer|
combined_statements.each do |statement|
writer << statement
end
end