turtle_reader 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/COPYING +663 -0
- data/ChangeLog +11 -0
- data/README +41 -0
- data/Rakefile +25 -0
- data/lib/turtle_reader/rdf/compression.rb +96 -0
- data/lib/turtle_reader/rdf/prefix.rb +31 -0
- data/lib/turtle_reader/rdf/turtle/reader.rb +74 -0
- data/lib/turtle_reader/rdf/uri.rb +37 -0
- data/lib/turtle_reader/version.rb +27 -0
- data/lib/turtle_reader.rb +119 -0
- data/spec/data/GND-sample.ttl +996 -0
- data/spec/data/GND-sample.ttl.bz2 +0 -0
- data/spec/data/GND-sample.ttl.gz +0 -0
- data/spec/spec_helper.rb +13 -0
- data/spec/turtle_reader_spec.rb +55 -0
- metadata +144 -0
data/ChangeLog
ADDED
data/README
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
= turtle_reader - RDF Turtle reader
|
2
|
+
|
3
|
+
== VERSION
|
4
|
+
|
5
|
+
This documentation refers to turtle_reader version 0.0.1
|
6
|
+
|
7
|
+
|
8
|
+
== DESCRIPTION
|
9
|
+
|
10
|
+
A convenience wrapper for reading RDF Turtle data.
|
11
|
+
|
12
|
+
|
13
|
+
== LINKS
|
14
|
+
|
15
|
+
Documentation:: https://blackwinter.github.io/turtle_reader/
|
16
|
+
Source code:: https://github.com/blackwinter/turtle_reader
|
17
|
+
RubyGem:: https://rubygems.org/gems/turtle_reader
|
18
|
+
Travis CI:: https//travis-ci.org/blackwinter/turtle_reader
|
19
|
+
|
20
|
+
|
21
|
+
== AUTHORS
|
22
|
+
|
23
|
+
* Jens Wille <mailto:jens.wille@gmail.com>
|
24
|
+
|
25
|
+
|
26
|
+
== LICENSE AND COPYRIGHT
|
27
|
+
|
28
|
+
Copyright (C) 2014 Jens Wille
|
29
|
+
|
30
|
+
turtle_reader is free software: you can redistribute it and/or modify it
|
31
|
+
under the terms of the GNU Affero General Public License as published by
|
32
|
+
the Free Software Foundation, either version 3 of the License, or (at your
|
33
|
+
option) any later version.
|
34
|
+
|
35
|
+
turtle_reader is distributed in the hope that it will be useful, but
|
36
|
+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
37
|
+
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
38
|
+
License for more details.
|
39
|
+
|
40
|
+
You should have received a copy of the GNU Affero General Public License
|
41
|
+
along with turtle_reader. If not, see <http://www.gnu.org/licenses/>.
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.expand_path(%q{../lib/turtle_reader/version}, __FILE__)
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'hen'
|
5
|
+
|
6
|
+
Hen.lay! {{
|
7
|
+
gem: {
|
8
|
+
name: %q{turtle_reader},
|
9
|
+
version: TurtleReader::VERSION,
|
10
|
+
summary: %q{RDF Turtle reader.},
|
11
|
+
description: %q{A convenience wrapper for reading RDF Turtle data.},
|
12
|
+
author: %q{Jens Wille},
|
13
|
+
email: %q{jens.wille@gmail.com},
|
14
|
+
license: %q{AGPL-3.0},
|
15
|
+
homepage: :blackwinter,
|
16
|
+
dependencies: %w[rdf-turtle],
|
17
|
+
|
18
|
+
development_dependencies: %w[rbzip2],
|
19
|
+
|
20
|
+
required_ruby_version: '>= 1.9.3'
|
21
|
+
}
|
22
|
+
}}
|
23
|
+
rescue LoadError => err
|
24
|
+
warn "Please install the `hen' gem. (#{err})"
|
25
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#--
|
4
|
+
###############################################################################
|
5
|
+
# #
|
6
|
+
# turtle_reader -- RDF Turtle reader #
|
7
|
+
# #
|
8
|
+
# Copyright (C) 2014 Jens Wille #
|
9
|
+
# #
|
10
|
+
# Authors: #
|
11
|
+
# Jens Wille <jens.wille@gmail.com> #
|
12
|
+
# #
|
13
|
+
# turtle_reader is free software; you can redistribute it and/or modify it #
|
14
|
+
# under the terms of the GNU Affero General Public License as published by #
|
15
|
+
# the Free Software Foundation; either version 3 of the License, or (at your #
|
16
|
+
# option) any later version. #
|
17
|
+
# #
|
18
|
+
# turtle_reader is distributed in the hope that it will be useful, but #
|
19
|
+
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
|
20
|
+
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public #
|
21
|
+
# License for more details. #
|
22
|
+
# #
|
23
|
+
# You should have received a copy of the GNU Affero General Public License #
|
24
|
+
# along with turtle_reader. If not, see <http://www.gnu.org/licenses/>. #
|
25
|
+
# #
|
26
|
+
###############################################################################
|
27
|
+
#++
|
28
|
+
|
29
|
+
require 'rdf/util'
|
30
|
+
|
31
|
+
begin
|
32
|
+
require 'zlib'
|
33
|
+
rescue LoadError => err
|
34
|
+
warn err if $VERBOSE
|
35
|
+
end
|
36
|
+
|
37
|
+
begin
|
38
|
+
require 'rbzip2'
|
39
|
+
|
40
|
+
class RBzip2::Decompressor
|
41
|
+
|
42
|
+
def eof?
|
43
|
+
@current_state == EOF
|
44
|
+
end
|
45
|
+
|
46
|
+
def gets(sep = $/)
|
47
|
+
r = ''
|
48
|
+
|
49
|
+
loop {
|
50
|
+
b = read0
|
51
|
+
break if b < 0
|
52
|
+
|
53
|
+
count(1)
|
54
|
+
r << b
|
55
|
+
|
56
|
+
break if r.end_with?(sep)
|
57
|
+
}
|
58
|
+
|
59
|
+
r
|
60
|
+
end
|
61
|
+
|
62
|
+
def rewind
|
63
|
+
@io.rewind
|
64
|
+
initialize(@io)
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
rescue LoadError => err
|
69
|
+
warn err if $VERBOSE
|
70
|
+
end
|
71
|
+
|
72
|
+
class << RDF::Util::File
|
73
|
+
|
74
|
+
alias_method :_turtle_reader_original_open_file, :open_file
|
75
|
+
|
76
|
+
def open_file(filename_or_url, options = {}, &block)
|
77
|
+
klass = begin
|
78
|
+
case filename_or_url
|
79
|
+
when /\.bz(?:ip)?2?\z/i then RBzip2::Decompressor
|
80
|
+
when /\.gz(?:ip)?\z/i then Zlib::GzipReader
|
81
|
+
end
|
82
|
+
rescue NameError => err
|
83
|
+
err.message.sub!('Module::', '')
|
84
|
+
raise
|
85
|
+
end
|
86
|
+
|
87
|
+
if klass
|
88
|
+
original_block, block = block, lambda { |file|
|
89
|
+
original_block[file.is_a?(IO) ? klass.new(file) : file]
|
90
|
+
}
|
91
|
+
end
|
92
|
+
|
93
|
+
_turtle_reader_original_open_file(filename_or_url, options, &block)
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#--
|
4
|
+
###############################################################################
|
5
|
+
# #
|
6
|
+
# turtle_reader -- RDF Turtle reader #
|
7
|
+
# #
|
8
|
+
# Copyright (C) 2014 Jens Wille #
|
9
|
+
# #
|
10
|
+
# Authors: #
|
11
|
+
# Jens Wille <jens.wille@gmail.com> #
|
12
|
+
# #
|
13
|
+
# turtle_reader is free software; you can redistribute it and/or modify it #
|
14
|
+
# under the terms of the GNU Affero General Public License as published by #
|
15
|
+
# the Free Software Foundation; either version 3 of the License, or (at your #
|
16
|
+
# option) any later version. #
|
17
|
+
# #
|
18
|
+
# turtle_reader is distributed in the hope that it will be useful, but #
|
19
|
+
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
|
20
|
+
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public #
|
21
|
+
# License for more details. #
|
22
|
+
# #
|
23
|
+
# You should have received a copy of the GNU Affero General Public License #
|
24
|
+
# along with turtle_reader. If not, see <http://www.gnu.org/licenses/>. #
|
25
|
+
# #
|
26
|
+
###############################################################################
|
27
|
+
#++
|
28
|
+
|
29
|
+
require 'rdf'
|
30
|
+
|
31
|
+
def RDF.__prefix__; :rdf; end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#--
|
4
|
+
###############################################################################
|
5
|
+
# #
|
6
|
+
# turtle_reader -- RDF Turtle reader #
|
7
|
+
# #
|
8
|
+
# Copyright (C) 2014 Jens Wille #
|
9
|
+
# #
|
10
|
+
# Authors: #
|
11
|
+
# Jens Wille <jens.wille@gmail.com> #
|
12
|
+
# #
|
13
|
+
# turtle_reader is free software; you can redistribute it and/or modify it #
|
14
|
+
# under the terms of the GNU Affero General Public License as published by #
|
15
|
+
# the Free Software Foundation; either version 3 of the License, or (at your #
|
16
|
+
# option) any later version. #
|
17
|
+
# #
|
18
|
+
# turtle_reader is distributed in the hope that it will be useful, but #
|
19
|
+
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
|
20
|
+
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public #
|
21
|
+
# License for more details. #
|
22
|
+
# #
|
23
|
+
# You should have received a copy of the GNU Affero General Public License #
|
24
|
+
# along with turtle_reader. If not, see <http://www.gnu.org/licenses/>. #
|
25
|
+
# #
|
26
|
+
###############################################################################
|
27
|
+
#++
|
28
|
+
|
29
|
+
require 'rdf/turtle'
|
30
|
+
|
31
|
+
module RDF
|
32
|
+
class Turtle::Reader
|
33
|
+
|
34
|
+
PARSE_OPTIONS = {
|
35
|
+
branch: BRANCH,
|
36
|
+
first: FIRST,
|
37
|
+
follow: FOLLOW,
|
38
|
+
reset_on_start: true,
|
39
|
+
|
40
|
+
# NoMethodError: undefined method `[]' for nil:NilClass (line 17) [GND-sample.ttl]
|
41
|
+
# from .../lib/ruby/gems/shared/gems/rdf-turtle-1.1.3.1/lib/rdf/turtle/reader.rb:153
|
42
|
+
progress: RUBY_PLATFORM == 'java'
|
43
|
+
}
|
44
|
+
|
45
|
+
def closed?
|
46
|
+
@input.closed?
|
47
|
+
end
|
48
|
+
|
49
|
+
def parse_prologue
|
50
|
+
parse_internal { break }
|
51
|
+
rewind
|
52
|
+
[base_uri, prefixes]
|
53
|
+
end
|
54
|
+
|
55
|
+
def parse_statements
|
56
|
+
parse_internal { |context, _, *data|
|
57
|
+
if context == :statement
|
58
|
+
data[3] = { context: data[3] }
|
59
|
+
yield Statement.new(*data)
|
60
|
+
end
|
61
|
+
}
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def parse_internal(&block)
|
67
|
+
parse(@input, START, @options.merge(PARSE_OPTIONS), &block)
|
68
|
+
rescue => err
|
69
|
+
err.message << " (line #{lineno})"
|
70
|
+
raise
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#--
|
4
|
+
###############################################################################
|
5
|
+
# #
|
6
|
+
# turtle_reader -- RDF Turtle reader #
|
7
|
+
# #
|
8
|
+
# Copyright (C) 2014 Jens Wille #
|
9
|
+
# #
|
10
|
+
# Authors: #
|
11
|
+
# Jens Wille <jens.wille@gmail.com> #
|
12
|
+
# #
|
13
|
+
# turtle_reader is free software; you can redistribute it and/or modify it #
|
14
|
+
# under the terms of the GNU Affero General Public License as published by #
|
15
|
+
# the Free Software Foundation; either version 3 of the License, or (at your #
|
16
|
+
# option) any later version. #
|
17
|
+
# #
|
18
|
+
# turtle_reader is distributed in the hope that it will be useful, but #
|
19
|
+
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
|
20
|
+
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public #
|
21
|
+
# License for more details. #
|
22
|
+
# #
|
23
|
+
# You should have received a copy of the GNU Affero General Public License #
|
24
|
+
# along with turtle_reader. If not, see <http://www.gnu.org/licenses/>. #
|
25
|
+
# #
|
26
|
+
###############################################################################
|
27
|
+
#++
|
28
|
+
|
29
|
+
require 'rdf'
|
30
|
+
|
31
|
+
class RDF::URI
|
32
|
+
|
33
|
+
def basename
|
34
|
+
File.basename(path).sub(/;[^\/]*$/, '')
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class TurtleReader
|
2
|
+
|
3
|
+
module Version
|
4
|
+
|
5
|
+
MAJOR = 0
|
6
|
+
MINOR = 0
|
7
|
+
TINY = 1
|
8
|
+
|
9
|
+
class << self
|
10
|
+
|
11
|
+
# Returns array representation.
|
12
|
+
def to_a
|
13
|
+
[MAJOR, MINOR, TINY]
|
14
|
+
end
|
15
|
+
|
16
|
+
# Short-cut for version string.
|
17
|
+
def to_s
|
18
|
+
to_a.join('.')
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
VERSION = Version.to_s
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
#--
|
4
|
+
###############################################################################
|
5
|
+
# #
|
6
|
+
# turtle_reader -- RDF Turtle reader #
|
7
|
+
# #
|
8
|
+
# Copyright (C) 2014 Jens Wille #
|
9
|
+
# #
|
10
|
+
# Authors: #
|
11
|
+
# Jens Wille <jens.wille@gmail.com> #
|
12
|
+
# #
|
13
|
+
# turtle_reader is free software; you can redistribute it and/or modify it #
|
14
|
+
# under the terms of the GNU Affero General Public License as published by #
|
15
|
+
# the Free Software Foundation; either version 3 of the License, or (at your #
|
16
|
+
# option) any later version. #
|
17
|
+
# #
|
18
|
+
# turtle_reader is distributed in the hope that it will be useful, but #
|
19
|
+
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
|
20
|
+
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public #
|
21
|
+
# License for more details. #
|
22
|
+
# #
|
23
|
+
# You should have received a copy of the GNU Affero General Public License #
|
24
|
+
# along with turtle_reader. If not, see <http://www.gnu.org/licenses/>. #
|
25
|
+
# #
|
26
|
+
###############################################################################
|
27
|
+
#++
|
28
|
+
|
29
|
+
require_relative 'turtle_reader/rdf/turtle/reader'
|
30
|
+
require_relative 'turtle_reader/rdf/compression'
|
31
|
+
require_relative 'turtle_reader/rdf/prefix'
|
32
|
+
require_relative 'turtle_reader/rdf/uri'
|
33
|
+
|
34
|
+
class TurtleReader
|
35
|
+
|
36
|
+
include Enumerable
|
37
|
+
|
38
|
+
NS = RDF::Vocabulary.inject({}) { |h, v| h[v.__prefix__] = v; h }
|
39
|
+
|
40
|
+
class << self
|
41
|
+
|
42
|
+
def open(file, *args)
|
43
|
+
RDF::Reader.open(file, format: :ttl) { |reader|
|
44
|
+
turtle = new(reader, *args)
|
45
|
+
turtle.file = File.expand_path(file)
|
46
|
+
|
47
|
+
return block_given? ? yield(turtle) : turtle
|
48
|
+
}
|
49
|
+
end
|
50
|
+
|
51
|
+
def foreach(file, *args, &block)
|
52
|
+
open(file, *args) { |turtle| turtle.each(&block) }
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
def initialize(reader, map = true)
|
58
|
+
@reader, @base, @prefixes = reader, *reader.parse_prologue
|
59
|
+
self.map = map
|
60
|
+
end
|
61
|
+
|
62
|
+
attr_reader :reader, :map, :base, :prefixes
|
63
|
+
|
64
|
+
attr_accessor :file
|
65
|
+
|
66
|
+
def map=(map)
|
67
|
+
unless map.is_a?(Hash)
|
68
|
+
@map = Hash.new(map)
|
69
|
+
else
|
70
|
+
@map = {}
|
71
|
+
|
72
|
+
map.each { |k, v|
|
73
|
+
if k.is_a?(String)
|
74
|
+
n, s = k.split(':', 2)
|
75
|
+
k = NS.key?(n = n.to_sym) ? NS[n][s] : prefixes[n] / s
|
76
|
+
end
|
77
|
+
|
78
|
+
@map[k] = v
|
79
|
+
}
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def statements
|
84
|
+
each_statement.to_a
|
85
|
+
end
|
86
|
+
|
87
|
+
def each_statement(&block)
|
88
|
+
return enum_for(:each_statement) unless block_given?
|
89
|
+
reader.parse_statements(&block)
|
90
|
+
self
|
91
|
+
end
|
92
|
+
|
93
|
+
def each
|
94
|
+
return enum_for(:each) unless block_given?
|
95
|
+
|
96
|
+
uri, map, base = RDF::URI, self.map, self.base
|
97
|
+
|
98
|
+
each_statement { |t|
|
99
|
+
s, p, o = *t
|
100
|
+
|
101
|
+
if s.is_a?(uri) and k = map[p] and s.start_with?(base)
|
102
|
+
yield s, o, k
|
103
|
+
end
|
104
|
+
}
|
105
|
+
end
|
106
|
+
|
107
|
+
def closed?
|
108
|
+
reader.closed?
|
109
|
+
end
|
110
|
+
|
111
|
+
def inspect
|
112
|
+
'#<%s:0x%x @file=%p, @base=%p%s>' % [
|
113
|
+
self.class, object_id, file, base, closed? ? ' (closed)' : ''
|
114
|
+
]
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
require_relative 'turtle_reader/version'
|