Class: RDF::Turtle::FreebaseReader

Inherits:
NTriples::Reader show all
Includes:
Terminals
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-turtle-6506e92ccd8e/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::LANG_DIR, 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::ESCAPE_CHARS_ESCAPED, NTriples::Reader::ESCAPE_CHARS_ESCAPED_REGEXP, NTriples::Reader::IRIREF, NTriples::Reader::IRI_RANGE, NTriples::Reader::LANGTAG, NTriples::Reader::LANG_DIR, 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::QT_END, NTriples::Reader::QT_START, NTriples::Reader::STRING_LITERAL_QUOTE, NTriples::Reader::SUBJECT, NTriples::Reader::TT_END, NTriples::Reader::TT_START, NTriples::Reader::UCHAR, NTriples::Reader::UCHAR4, NTriples::Reader::UCHAR8, NTriples::Reader::URIREF, NTriples::Reader::U_CHARS1, NTriples::Reader::U_CHARS2

Constants included from Util::Logger

Util::Logger::IOWrapper

Instance Attribute Summary

Attributes inherited from Reader

#options

Attributes included from Enumerable

#existentials, #universals

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_quotedTriple, #read_tripleTerm, #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_pg_statement, #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, #canonicalize, #canonicalize!, #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?, #graph_names, #invalid?, #method_missing, #object?, #objects, #predicate?, #predicates, #project_graph, #quad?, #quads, #respond_to_missing?, #statement?, #statements, #subject?, #subjects, #supports?, #term?, #terms, #to_a, #to_h, #to_set, #triple?, #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/3.3.0/bundler/gems/rdf-turtle-6506e92ccd8e/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



123
124
125
126
127
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-turtle-6506e92ccd8e/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_literalRDF::Literal

Returns:

See Also:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-turtle-6506e92ccd8e/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_numericRDF::Literal::Integer, ...

Read a numeric value



107
108
109
110
111
112
113
114
115
116
117
118
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-turtle-6506e92ccd8e/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) ⇒ 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
74
75
76
77
78
79
80
81
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-turtle-6506e92ccd8e/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_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/3.3.0/bundler/gems/rdf-turtle-6506e92ccd8e/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/3.3.0/bundler/gems/rdf-turtle-6506e92ccd8e/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