class RDF::Turtle::FreebaseReader

Parser specifically for Freebase, which has a very regular form.

@see developers.google.com/freebase/data

Public Class Methods

format() click to toggle source
# File lib/rdf/turtle/freebase_reader.rb, line 12
def self.format; RDF::Turtle::Format; end

Public Instance Methods

read_boolean() click to toggle source

Read a boolean value @return [RDF::Literal::Boolean]

# File lib/rdf/turtle/freebase_reader.rb, line 123
def read_boolean
  if bool_str = match(/^(true|false)/)
    RDF::Literal::Boolean.new(bool_str, canonicalize:  canonicalize?)
  end
end
read_literal() click to toggle source

@return [RDF::Literal] @see www.w3.org/TR/rdf-testcases/#ntrip_grammar (literal)

# File lib/rdf/turtle/freebase_reader.rb, line 86
def read_literal
  if literal_str = match(LITERAL_PLAIN)
    literal_str = self.class.unescape(literal_str)
    literal = case
      when lang_dir = match(RDF::Turtle::Reader::LANG_DIR)
        language, direction = lang_dir.split('--')
        RDF::Literal.new(literal_str, language:  language, direction: direction)
      when datatype = match(/^(\^\^)/)
        RDF::Literal.new(literal_str, datatype:  read_pname(intern:  true) || read_uriref || fail_object)
      else
        RDF::Literal.new(literal_str) # plain string literal
    end
    literal.validate!     if validate?
    literal.canonicalize! if canonicalize?
    literal
  end
end
read_numeric() click to toggle source

Read a numeric value @return [RDF::Literal::Integer, RDF::Literal::Float, RDF::Literal::Double]

# File lib/rdf/turtle/freebase_reader.rb, line 107
def read_numeric
  case
  when double_str = match(/^(#{DOUBLE})/)
    double_str = double_str.sub(/\.([eE])/, '.0\1')
    RDF::Literal::Double.new(double_str, canonicalize:  canonicalize?)
  when decimal_str = match(/^(#{DECIMAL})/)
    decimal_str = "0#{decimal_str}" if decimal_str[0,1] == "."
    RDF::Literal::Decimal.new(decimal_str, canonicalize:  canonicalize?)
  when integer_str = match(/^(#{INTEGER})/)
    RDF::Literal::Integer.new(integer_str, canonicalize:  canonicalize?)
  end
end
read_pname(**options) click to toggle source

Read a PNAME of the form ‘prefix:suffix`. @return [RDF::URI]

# File lib/rdf/turtle/freebase_reader.rb, line 58
def read_pname(**options)
  if pname_str = match(/^(\w+:\S+)/)
    ns, suffix = pname_str.split(':', 2)
    if suffix[-1,1] == "."
      suffix.chop!  # Remove end of statement
      @line.insert(0, ".")
    end
    pfx_iri = prefix(ns)
    raise RDF::ReaderError.new("ERROR [line #{lineno}] prefix #{ns.inspect} is not defined", lineno: lineno) unless pfx_iri

    # Unescape PN_LOCAL_ESC
    suffix = suffix.gsub(PN_LOCAL_ESC) {|esc| esc[1]} if
      suffix.match?(PN_LOCAL_ESC)

    # Remove any redundant leading hash from suffix
    suffix = suffix.sub(/^\#/, "") if pfx_iri.to_s.index("#")

    uri = RDF::URI(pfx_iri + suffix)
    uri.validate!     if validate?
    uri
  end
rescue ArgumentError => e
  raise RDF::ReaderError.new("ERROR [line #{lineno}] invalid PName", lineno: lineno)
end
read_prefix() click to toggle source

Read a prefix of the form ‘@prefix pfx: <uri> .

Add prefix definition to ‘prefixes` @return [RDF::URI]

# File lib/rdf/turtle/freebase_reader.rb, line 47
def read_prefix
  if prefix_str = match(/^@prefix\s+(\w+:\s+#{IRIREF})\s*.$/)
    prefix, iri = prefix_str.split(/:\s+/)
    return nil unless iri
    prefix(prefix, iri[1..-2])
  end
end
read_triple() click to toggle source

Extension to N-Triples reader, includes reading pnames and prefixes

# File lib/rdf/turtle/freebase_reader.rb, line 17
def read_triple
  loop do
    begin
      readline.strip!
      line = @line
      unless blank? || read_prefix
        subject   = read_pname(intern:  true) || fail_subject
        predicate = read_pname(intern:  true) || fail_predicate
        object    = read_pname || read_uriref || read_boolean || read_numeric || read_literal || fail_object
        if validate? && !read_eos
          log_error("Expected end of statement (found: #{current_line.inspect})", lineno: lineno, exception: RDF::ReaderError)
        end
        return [subject, predicate, object]
      end
    rescue RDF::ReaderError =>  e
      raise e if validate?
      if @options[:errors]
        @options[:errors] << e.message
      else
        $stderr.puts e.message
      end
    end
  end
end