Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Better NQuads format detection.

  • Loading branch information...
commit 90381a7efa93d932f4f57e11f1d9cfed743655ff 1 parent fedf815
@gkellogg gkellogg authored
Showing with 22 additions and 8 deletions.
  1. +17 −2 lib/rdf/nquads.rb
  2. +5 −6 spec/nquads_spec.rb
View
19 lib/rdf/nquads.rb
@@ -27,9 +27,24 @@ class Format < RDF::Format
##
# Sample detection to see if it matches N-Quads (or N-Triples)
+ #
+ # Use a text sample to detect the format of an input file. Sub-classes implement
+ # a matcher sufficient to detect probably format matches, including disambiguating
+ # between other similar formats.
+ #
+ # @param [String] sample Beginning several bytes (~ 1K) of input.
+ # @result [Boolean]
def self.detect(sample)
- sample.match(%r(^\s*<[^>]*>.*\.\s*$)) &&
- !sample.match(%r(@(base|prefix))) # Not Turtle/N3
+ !!sample.match(%r(
+ (?:\s*(?:<[^>]*>) | (?:_:\w+)) # Subject
+ (?:\s*<[^>]*>) # Predicate
+ \s*
+ (?:(?:<[^>]*>) | (?:_:\w+) | (?:"[^"]*"(?:^^|@\S+)?)) # Object
+ (?:\s*<[^>]*>)? # Optional context
+ \s*\.
+ )mx) && (
+ !sample.match(%r(@(base|prefix|keywords))) # Not Turtle/N3
+ )
end
end
View
11 spec/nquads_spec.rb
@@ -30,7 +30,7 @@
:ntriples => "<a> <b> <c> .",
:nquads => "<a> <b> <c> <d> . ",
:literal => '<a> <b> "literal" .',
- :multi_line => '<a>\n <b>\n "literal"\n .',
+ :multi_line => %(<a>\n <b>\n "literal"\n .),
}.each do |sym, str|
it "detects #{sym}" do
@format_class.for {str}.should == @format_class
@@ -47,7 +47,7 @@
:ntriples => "<a> <b> <c> .",
:nquads => "<a> <b> <c> <d> . ",
:literal => '<a> <b> "literal" .',
- :multi_line => '<a>\n <b>\n "literal"\n .',
+ :multi_line => %(<a>\n <b>\n "literal"\n .),
}.each do |sym, str|
it "detects #{sym}" do
@format_class.detect(str).should be_true
@@ -58,16 +58,15 @@
:turtle => "@prefix foo: <bar> .\n foo:a foo:b <c> .",
:rdfxml => '<rdf:RDF about="foo"></rdf:RDF>',
:n3 => '@prefix foo: <bar> .\nfoo:bar = {<a> <b> <c>} .',
+ :jsonld => '{"@context" => "foo"}',
+ :rdfa => '<div about="foo"></div>',
+ :microdata => '<div itemref="bar"></div>',
}.each do |sym, str|
it "does not detect #{sym}" do
@format_class.detect(str).should be_false
end
end
end
-
- it "should return :nquads for to_sym" do
- RDF::NQuads::Format.to_sym.should == :nquads
- end
end
describe RDF::NQuads::Reader do
Please sign in to comment.
Something went wrong with that request. Please try again.