Class: RDF::Turtle::FreebaseReader

Inherits:
NTriples::Reader show all
Includes:
Terminals
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/rdf-turtle-f03e5032c9ee/lib/rdf/turtle/freebase_reader.rb

Overview

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

Constant Summary

Constants included from Terminals

Terminals::ANON, Terminals::BASE, Terminals::BLANK_NODE_LABEL, Terminals::DECIMAL, Terminals::DOUBLE, Terminals::ECHAR, Terminals::EXPONENT, Terminals::INTEGER, Terminals::IRIREF, Terminals::IRI_RANGE, Terminals::LANGTAG, Terminals::PERCENT, Terminals::PLX, Terminals::PNAME_LN, Terminals::PNAME_NS, Terminals::PN_CHARS, Terminals::PN_CHARS_BASE, Terminals::PN_CHARS_BODY, Terminals::PN_CHARS_U, Terminals::PN_LOCAL, Terminals::PN_LOCAL_BODY, Terminals::PN_LOCAL_ESC, Terminals::PN_PREFIX, Terminals::PREFIX, Terminals::STRING_LITERAL_LONG_QUOTE, Terminals::STRING_LITERAL_LONG_SINGLE_QUOTE, Terminals::STRING_LITERAL_QUOTE, Terminals::STRING_LITERAL_SINGLE_QUOTE, Terminals::UCHAR, Terminals::U_CHARS1, Terminals::U_CHARS2, Terminals::WS

Constants inherited from NTriples::Reader

NTriples::Reader::BLANK_NODE_LABEL, NTriples::Reader::COMMENT, NTriples::Reader::DATATYPE_URI, NTriples::Reader::ECHAR, NTriples::Reader::END_OF_STATEMENT, NTriples::Reader::ESCAPE_CHARS, NTriples::Reader::IRIREF, NTriples::Reader::IRI_RANGE, NTriples::Reader::LANGTAG, NTriples::Reader::LITERAL, NTriples::Reader::LITERAL_PLAIN, NTriples::Reader::LITERAL_WITH_DATATYPE, NTriples::Reader::LITERAL_WITH_LANGUAGE, NTriples::Reader::NODEID, NTriples::Reader::OBJECT, NTriples::Reader::PN_CHARS, NTriples::Reader::PN_CHARS_BASE, NTriples::Reader::PN_CHARS_U, NTriples::Reader::PREDICATE, NTriples::Reader::STRING_LITERAL_QUOTE, NTriples::Reader::SUBJECT, NTriples::Reader::UCHAR, NTriples::Reader::UCHAR4, NTriples::Reader::UCHAR8, NTriples::Reader::URIREF, NTriples::Reader::U_CHARS1, NTriples::Reader::U_CHARS2

Instance Attribute Summary

Attributes inherited from Reader

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NTriples::Reader

parse_literal, parse_node, parse_object, parse_predicate, parse_subject, parse_uri, #read_comment, #read_eos, #read_node, #read_uriref, #read_value, unescape, unserialize

Methods included from Util::Logger

#log_debug, #log_depth, #log_error, #log_fatal, #log_info, #log_recover, #log_recovering?, #log_statistics, #log_warn, #logger

Methods inherited from Reader

#base_uri, #canonicalize?, #close, each, #each_statement, #each_triple, #encoding, #fail_object, #fail_predicate, #fail_subject, for, #initialize, #intern?, #lineno, open, options, #prefix, #prefixes, #prefixes=, #read_statement, #rewind, #to_sym, to_sym, #valid?, #validate?

Methods included from Util::Aliasing::LateBound

#alias_method

Methods included from Enumerable

add_entailment, #dump, #each_graph, #each_object, #each_predicate, #each_quad, #each_statement, #each_subject, #each_term, #each_triple, #entail, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_term, #enum_triple, #graph_names, #has_graph?, #has_object?, #has_predicate?, #has_quad?, #has_statement?, #has_subject?, #has_term?, #has_triple?, #invalid?, #method_missing, #objects, #predicates, #project_graph, #quads, #respond_to_missing?, #statements, #subjects, #supports?, #terms, #to_a, #to_h, #to_set, #triples, #valid?, #validate!

Methods included from Isomorphic

#bijection_to, #isomorphic_with?

Methods included from Countable

#count, #empty?

Methods included from Readable

#readable?

Constructor Details

This class inherits a constructor from RDF::Reader

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RDF::Enumerable

Class Method Details

.formatObject



12
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-turtle-f03e5032c9ee/lib/rdf/turtle/freebase_reader.rb', line 12

def self.format; RDF::Turtle::Format; end

Instance Method Details

#read_booleanRDF::Literal::Boolean

Read a boolean value



114
115
116
117
118
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-turtle-f03e5032c9ee/lib/rdf/turtle/freebase_reader.rb', line 114

def read_boolean
  if bool_str = match(/^(true|false)/)
    RDF::Literal::Boolean.new(bool_str, canonicalize:  canonicalize?)
  end
end

#read_literalRDF::Literal

Returns:

See Also:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-turtle-f03e5032c9ee/lib/rdf/turtle/freebase_reader.rb', line 78

def read_literal
  if literal_str = match(LITERAL_PLAIN)
    literal_str = self.class.unescape(literal_str)
    literal = case
      when language = match(RDF::NTriples::Reader::LANGTAG)
        RDF::Literal.new(literal_str, language:  language)
      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_numericRDF::Literal::Integer, ...

Read a numeric value



98
99
100
101
102
103
104
105
106
107
108
109
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-turtle-f03e5032c9ee/lib/rdf/turtle/freebase_reader.rb', line 98

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 = {}) ⇒ RDF::URI

Read a PNAME of the form prefix:suffix.

Returns:



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-turtle-f03e5032c9ee/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
    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_prefixRDF::URI

Read a prefix of the form `@prefix pfx: .

Add prefix definition to prefixes

Returns:



47
48
49
50
51
52
53
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-turtle-f03e5032c9ee/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_tripleObject

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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-turtle-f03e5032c9ee/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