Module: SPARQLQueries

Included in:
BrowserWebData::SPARQLRequest
Defined in:
lib/utils/sparql_queries.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



122
123
124
# File 'lib/utils/sparql_queries.rb', line 122

def self.included(base)
  base.extend SPARQLQueries
end

Instance Method Details

#all_predicates_by_object(object) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/utils/sparql_queries.rb', line 22

def all_predicates_by_object(object)
  object = object['http'] ? "<#{object}>" : "dbo:#{object}"

  " PREFIX dbo: <http://dbpedia.org/ontology/>
    PREFIX dbp: <http://dbpedia.org/property/>

    SELECT DISTINCT ?property

    WHERE {
      ?subject ?property #{object}.
    }"
end

#all_predicates_by_object_and_subject(subject, object) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/utils/sparql_queries.rb', line 50

def all_predicates_by_object_and_subject(subject, object)
  subject = subject['http'] ? "<#{subject}>" : "dbo:#{subject}"
  object = object['http'] ? "<#{object}>" : "dbo:#{object}"

  " PREFIX dbo: <http://dbpedia.org/ontology/>
    PREFIX dbp: <http://dbpedia.org/property/>

    SELECT DISTINCT ?property

    WHERE {
      #{subject} ?property #{object}.
    }"
end

#all_predicates_by_subject(subject, only_literal) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/utils/sparql_queries.rb', line 35

def all_predicates_by_subject(subject, only_literal)
  subject = subject['http'] ? "<#{subject}>" : "dbo:#{subject}"
  filter = only_literal ? 'FILTER(isLiteral(?object))' : nil

  " PREFIX dbo: <http://dbpedia.org/ontology/>
    PREFIX dbp: <http://dbpedia.org/property/>

    SELECT DISTINCT ?property

    WHERE {
      #{subject} ?property ?object.
      #{filter}
    }"
end

#count_of_identical_predicates(predicates) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/utils/sparql_queries.rb', line 81

def count_of_identical_predicates(predicates)
  predicates = [predicates] unless predicates.is_a?(Array)
  where_part = predicates.map{|predicate|
    predicate = predicate['http'] ? "<#{predicate}>" : "dbo:#{predicate}"
    "?subject #{predicate} ?object ."
  }.join("\n")

  " SELECT COUNT(DISTINCT ?subject) AS ?count
    WHERE{#{where_part}
   }"
end

#count_predicate_by_entity(entity_class, predicate) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/utils/sparql_queries.rb', line 64

def count_predicate_by_entity(entity_class, predicate)
  entity_class = entity_class['http'] ? "<#{entity_class}>" : "dbo:#{entity_class}"
  predicate = predicate['http'] ? "<#{predicate}>" : "dbo:#{predicate}"

  " PREFIX dbo: <http://dbpedia.org/ontology/>
    PREFIX dbp: <http://dbpedia.org/property/>

    SELECT DISTINCT COUNT(?subject) as ?count

    WHERE {
      ?subject a #{entity_class} .
      {?subject #{predicate} ?a .} UNION {?b #{predicate} ?subject .}
    }

    ORDER BY DESC(?count)"
end

#entity_classes(resource) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/utils/sparql_queries.rb', line 112

def entity_classes(resource)
  resource = resource['http'] ? "<#{resource}>" : "<http://dbpedia.org/resource/#{resource}"

  " SELECT DISTINCT ?entity_class
    WHERE {
      #{resource} a ?entity_class .
      ?entity_class a owl:Class .
    }"
end

#resource_properties(resource, lang = 'en') ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/utils/sparql_queries.rb', line 93

def resource_properties(resource, lang = 'en')
  resource = resource['http'] ? "<#{resource}>" : "<http://dbpedia.org/resource/#{resource}>"

  " PREFIX dbo: <http://dbpedia.org/ontology/>
    PREFIX dbp: <http://dbpedia.org/property/>
    SELECT DISTINCT ?predicate, ?predicate_label, ?value, ?value_label
    WHERE {
      { #{resource} ?predicate ?value . } UNION { ?value ?predicate #{resource} . }

      OPTIONAL{
        ?value rdfs:label ?value_label .
        FILTER (lang(?value_label) = '#{lang}')
      }

      ?predicate rdfs:label ?predicate_label .
      FILTER (lang(?predicate_label) = '#{lang}')
    }"
end

#resources_by_dbpedia_page_rank(entity_type, limit = 10) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/utils/sparql_queries.rb', line 5

def resources_by_dbpedia_page_rank(entity_type, limit = 10)
  entity_type = entity_type['http'] ? "<#{entity_type}>" : "dbo:#{entity_type}"

  " PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX dbo:<http://dbpedia.org/ontology/>
    PREFIX vrank:<http://purl.org/voc/vrank#>

    SELECT ?entity ?rank
    FROM <http://dbpedia.org>
    FROM <http://people.aifb.kit.edu/ath/#DBpedia_PageRank>
    WHERE {
      ?entity rdf:type #{entity_type}.
      ?entity vrank:hasRank/vrank:rankValue ?rank.
    }
    ORDER BY DESC(?rank) LIMIT #{limit}"
end