class Reality::Wikidata::Entity

Attributes

id[R]
predicates[R]

Public Class Methods

by_id(*ids) click to toggle source
# File lib/reality/wikidata.rb, line 30
def by_id(*ids)
  Query.by_id(*ids)
end
by_label(*labels) click to toggle source
# File lib/reality/wikidata.rb, line 34
def by_label(*labels)
  Query.by_label(*labels)
end
by_wikititle(*titles) click to toggle source
# File lib/reality/wikidata.rb, line 26
def by_wikititle(*titles)
  Query.by_wikititle(*titles)
end
from_sparql(sparql_json) click to toggle source
# File lib/reality/wikidata.rb, line 50
def from_sparql(sparql_json)
  JSON.parse(sparql_json)['results']['bindings'].map{|row|
    [
      row['s']['value'].sub('http://www.wikidata.org/entity/', ''),
      row['p']['value'].sub('http://www.wikidata.org/prop/direct/', ''),
      row['o'].merge('label' => row['oLabel']['value'])
    ]
  }.group_by(&:first).
  map{|id, rows|
    new(id, hash_from_predicates(rows))
  }
end
hash_from_predicates(rows) click to toggle source
# File lib/reality/wikidata.rb, line 63
def hash_from_predicates(rows)
  rows.map{|s, p, o| [p, parse_value(o)]}.
    group_by(&:first).map{|p, gs| [p, gs.map(&:last).compact]}.
    to_h
end
new(id, predicates) click to toggle source
# File lib/reality/wikidata.rb, line 117
def initialize(id, predicates)
  @id, @predicates = id, predicates
end
one_by_id(id) click to toggle source
# File lib/reality/wikidata.rb, line 42
def one_by_id(id)
  by_id(id).values.first
end
one_by_label(label) click to toggle source
# File lib/reality/wikidata.rb, line 46
def one_by_label(label)
  by_label(label).values.first
end
one_by_wikititle(title) click to toggle source
# File lib/reality/wikidata.rb, line 38
def one_by_wikititle(title)
  by_wikititle(title).values.first
end
parse_literal(hash) click to toggle source
# File lib/reality/wikidata.rb, line 91
def parse_literal(hash)
  case hash['datatype']
  when 'http://www.w3.org/2001/XMLSchema#decimal'
    hash['value'].to_i
  when 'http://www.w3.org/2001/XMLSchema#dateTime'
    DateTime.parse(hash['value'])
  when 'http://www.opengis.net/ont/geosparql#wktLiteral'
    # TODO: WTF
    if hash['value'] =~ /^\s*point\s*\(\s*([-\d.]+)\s+([-\d.]+)\s*\)\s*$/i
      lng, lat = $1, $2
      Geo::Coord.new(lat.to_f, lng.to_f)
    else
      fail ArgumentError, "Unparseable WKT: #{hash['value']}"
    end
  else
    if hash['xml:lang'] && hash['xml:lang'] != 'en'
      nil
    else
      hash['value']
    end
  end
end
parse_uri(hash) click to toggle source
# File lib/reality/wikidata.rb, line 83
def parse_uri(hash)
  if hash['value'] =~ %r{https?://www\.wikidata\.org/entity/([^/]+)$}
    Link.new($1, hash['label'])
  else
    hash['value']
  end
end
parse_value(hash) click to toggle source

FIXME: move all parse_* to util/parsers or wikidata/parsers

# File lib/reality/wikidata.rb, line 70
def parse_value(hash)
  case hash['type']
  when 'literal'
    parse_literal(hash)
  when 'uri'
    parse_uri(hash)
  when 'bnode'
    nil
  else
    fail ArgumentError, "Unidentifieble datatype: #{hash['type']} in #{hash}"
  end
end

Public Instance Methods

[](pred) click to toggle source
# File lib/reality/wikidata.rb, line 121
def [](pred)
  @predicates[pred]
end
about() click to toggle source
# File lib/reality/wikidata.rb, line 133
def about
  self['http://schema.org/about']
end
en_wikipage() click to toggle source
# File lib/reality/wikidata.rb, line 137
def en_wikipage
  return nil unless about

  name = about.first.
    scan(%r{https://en\.wikipedia\.org/wiki/(.+)$}).
    flatten.first.derp{|s| URI.unescape(s)}
end
id_i() click to toggle source
# File lib/reality/wikidata.rb, line 125
def id_i
  id.sub('Q', '').to_i
end
inspect() click to toggle source
# File lib/reality/wikidata.rb, line 145
def inspect
  "#<#{self.class}(#{[id, label].compact.join(': ')})>"
end
label() click to toggle source
# File lib/reality/wikidata.rb, line 129
def label
  self['http://www.w3.org/2000/01/rdf-schema#label'].first
end
to_h() click to toggle source
# File lib/reality/wikidata.rb, line 153
def to_h
  @predicates
end
to_s() click to toggle source
# File lib/reality/wikidata.rb, line 149
def to_s
  label || id
end