-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
74 lines (64 loc) · 1.73 KB
/
Rakefile
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# coding: utf-8
require 'rubygems'
require 'bundler/setup'
require 'csv'
require 'json'
require 'yaml'
require 'active_support/inflector'
require 'faraday'
require 'nokogiri'
require 'rdf/turtle'
require 'rdf/rdfxml'
class RDFParser
attr_reader :graph
def initialize(url, options = {})
@namespaces = options.fetch(:namespaces, {
'' => url,
})
@graph = RDF::Graph.load(url)
end
def own_subjects(criteria)
@graph.query(criteria).reject do |statement|
RDF::Node === statement.subject || statement.subject.qname || @namespaces.any?{|k,v| !k.empty? && statement.subject.pname.start_with?(v)}
end.map(&:subject)
end
def clean_object_url(subject, predicate)
statements = @graph.query(subject: subject, predicate: predicate).reject do |statement|
RDF::Node === statement.object || statement.object == RDF::OWL.differentFrom
end
if statements.empty?
'N/A'
else
statements.map{|statement| clean_url(statement.object)}.sort.join(', ')
end
end
def clean_object_literal(subject, predicate)
statements = @graph.query(subject: subject, predicate: predicate).select do |statement|
statement.object.language == :en
end
if statements.empty?
'N/A'
else
statements.map{|statement| statement.object.value}.sort.join(', ')
end
end
def clean_url(url)
if Array === url
if url.empty?
'N/A'
else
url.map{|url| clean_url(url)}.sort.join(', ')
end
elsif url
@namespaces.each do |replacement,pattern|
if url.pname.start_with?(pattern)
return url.pname.sub(pattern, replacement)
end
end
url.pname
else
'N/A'
end
end
end
Dir['tasks/*.rake'].each { |r| import r }