module Reality::Wikidata::Query

Constants

MAX_SLICE
PREDICATES

selecting

?entity wdt:* ?value
?entity rdfs:label ?label@en
?enwikiarticle schema:about ?entity
PREFIX
QUERY
SELECTORS
SERVICES
UNSAFE

Public Instance Methods

by_id(*ids) click to toggle source
# File lib/reality/wikidata/query.rb, line 66
def by_id(*ids)
  fetch(:id, ids.map{|id| SELECTORS[:id] % {id: id}} )
end
by_label(*labels) click to toggle source
# File lib/reality/wikidata/query.rb, line 70
def by_label(*labels)
  fetch(:label, labels.map{|l| SELECTORS[:label] % {label: l}} )
end
by_wikititle(*titles) click to toggle source
# File lib/reality/wikidata/query.rb, line 59
def by_wikititle(*titles)
  fetch(:en_wikipage, titles.
    map{|t| SELECTORS[:wikipedia] % {title: t.tr(' ', '_')}}
    #map{|t| SELECTORS[:wikipedia] % {title: URI.escape(t, UNSAFE)}}
  )
end
faraday() click to toggle source
# File lib/reality/wikidata/query.rb, line 96
def faraday
  @faraday ||= Faraday.new(url: 'https://query.wikidata.org/sparql'){|f|
    f.adapter Faraday.default_adapter
  }
end
fetch(key, selectors) click to toggle source
# File lib/reality/wikidata/query.rb, line 80
def fetch(key, selectors)
  selectors.each_slice(MAX_SLICE).map{|chunk|
      fetch_sublist(chunk)
    }.flatten(1).
    group_by(&key).
    # of several entities with same label, we always prefer first-by-id
    map{|key, group| group.sort_by(&:id_i).first}.
    map{|entity| [entity.send(key), entity]}.to_h
end
fetch_sublist(selectors) click to toggle source
# File lib/reality/wikidata/query.rb, line 90
def fetch_sublist(selectors)
  make_query(selectors).
    derp{|query| faraday.get('', query: query, format: :json)}.
    derp{|res| Wikidata::Entity.from_sparql(res.body)}
end
make_query(selectors) click to toggle source
# File lib/reality/wikidata/query.rb, line 102
def make_query(selectors)
  QUERY % {selectors: selectors.map{|sel| "{#{sel}}"}.join(" UNION ")}
end