This repository was archived by the owner on Nov 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_rdf_file.rb
More file actions
66 lines (55 loc) · 1.81 KB
/
parse_rdf_file.rb
File metadata and controls
66 lines (55 loc) · 1.81 KB
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
# Following tutorial at http://blog.datagraph.org/2010/04/parsing-rdf-with-ruby
require 'rdf'
require 'sparql'
require 'net/http'
require 'openssl'
require 'linkeddata'
# 5. method to get the abstract from a person's interest
def info_for(interest)
abs_query = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?label ?abs
WHERE { ?s dbo:abstract ?abs;
rdfs:label ?label
FILTER (lang(?abs) = 'en')
FILTER (lang(?label) = 'en')}"
abs_graph = RDF::Graph.load(interest)
sparql_abstracts_query = SPARQL.parse(abs_query)
sparql_abstracts_query.execute(abs_graph) do |result|
puts result.label
puts result.abs
end
end
# abstract_for('http://dbpedia.org/resource/Quilting')
graph = RDF::Graph.load("foaf_files/sdoljack_foaf.rdf")
# graph = RDF::Graph.load("http://stanford.edu/~sdoljack/sdoljack_foaf.rdf")
# after creating an RDF::Graph object, check what type of object it is with .inspect
puts graph.inspect
# 2. Find everyone I know
query = "
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?o
WHERE { ?s foaf:knows ?o }
"
# 3. Load all of their FOAF files into the same graph as mine
puts "before loading"
sse = SPARQL.parse(query)
sse.execute(graph) do |result|
puts result.o
graph.load(result.o) # graph should now contain the contents of the foaf files of people I know
end
# 4. What are the interests of people I know
interests_query = "
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?interest ?name
WHERE { ?s foaf:interest ?interest;
foaf:name ?name}
"
puts "People's interests"
q_parsed = SPARQL.parse(interests_query)
q_parsed.execute(graph) do |result|
puts result.name
# puts result.interest
info_for(result.interest)
end