diff --git a/Rakefile b/Rakefile index 6f93ce5..42a2ab0 100644 --- a/Rakefile +++ b/Rakefile @@ -40,7 +40,7 @@ namespace :treat do task :spec, [:language] do |t, args| require_relative 'spec/helper' Treat::Specs::Helper.start_coverage - Treat::Specs::Helper.run_core_specs + Treat::Specs::Helper.run_library_specs Treat::Specs::Helper.run_language_specs(args.language) end diff --git a/spec/entities/collection.rb b/spec/entities/collection.rb index 98e7073..2b75b63 100644 --- a/spec/entities/collection.rb +++ b/spec/entities/collection.rb @@ -1,40 +1,42 @@ -describe Treat::Entities::Collection do +module Treat::Specs::Entities + + describe Treat::Entities::Collection do - before :all do - @file = Treat.paths.spec + - 'workers/examples/english/mathematicians' - end - - - describe "Buildable" do - - describe "#build" do - - context "when supplied with an existing folder name" do - - it "recursively searches the folder for " + - "files and opens them into a collection of documents" do - collection = Treat::Entities::Collection.build(@file) - collection.size.should eql 6 - end - - end - - context "when supplied a folder name that doesn't exist" do - - it "creates the directory and opens the collection" do - f = Treat.paths.spec + 'workers/examples/english/test' - c = Treat::Entities::Collection.build(f) - FileTest.directory?(f).should eql true - c.should be_an_instance_of Treat::Entities::Collection - FileUtils.rm_rf(f) - end - end + before :all do + @file = Treat.paths.spec + + 'workers/examples/english/mathematicians' end - end - describe "#<<" do + describe "Buildable" do + + describe "#build" do + + context "when supplied with an existing folder name" do + + it "recursively searches the folder for " + + "files and opens them into a collection of documents" do + collection = Treat::Entities::Collection.build(@file) + collection.size.should eql 6 + end + + end + + context "when supplied a folder name that doesn't exist" do + + it "creates the directory and opens the collection" do + f = Treat.paths.spec + 'workers/examples/english/test' + c = Treat::Entities::Collection.build(f) + FileTest.directory?(f).should eql true + c.should be_an_instance_of Treat::Entities::Collection + FileUtils.rm_rf(f) + end + end + end + + end + + describe "#<<" do it "adds the object to the collection" do f = Treat.paths.spec + 'workers/examples/english/economist' @@ -43,6 +45,8 @@ describe Treat::Entities::Collection do c.size.should eql 4 end + end + end end @@ -105,4 +109,4 @@ end end end -=end \ No newline at end of file +=end diff --git a/spec/entities/document.rb b/spec/entities/document.rb index 296058a..f8c34e0 100644 --- a/spec/entities/document.rb +++ b/spec/entities/document.rb @@ -1,55 +1,56 @@ -describe Treat::Entities::Document do +module Treat::Specs::Entities + describe Treat::Entities::Document do - describe "Buildable" do + describe "Buildable" do - describe "#build" do + describe "#build" do - context "when supplied with a readable file name" do - it "opens the file and reads its " + - "content into a document" do - f = Treat.paths.spec + - 'workers/examples/english/mathematicians/leibniz.txt' - d = Treat::Entities::Document.build(f) - d.should be_an_instance_of Treat::Entities::Document - d.to_s.index('Gottfried Leibniz').should_not eql nil + context "when supplied with a readable file name" do + it "opens the file and reads its " + + "content into a document" do + f = Treat.paths.spec + + 'workers/examples/english/mathematicians/leibniz.txt' + d = Treat::Entities::Document.build(f) + d.should be_an_instance_of Treat::Entities::Document + d.to_s.index('Gottfried Leibniz').should_not eql nil + end end - end - context "when supplied with a url" do - it "downloads the file the URL points to and opens " + - "a document with the contents of the file" do - url = 'http://www.rubyinside.com/nethttp-cheat-sheet-2940.html' - d = Treat::Entities::Document.build(url) - d.format.should eql 'html' - d.should be_an_instance_of Treat::Entities::Document - d.to_s.index('Rubyist').should_not eql nil + context "when supplied with a url" do + it "downloads the file the URL points to and opens " + + "a document with the contents of the file" do + url = 'http://www.rubyinside.com/nethttp-cheat-sheet-2940.html' + d = Treat::Entities::Document.build(url) + d.format.should eql 'html' + d.should be_an_instance_of Treat::Entities::Document + d.to_s.index('Rubyist').should_not eql nil + end end - end - context "when supplied with a url with no file extension" do - it "downloads the file the URL points to and opens " + - "a document with the contents of the file, assuming " + - "the downloaded file to be in HTML format" do - url = 'http://www.economist.com/node/21552208' - d = Treat::Entities::Document.build(url) - d.should be_an_instance_of Treat::Entities::Document - d.to_s.index('Ronnie Lupe').should_not eql nil + context "when supplied with a url with no file extension" do + it "downloads the file the URL points to and opens " + + "a document with the contents of the file, assuming " + + "the downloaded file to be in HTML format" do + url = 'http://www.economist.com/node/21552208' + d = Treat::Entities::Document.build(url) + d.should be_an_instance_of Treat::Entities::Document + d.to_s.index('Ronnie Lupe').should_not eql nil + end end - end - - context "when called with anything else than a " + - "readable file name or url" do - it "raises an exception" do - lambda do - Treat::Entities::Document.build('nonexistent') - end.should raise_error + context "when called with anything else than a " + + "readable file name or url" do + + it "raises an exception" do + lambda do + Treat::Entities::Document.build('nonexistent') + end.should raise_error + end + end end end - end - -end \ No newline at end of file +end diff --git a/spec/entities/entity.rb b/spec/entities/entity.rb index cb8c926..22174d0 100644 --- a/spec/entities/entity.rb +++ b/spec/entities/entity.rb @@ -1,70 +1,71 @@ -describe Treat::Entities::Entity do +module Treat::Specs::Entities + describe Treat::Entities::Entity do - before do + before do - @paragraph = Treat::Entities::Paragraph.new - @sentence = Treat::Entities::Sentence.new - @noun_phrase = Treat::Entities::Phrase.new - @noun_phrase.set :tag, 'NP' - @verb_phrase = Treat::Entities::Phrase.new - @verb_phrase.set :tag, 'VP' - @adj_phrase = Treat::Entities::Phrase.new - @adj_phrase.set :tag, 'ADJP' - @det = Treat::Entities::Word.new('The') - @det.set :category, 'determiner' - @det.set :tag, 'DT' - @adj = Treat::Entities::Word.new('lazy') - @adj.set :category, 'adjective' - @adj.set :tag, 'JJ' - @noun = Treat::Entities::Word.new('fox') - @noun.set :category, 'noun' - @noun.set :tag, 'NN' - @aux = Treat::Entities::Word.new('is') - @aux.set :category, 'verb' - @aux.set :tag, 'VBZ' - @verb = Treat::Entities::Word.new('running') - @verb.set :category, 'verb' - @verb.set :tag, 'VBG' - @dot = Treat::Entities::Punctuation.new('.') - @dot.set :tag, '.' - @paragraph << @sentence << [@noun_phrase, @verb_phrase, @dot] - @noun_phrase << [@det, @adj_phrase, @noun] - @adj_phrase << @adj - @verb_phrase << [@aux, @verb] + @paragraph = Treat::Entities::Paragraph.new + @sentence = Treat::Entities::Sentence.new + @noun_phrase = Treat::Entities::Phrase.new + @noun_phrase.set :tag, 'NP' + @verb_phrase = Treat::Entities::Phrase.new + @verb_phrase.set :tag, 'VP' + @adj_phrase = Treat::Entities::Phrase.new + @adj_phrase.set :tag, 'ADJP' + @det = Treat::Entities::Word.new('The') + @det.set :category, 'determiner' + @det.set :tag, 'DT' + @adj = Treat::Entities::Word.new('lazy') + @adj.set :category, 'adjective' + @adj.set :tag, 'JJ' + @noun = Treat::Entities::Word.new('fox') + @noun.set :category, 'noun' + @noun.set :tag, 'NN' + @aux = Treat::Entities::Word.new('is') + @aux.set :category, 'verb' + @aux.set :tag, 'VBZ' + @verb = Treat::Entities::Word.new('running') + @verb.set :category, 'verb' + @verb.set :tag, 'VBG' + @dot = Treat::Entities::Punctuation.new('.') + @dot.set :tag, '.' + @paragraph << @sentence << [@noun_phrase, @verb_phrase, @dot] + @noun_phrase << [@det, @adj_phrase, @noun] + @adj_phrase << @adj + @verb_phrase << [@aux, @verb] - end + end - describe "Checkable" do + describe "Checkable" do - describe "#check_has(feature, do_it = true) " do - - it "checks if an entity has the feature; if not, " + - "calls the default worker to get the feature if do_it " + - "is set to true; if the entity doesn't have the feature " + - " and do_it is set to false, it raises an exception." do - - # NOT PASSING! Dependence on caller method. + describe "#check_has(feature, do_it = true) " do + + it "checks if an entity has the feature; if not, " + + "calls the default worker to get the feature if do_it " + + "is set to true; if the entity doesn't have the feature " + + " and do_it is set to false, it raises an exception." do + + # NOT PASSING! Dependence on caller method. + + # lambda { '$'.to_entity.check_has(:tag, false) }.should raise_error Treat::Exception + + end - # lambda { '$'.to_entity.check_has(:tag, false) }.should raise_error Treat::Exception - end end - end + describe "Countable" do - describe "Countable" do + describe "#position" do - describe "#position" do + it "returns the position of the entity in its parent, sarting at 0" do + @noun_phrase.position.should eql 0 + @det.position.should eql 0 + end - it "returns the position of the entity in its parent, sarting at 0" do - @noun_phrase.position.should eql 0 - @det.position.should eql 0 end - end - =begin describe "#frequency" do @@ -88,334 +89,336 @@ describe Treat::Entities::Entity do =end - end - - describe "Delegatable" do - - describe "#self.call_worker" do - - it "finds the worker class to " + - "perform a task and delegates the task to it " do - - Treat::Entities::Entity.call_worker( - '$'.to_entity, :tag, :lingua, - Treat::Workers::Lexicalizers::Taggers, {}).should - eql '$'.tag(:lingua) - - end - end - end + describe "Delegatable" do - describe "Exportable" do + describe "#self.call_worker" do - context "when supplied with a classification to export" do - feature = Treat::Learning::Feature.new(:tag) - question = Treat::Learning::Question.new(:is_keyword, :word, false, :discrete) - problem = Treat::Learning::Problem.new(question, feature) - it "returns a data set with the exported features" do - ds = @sentence.export(problem) - ds.problem.should eql problem - # MORE TESTS HERE - FIXME - end - end + it "finds the worker class to " + + "perform a task and delegates the task to it " do - end + Treat::Entities::Entity.call_worker( + '$'.to_entity, :tag, :lingua, + Treat::Workers::Lexicalizers::Taggers, {}).should + eql '$'.tag(:lingua) - describe "Iterable" do - - describe "#each { |child| ... }" do - it "yields each direct child of a node" do - a = [] - @sentence.each do |child| - a << child end - a.should eql [@noun_phrase, @verb_phrase, @dot] + end + end - describe "#each_entity(*entity_types) { |entity| ... }" do - - context "when called with no arguments" do - it "recursively yields each element in " + - "the tree, including itself, top-down " + - "first then left to right" do - - a = [] - @sentence.each_entity do |e| - a << e - end - - a.should eql [@sentence, @noun_phrase, @det, - @adj_phrase, @adj, @noun, - @verb_phrase, @aux, @verb, @dot] + describe "Exportable" do + context "when supplied with a classification to export" do + feature = Treat::Learning::Feature.new(:tag) + question = Treat::Learning::Question.new(:is_keyword, :word, false, :discrete) + problem = Treat::Learning::Problem.new(question, feature) + it "returns a data set with the exported features" do + ds = @sentence.export(problem) + ds.problem.should eql problem + # MORE TESTS HERE - FIXME end end - context "when called with one or more entity " + - "types supplied as lowercase symbols" do - it "recursively yields all elements with the given type(s), "+ - "including the receiver if it matches on of the types" do + end + + describe "Iterable" do + + describe "#each { |child| ... }" do + it "yields each direct child of a node" do a = [] - @sentence.each_entity(:phrase, :punctuation) do |e| - a << e + @sentence.each do |child| + a << child end + a.should eql [@noun_phrase, @verb_phrase, @dot] + end + end + + describe "#each_entity(*entity_types) { |entity| ... }" do + + context "when called with no arguments" do + it "recursively yields each element in " + + "the tree, including itself, top-down " + + "first then left to right" do + + a = [] + @sentence.each_entity do |e| + a << e + end + + a.should eql [@sentence, @noun_phrase, @det, + @adj_phrase, @adj, @noun, + @verb_phrase, @aux, @verb, @dot] + + end + end + + context "when called with one or more entity " + + "types supplied as lowercase symbols" do + it "recursively yields all elements with the given type(s), "+ + "including the receiver if it matches on of the types" do + a = [] + @sentence.each_entity(:phrase, :punctuation) do |e| + a << e + end + a.should eql [@noun_phrase, + @adj_phrase, @verb_phrase, @dot] + end + end + + end + end + + describe "Magical" do + + describe "# - e.g. " + + "#title, #paragraph, etc. and #adjective, #noun, etc." do + + it "return the first entity with the corresponding " + + "type inside another entity, but raises an exception "+ + "the type occurs more than once in the entity" do + @paragraph.sentence.should eql @sentence + end + + end + + + describe "#s - e.g. " + + "#sections, #words, etc. and #nouns, #adverbs, etc." do + + it "return an array of the entities with the " + + "corresponding type in the subtree of an entity" do + @paragraph.phrases.should eql [@noun_phrase, @adj_phrase, @verb_phrase] + end + + end + + describe "#each_ - e.g. " + + "#each_sentence, #each_word, etc." do + + it "yields each of the entities with the " + + "corresponding type in the subtree of an entity" do + a = [] + + @paragraph.each_phrase { |p| a << p } a.should eql [@noun_phrase, - @adj_phrase, @verb_phrase, @dot] + @adj_phrase, @verb_phrase] + + end + + end + + describe "#_count - e.g. " + + "#sentence_count, #paragraph_count, etc. and " + + "#noun_count, #verb_count, etc." do + + it "return the number of entities with the " + + "corresponding type inside another entity" do + @paragraph.sentence_count.should eql 1 + @paragraph.phrase_count.should eql 3 + end + + end + + describe "#_with_(value) - " + + "e.g. #word_with_id(x) or #adverb_with_value('seemingly')" do + + it "return the entity with the corresponding type " + + "that have [feature] set to the supplied value; raise" + + "a warning if there are many entities of that type" do + @paragraph.word_with_value('The').should eql @det + @paragraph.token_with_tag('.').should eql @dot + @sentence.phrase_with_tag('NP').should eql @noun_phrase + end + + end + + describe "#s_with_(value) - " + + "e.g. #phrases_with_tag('NP'), #nouns_with_value('foo')" do + + it "return an array of the entities with the " + + "corresponding type that have [feature] set to "+ + "the supplied value" do + @paragraph.words_with_value('The').should eql [@det] + @paragraph.tokens_with_tag('.').should eql [@dot] + @sentence.phrases_with_tag('NP').should eql [@noun_phrase] + end + + end + + describe "#parent_ - e.g. " + + "#parent_document, #parent_collection, etc." do + + it "return the first ancestor of the entity " + + "that has the supplied type, or nil if none" do + @sentence.parent_paragraph.should eql @paragraph + @adj.parent_sentence.should eql @sentence + end + + end + + describe "#frequency_in_ - e.g. " + + "#frequency_in_collection, #frequency_in_document, etc." do + + it "return the frequency of this entity's value " + + "in the parent entity with the corresponding type" do + @adj.frequency_in_sentence.should eql 1 + end + + end + + end + + describe "Stringable" do + + describe "#to_string" do + it "returns the true text value of the entity " + + "or an empty string if it has none" do + @paragraph.to_string.should eql '' + @noun.to_string.should eql 'fox' + end + end + + describe "#to_s" do + it "returns the string value of the " + + "entity or its full subtree" do + @paragraph.to_s.should + eql 'The lazy fox is running.' + @noun.to_s.should eql 'fox' + end + end + + describe "#inspect" do + it "returns an informative string " + + "concerning the entity" do + @paragraph.inspect.should + be_an_instance_of String + end + end + + describe "#short_value" do + it "returns a shortened version of the " + + "entity's string value" do + @paragraph.short_value.should + eql 'The lazy fox is running.' end end end - end - describe "Magical" do + describe "Formatters" do - describe "# - e.g. " + - "#title, #paragraph, etc. and #adjective, #noun, etc." do - it "return the first entity with the corresponding " + - "type inside another entity, but raises an exception "+ - "the type occurs more than once in the entity" do - @paragraph.sentence.should eql @sentence + before do + @serializers = Treat.languages.agnostic. + workers.formatters.serializers + @txt = "The story of the fox. The quick brown fox jumped over the lazy dog." end - end + describe "#serialize" do + context "when called with a file to save to" do - describe "#s - e.g. " + - "#sections, #words, etc. and #nouns, #adverbs, etc." do + it "serializes a document to the supplied format" do - it "return an array of the entities with the " + - "corresponding type in the subtree of an entity" do - @paragraph.phrases.should eql [@noun_phrase, @adj_phrase, @verb_phrase] - end + @serializers.each do |ser| + next if ser == :mongo # Fix this! + f = Treat.paths.spec + 'test.' + ser.to_s + s = Treat::Entities::Paragraph.new(@txt) + s.apply(:segment, :tokenize) + s.serialize(ser, :file => f) + File.delete(f) + end - end - - describe "#each_ - e.g. " + - "#each_sentence, #each_word, etc." do - - it "yields each of the entities with the " + - "corresponding type in the subtree of an entity" do - a = [] - - @paragraph.each_phrase { |p| a << p } - a.should eql [@noun_phrase, - @adj_phrase, @verb_phrase] - - end - - end - - describe "#_count - e.g. " + - "#sentence_count, #paragraph_count, etc. and " + - "#noun_count, #verb_count, etc." do - - it "return the number of entities with the " + - "corresponding type inside another entity" do - @paragraph.sentence_count.should eql 1 - @paragraph.phrase_count.should eql 3 - end - - end - - describe "#_with_(value) - " + - "e.g. #word_with_id(x) or #adverb_with_value('seemingly')" do - - it "return the entity with the corresponding type " + - "that have [feature] set to the supplied value; raise" + - "a warning if there are many entities of that type" do - @paragraph.word_with_value('The').should eql @det - @paragraph.token_with_tag('.').should eql @dot - @sentence.phrase_with_tag('NP').should eql @noun_phrase - end - - end - - describe "#s_with_(value) - " + - "e.g. #phrases_with_tag('NP'), #nouns_with_value('foo')" do - - it "return an array of the entities with the " + - "corresponding type that have [feature] set to "+ - "the supplied value" do - @paragraph.words_with_value('The').should eql [@det] - @paragraph.tokens_with_tag('.').should eql [@dot] - @sentence.phrases_with_tag('NP').should eql [@noun_phrase] - end - - end - - describe "#parent_ - e.g. " + - "#parent_document, #parent_collection, etc." do - - it "return the first ancestor of the entity " + - "that has the supplied type, or nil if none" do - @sentence.parent_paragraph.should eql @paragraph - @adj.parent_sentence.should eql @sentence - end - - end - - describe "#frequency_in_ - e.g. " + - "#frequency_in_collection, #frequency_in_document, etc." do - - it "return the frequency of this entity's value " + - "in the parent entity with the corresponding type" do - @adj.frequency_in_sentence.should eql 1 - end - - end - - end - - describe "Stringable" do - - describe "#to_string" do - it "returns the true text value of the entity " + - "or an empty string if it has none" do - @paragraph.to_string.should eql '' - @noun.to_string.should eql 'fox' - end - end - - describe "#to_s" do - it "returns the string value of the " + - "entity or its full subtree" do - @paragraph.to_s.should - eql 'The lazy fox is running.' - @noun.to_s.should eql 'fox' - end - end - - describe "#inspect" do - it "returns an informative string " + - "concerning the entity" do - @paragraph.inspect.should - be_an_instance_of String - end - end - - describe "#short_value" do - it "returns a shortened version of the " + - "entity's string value" do - @paragraph.short_value.should - eql 'The lazy fox is running.' - end - end - - end - - describe "Formatters" do - - - before do - @serializers = Treat.languages.agnostic. - workers.formatters.serializers - @txt = "The story of the fox. The quick brown fox jumped over the lazy dog." - end - - describe "#serialize" do - - context "when called with a file to save to" do - - it "serializes a document to the supplied format" do - - @serializers.each do |ser| - next if ser == :mongo # Fix this! - f = Treat.paths.spec + 'test.' + ser.to_s - s = Treat::Entities::Paragraph.new(@txt) - s.do(:segment, :tokenize) - s.serialize(ser, :file => f) - File.delete(f) end - + end - + end - - end - - describe "#unserialize" do - - context "when called with a serialized file" do - - it "reconstitutes the original entity" do - @serializers.each do |ser| - next if ser == :mongo # Fix this! - - f = Treat.paths.spec + 'test.' + ser.to_s - s = Treat::Entities::Paragraph.new(@txt) - - s.set :test_int, 9 - s.set :test_float, 9.9 - s.set :test_string, 'hello' - s.set :test_sym, :hello - s.set :test_bool, false - - s.do(:segment, :tokenize) - - s.serialize(ser, file: f) - - d = Treat::Entities::Document.build(f) - d.test_int.should eql 9 - d.test_float.should eql 9.9 - d.test_string.should eql 'hello' - d.test_sym.should eql :hello - d.test_bool.should eql false + describe "#unserialize" do + + context "when called with a serialized file" do + + it "reconstitutes the original entity" do + @serializers.each do |ser| + next if ser == :mongo # Fix this! + + f = Treat.paths.spec + 'test.' + ser.to_s + s = Treat::Entities::Paragraph.new(@txt) + + s.set :test_int, 9 + s.set :test_float, 9.9 + s.set :test_string, 'hello' + s.set :test_sym, :hello + s.set :test_bool, false + + s.apply(:segment, :tokenize) + + s.serialize(ser, file: f) + + d = Treat::Entities::Document.build(f) + + d.test_int.should eql 9 + d.test_float.should eql 9.9 + d.test_string.should eql 'hello' + d.test_sym.should eql :hello + d.test_bool.should eql false + + d.to_s.should eql "The story of the fox." + + " The quick brown fox jumped over the lazy dog." + d.size.should eql s.size + + d.token_count.should eql s.token_count + d.tokens[0].id.should eql s.tokens[0].id + + File.delete(f) + + end - d.to_s.should eql "The story of the fox." + - " The quick brown fox jumped over the lazy dog." - d.size.should eql s.size - - d.token_count.should eql s.token_count - d.tokens[0].id.should eql s.tokens[0].id - - File.delete(f) - end - + end - + end - + end - - end - describe "Extractors" do + describe "Extractors" do - describe "#language" do - context "when language detection is disabled " + - "(Treat.core.detect is set to false)" do - it "returns the default language (Treat.core.language.default)" do - Treat.core.language.detect = false - Treat.core.language.default = :test - s = 'Les grands hommes ne sont pas toujours grands, dit un jour Napoleon.' - s.language.should eql :test - Treat.core.language.default = :english + describe "#language" do + context "when language detection is disabled " + + "(Treat.core.detect is set to false)" do + it "returns the default language (Treat.core.language.default)" do + Treat.core.language.detect = false + Treat.core.language.default = :test + s = 'Les grands hommes ne sont pas toujours grands, dit un jour Napoleon.' + s.language.should eql :test + Treat.core.language.default = :english + end end - end - context "when language detection is enabled " + - "(Treat.detect_language is set to true)" do + context "when language detection is enabled " + + "(Treat.detect_language is set to true)" do - it "guesses the language of the entity" do + it "guesses the language of the entity" do - Treat.core.language.detect = true - a = 'I want to know God\'s thoughts; the rest are details. - Albert Einstein' - b = 'El mundo de hoy no tiene sentido, asi que por que deberia pintar cuadros que lo tuvieran? - Pablo Picasso' - c = 'Un bon Allemand ne peut souffrir les Francais, mais il boit volontiers les vins de France. - Goethe' - d = 'Wir haben die Kunst, damit wir nicht an der Wahrheit zugrunde gehen. - Friedrich Nietzsche' - a.language.should eql :english - #b.language.should eql :spanish - #c.language.should eql :french - #d.language.should eql :german + Treat.core.language.detect = true + a = 'I want to know God\'s thoughts; the rest are details. - Albert Einstein' + b = 'El mundo de hoy no tiene sentido, asi que por que deberia pintar cuadros que lo tuvieran? - Pablo Picasso' + c = 'Un bon Allemand ne peut souffrir les Francais, mais il boit volontiers les vins de France. - Goethe' + d = 'Wir haben die Kunst, damit wir nicht an der Wahrheit zugrunde gehen. - Friedrich Nietzsche' + a.language.should eql :english + #b.language.should eql :spanish + #c.language.should eql :french + #d.language.should eql :german + + # Reset default + Treat.core.language.detect = false + end - # Reset default - Treat.core.language.detect = false end end @@ -425,8 +428,6 @@ describe Treat::Entities::Entity do end end - - =begin diff --git a/spec/entities/phrase.rb b/spec/entities/phrase.rb index 75c7b92..60af85e 100644 --- a/spec/entities/phrase.rb +++ b/spec/entities/phrase.rb @@ -1,27 +1,31 @@ -describe Treat::Entities::Phrase do +module Treat::Specs::Entities - describe "Buildable" do + describe Treat::Entities::Phrase do - describe "#build" do + describe "Buildable" do - context "when supplied with a sentence" do + describe "#build" do + + context "when supplied with a sentence" do + + it "creates a sentence with the text" do + sentence = "This is a sentence." + s = Treat::Entities::Phrase.build(sentence) + s.type.should eql :sentence + s.to_s.should eql sentence + end - it "creates a sentence with the text" do - sentence = "This is a sentence." - s = Treat::Entities::Phrase.build(sentence) - s.type.should eql :sentence - s.to_s.should eql sentence end - end + context "when supplied with a phrase" do - context "when supplied with a phrase" do + it "creates a phrase with the text" do + phrase = "this is a phrase" + p = Treat::Entities::Phrase.build(phrase) + p.type.should eql :phrase + p.to_s.should eql phrase + end - it "creates a phrase with the text" do - phrase = "this is a phrase" - p = Treat::Entities::Phrase.build(phrase) - p.type.should eql :phrase - p.to_s.should eql phrase end end @@ -30,4 +34,4 @@ describe Treat::Entities::Phrase do end -end \ No newline at end of file +end diff --git a/spec/entities/token.rb b/spec/entities/token.rb index b969625..29b04e1 100644 --- a/spec/entities/token.rb +++ b/spec/entities/token.rb @@ -1,55 +1,58 @@ #encoding: utf-8 -describe Treat::Entities::Token do +module Treat::Specs::Entities - describe "Buildable" do + describe Treat::Entities::Token do - describe "#build" do + describe "Buildable" do - context "when supplied with a word" do - it "creates a word with the text" do - t = Treat::Entities::Token.build('word') - t.should be_an_instance_of Treat::Entities::Word - t.to_s.should eql 'word' + describe "#build" do + + context "when supplied with a word" do + it "creates a word with the text" do + t = Treat::Entities::Token.build('word') + t.should be_an_instance_of Treat::Entities::Word + t.to_s.should eql 'word' + end end + + context "when supplied with a number or a string representing a numerical quantity" do + it "creates a number" do + t = Treat::Entities::Token.build(2) + t2 = Treat::Entities::Token.build(2.2) + t3 = Treat::Entities::Token.build('2') + t4 = Treat::Entities::Token.build('2.2') + t.should be_an_instance_of Treat::Entities::Number + t2.should be_an_instance_of Treat::Entities::Number + t3.should be_an_instance_of Treat::Entities::Number + t4.should be_an_instance_of Treat::Entities::Number + t.to_i.should eql 2 + t2.to_i.should eql 2 + t3.to_i.should eql 2 + t4.to_i.should eql 2 + t.to_f.should eql 2.0 + t2.to_f.should eql 2.2 + t3.to_f.should eql 2.0 + t4.to_f.should eql 2.2 + end + end + + context "when supplied with a punctuation character" do + it "creates a punctuation with the text" do + t = Treat::Entities::Token.build('.') + t.should be_an_instance_of Treat::Entities::Punctuation + end + end + + context "when supplied with a symbol character" do + it "creates a symbol with the text" do + t = Treat::Entities::Token.build('¨') + t.should be_an_instance_of Treat::Entities::Symbol + end + end + end - context "when supplied with a number or a string representing a numerical quantity" do - it "creates a number" do - t = Treat::Entities::Token.build(2) - t2 = Treat::Entities::Token.build(2.2) - t3 = Treat::Entities::Token.build('2') - t4 = Treat::Entities::Token.build('2.2') - t.should be_an_instance_of Treat::Entities::Number - t2.should be_an_instance_of Treat::Entities::Number - t3.should be_an_instance_of Treat::Entities::Number - t4.should be_an_instance_of Treat::Entities::Number - t.to_i.should eql 2 - t2.to_i.should eql 2 - t3.to_i.should eql 2 - t4.to_i.should eql 2 - t.to_f.should eql 2.0 - t2.to_f.should eql 2.2 - t3.to_f.should eql 2.0 - t4.to_f.should eql 2.2 - end - end - - context "when supplied with a punctuation character" do - it "creates a punctuation with the text" do - t = Treat::Entities::Token.build('.') - t.should be_an_instance_of Treat::Entities::Punctuation - end - end - - context "when supplied with a symbol character" do - it "creates a symbol with the text" do - t = Treat::Entities::Token.build('¨') - t.should be_an_instance_of Treat::Entities::Symbol - end - end - end end - -end +end \ No newline at end of file diff --git a/spec/entities/word.rb b/spec/entities/word.rb index 2531302..02c88a4 100644 --- a/spec/entities/word.rb +++ b/spec/entities/word.rb @@ -1,3 +1,7 @@ -describe Treat::Entities::Word do +module Treat::Specs::Entities + + describe Treat::Entities::Word do + + end end diff --git a/spec/entities/zone.rb b/spec/entities/zone.rb index d213101..9d4f038 100644 --- a/spec/entities/zone.rb +++ b/spec/entities/zone.rb @@ -1,43 +1,47 @@ -describe Treat::Entities::Zone do +module Treat::Specs::Entities - describe "Buildable" do + describe Treat::Entities::Zone do - describe "#build" do + describe "Buildable" do - context "when called with a section of text" do + describe "#build" do - it "creates a section with the text" do + context "when called with a section of text" do - section = "A title\nFollowed by a fake sentence." - s = Treat::Entities::Zone.build(section) - s.should be_an_instance_of Treat::Entities::Section + it "creates a section with the text" do + + section = "A title\nFollowed by a fake sentence." + s = Treat::Entities::Zone.build(section) + s.should be_an_instance_of Treat::Entities::Section + + end end - end + context "when called with a paragraph of text" do - context "when called with a paragraph of text" do + it "creates a paragraph with the text" do + paragraph = "Sentence 1. Sentence 2. Sentence 3." + p = Treat::Entities::Zone.build(paragraph) + p.should be_instance_of Treat::Entities::Paragraph + end - it "creates a paragraph with the text" do - paragraph = "Sentence 1. Sentence 2. Sentence 3." - p = Treat::Entities::Zone.build(paragraph) - p.should be_instance_of Treat::Entities::Paragraph end - end + context "when called with a very short text" do - context "when called with a very short text" do + it "creates a title with the text" do + title = "A title!" + p = Treat::Entities::Zone.build(title) + p.should be_instance_of Treat::Entities::Title + end - it "creates a title with the text" do - title = "A title!" - p = Treat::Entities::Zone.build(title) - p.should be_instance_of Treat::Entities::Title end end end - - end + end + end diff --git a/spec/helper.rb b/spec/helper.rb index a0a253e..00f1434 100644 --- a/spec/helper.rb +++ b/spec/helper.rb @@ -7,7 +7,6 @@ module Treat::Specs require 'rspec' # Some configuration options for devel. - Treat.databases.mongo.db = 'treat_test' Treat.libraries.stanford.model_path = '/ruby/stanford-core-nlp-minimal/models/' @@ -18,6 +17,11 @@ module Treat::Specs Treat.libraries.reuters.model_path = '/ruby/reuters/models/' + # Mimic the ./lib structure. + module Entities; end + module Workers; end + module Learning; end + ModuleFiles = ['entities/*.rb', 'learning/*.rb'] # Provide helper functions for running specs. @@ -40,7 +44,7 @@ module Treat::Specs end # Run specs for the core classes. - def self.run_core_specs + def self.run_library_specs files = ModuleFiles.map do |d| Dir.glob(Treat.paths.spec + d) end diff --git a/spec/learning/data_set.rb b/spec/learning/data_set.rb index fcabefb..5ca0925 100644 --- a/spec/learning/data_set.rb +++ b/spec/learning/data_set.rb @@ -1,174 +1,178 @@ -describe Treat::Learning::DataSet do +module Treat::Specs::Learning - before do - @question = Treat::Learning::Question.new(:is_key_sentence, :sentence, 0, :continuous) - @feature = Treat::Learning::Feature.new(:word_count, 0) - @problem = Treat::Learning::Problem.new(@question, @feature) - @tag = Treat::Learning::Tag.new(:paragraph_length, 0, - "->(e) { e.parent_paragraph.word_count }") - @paragraph = Treat::Entities::Paragraph.new( - "Ranga and I went to the store. Meanwhile, Ryan was sleeping.") - @paragraph.do :segment, :tokenize - @sentence = @paragraph.sentences[0] - @data_set = Treat::Learning::DataSet.new(@problem) - end + describe Treat::Learning::DataSet do - describe "#initialize" do - context "when supplied with a problem" do - it "should initialize an empty data set" do - data_set = Treat::Learning::DataSet.new(@problem) - data_set.items.should eql [] - data_set.problem.should eql @problem - end + before do + @question = Treat::Learning::Question.new(:is_key_sentence, :sentence, 0, :continuous) + @feature = Treat::Learning::Feature.new(:word_count, 0) + @problem = Treat::Learning::Problem.new(@question, @feature) + @tag = Treat::Learning::Tag.new(:paragraph_length, 0, + "->(e) { e.parent_paragraph.word_count }") + @paragraph = Treat::Entities::Paragraph.new( + "Ranga and I went to the store. Meanwhile, Ryan was sleeping.") + @paragraph.apply :segment, :tokenize + @sentence = @paragraph.sentences[0] + @data_set = Treat::Learning::DataSet.new(@problem) end - context "when supplied with an improper argument" do - it "should raise an error" do - # The argument to initialize should be a Problem. - expect { data_set = Treat::Learning::DataSet.new("foo") }.to raise_error + + describe "#initialize" do + context "when supplied with a problem" do + it "should initialize an empty data set" do + data_set = Treat::Learning::DataSet.new(@problem) + data_set.items.should eql [] + data_set.problem.should eql @problem + end end - end - end - - describe "#self.build" do - - end - - describe "#==(other_data_set)" do - context "when supplied with an equivalent data set" do - it "returns true" do - data_set1 = Treat::Learning::DataSet.new(@problem) - data_set2 = Treat::Learning::DataSet.new(@problem) - data_set1.should == data_set2 - data_set1 << @sentence - data_set2 << @sentence - data_set1.should == data_set2 + context "when supplied with an improper argument" do + it "should raise an error" do + # The argument to initialize should be a Problem. + expect { data_set = Treat::Learning::DataSet.new("foo") }.to raise_error + end end end - context "when supplied with a non-equivalent data set" do - it "returns false" do - # Get two slightly different problems. - question1 = Treat::Learning::Question.new( - :is_key_sentence, :sentence, 0, :continuous) - question2 = Treat::Learning::Question.new( - :is_key_word, :sentence, 0, :continuous) - problem1 = Treat::Learning::Problem.new(question1, @feature) - problem2 = Treat::Learning::Problem.new(question2, @feature) - # Then the problems shouldn't be equal anymore. - problem1.should_not == problem2 - # Create data sets with the different problems. - data_set1 = Treat::Learning::DataSet.new(problem1) - data_set2 = Treat::Learning::DataSet.new(problem2) - # Then the data sets shouldn't be equal anymore. - data_set1.should_not == data_set2 - # Create two data sets with the same problems. - data_set1 = Treat::Learning::DataSet.new(@problem) - data_set2 = Treat::Learning::DataSet.new(@problem) - # Then these should be equal. - data_set1.should == data_set2 - # But when different items are added - data_set1 << Treat::Entities::Sentence.new( - "This sentence is not the same as the other.").tokenize - data_set2 << Treat::Entities::Sentence.new( - "This sentence is similar to the other.").tokenize - # They shouldn't be equal anymore. - data_set1.should_not == data_set2 + describe "#self.build" do + + end + + describe "#==(other_data_set)" do + context "when supplied with an equivalent data set" do + it "returns true" do + data_set1 = Treat::Learning::DataSet.new(@problem) + data_set2 = Treat::Learning::DataSet.new(@problem) + data_set1.should == data_set2 + data_set1 << @sentence + data_set2 << @sentence + data_set1.should == data_set2 + end end + + context "when supplied with a non-equivalent data set" do + it "returns false" do + # Get two slightly different problems. + question1 = Treat::Learning::Question.new( + :is_key_sentence, :sentence, 0, :continuous) + question2 = Treat::Learning::Question.new( + :is_key_word, :sentence, 0, :continuous) + problem1 = Treat::Learning::Problem.new(question1, @feature) + problem2 = Treat::Learning::Problem.new(question2, @feature) + # Then the problems shouldn't be equal anymore. + problem1.should_not == problem2 + # Create data sets with the different problems. + data_set1 = Treat::Learning::DataSet.new(problem1) + data_set2 = Treat::Learning::DataSet.new(problem2) + # Then the data sets shouldn't be equal anymore. + data_set1.should_not == data_set2 + # Create two data sets with the same problems. + data_set1 = Treat::Learning::DataSet.new(@problem) + data_set2 = Treat::Learning::DataSet.new(@problem) + # Then these should be equal. + data_set1.should == data_set2 + # But when different items are added + data_set1 << Treat::Entities::Sentence.new( + "This sentence is not the same as the other.").tokenize + data_set2 << Treat::Entities::Sentence.new( + "This sentence is similar to the other.").tokenize + # They shouldn't be equal anymore. + data_set1.should_not == data_set2 + end + end + + end + + describe "#merge" do + context "when supplied with two data sets refering to the same problem" do + it "merges the two together" do + # Create two data sets with the same problem. + data_set1 = Treat::Learning::DataSet.new(@problem) + data_set2 = Treat::Learning::DataSet.new(@problem) + # Add a sentence to each data set. + data_set1 << Treat::Entities::Sentence.new( + "This sentence is not the same as the other.").tokenize + data_set2 << Treat::Entities::Sentence.new( + "This sentence is similar to the other.").tokenize + # Merge the two data sets together. + data_set1.merge(data_set2) + # Check if the merge has occured properly. + data_set1.items.size.should eql 2 + data_set1.items[1].should eql data_set2.items[0] + end + end + + context "when supplied with two data sets refering to different problems" do + it "raises an error" do + # Get two slightly different questions. + question1 = Treat::Learning::Question.new( + :is_key_sentence, :sentence, 0, :continuous) + question2 = Treat::Learning::Question.new( + :is_key_word, :sentence, 0, :continuous) + # Create two problems with the different questions. + problem1 = Treat::Learning::Problem.new(question1, @feature) + problem2 = Treat::Learning::Problem.new(question2, @feature) + # Create two data sets with the different problems. + data_set1 = Treat::Learning::DataSet.new(problem1) + data_set2 = Treat::Learning::DataSet.new(problem2) + # Add elements to each of the data sets. + data_set1 << Treat::Entities::Sentence.new( + "This sentence is not the same as the other.").tokenize + data_set2 << Treat::Entities::Sentence.new( + "This sentence is similar to the other.").tokenize + # Try to merge them; but this should fail. + expect { data_set1.merge(data_set2) }.to raise_error + end + end + end + + describe "#<<(entity)" do + context "when supplied with a proper entity" do + it "exports the features and tags and adds them to the data set" do + problem = Treat::Learning::Problem.new(@question, @feature, @tag) + data_set = Treat::Learning::DataSet.new(problem) + data_set << @sentence + data_set.items.tap { |e| e[0][:id] = 0 }. + should eql [{:tags=>[11], :features=>[7, 0], :id=>0}] + end + end + end + + describe "#serialize" do + context "when asked to use a given adapter" do + it "calls the corresponding #to_something method" do + + end + end + end + + describe "#to_marshal, #self.from_marshal" do + context "when asked to successively serialize and deserialize data" do + it "completes a round trip without losing information" do + problem = Treat::Learning::Problem.new(@question, @feature, @tag) + data_set = Treat::Learning::DataSet.new(problem) + data_set << @sentence + data_set.to_marshal(file: 'test.dump') + Treat::Learning::DataSet.from_marshal( + file: 'test.dump').should == data_set + FileUtils.rm('test.dump') + end + end + end + + describe "#to_mongo" do + + end + + describe "#self.unserialize" do + context "when asked to use a given adapter" do + it "calls the corresponding #to_something method" do + + end + end + end + + describe "#self.from_mongo" do + end end - describe "#merge" do - context "when supplied with two data sets refering to the same problem" do - it "merges the two together" do - # Create two data sets with the same problem. - data_set1 = Treat::Learning::DataSet.new(@problem) - data_set2 = Treat::Learning::DataSet.new(@problem) - # Add a sentence to each data set. - data_set1 << Treat::Entities::Sentence.new( - "This sentence is not the same as the other.").tokenize - data_set2 << Treat::Entities::Sentence.new( - "This sentence is similar to the other.").tokenize - # Merge the two data sets together. - data_set1.merge(data_set2) - # Check if the merge has occured properly. - data_set1.items.size.should eql 2 - data_set1.items[1].should eql data_set2.items[0] - end - end - - context "when supplied with two data sets refering to different problems" do - it "raises an error" do - # Get two slightly different questions. - question1 = Treat::Learning::Question.new( - :is_key_sentence, :sentence, 0, :continuous) - question2 = Treat::Learning::Question.new( - :is_key_word, :sentence, 0, :continuous) - # Create two problems with the different questions. - problem1 = Treat::Learning::Problem.new(question1, @feature) - problem2 = Treat::Learning::Problem.new(question2, @feature) - # Create two data sets with the different problems. - data_set1 = Treat::Learning::DataSet.new(problem1) - data_set2 = Treat::Learning::DataSet.new(problem2) - # Add elements to each of the data sets. - data_set1 << Treat::Entities::Sentence.new( - "This sentence is not the same as the other.").tokenize - data_set2 << Treat::Entities::Sentence.new( - "This sentence is similar to the other.").tokenize - # Try to merge them; but this should fail. - expect { data_set1.merge(data_set2) }.to raise_error - end - end - end - - describe "#<<(entity)" do - context "when supplied with a proper entity" do - it "exports the features and tags and adds them to the data set" do - problem = Treat::Learning::Problem.new(@question, @feature, @tag) - data_set = Treat::Learning::DataSet.new(problem) - data_set << @sentence - data_set.items.tap { |e| e[0][:id] = 0 }. - should eql [{:tags=>[11], :features=>[7, 0], :id=>0}] - end - end - end - - describe "#serialize" do - context "when asked to use a given adapter" do - it "calls the corresponding #to_something method" do - - end - end - end - - describe "#to_marshal, #self.from_marshal" do - context "when asked to successively serialize and deserialize data" do - it "completes a round trip without losing information" do - problem = Treat::Learning::Problem.new(@question, @feature, @tag) - data_set = Treat::Learning::DataSet.new(problem) - data_set << @sentence - data_set.to_marshal(file: 'test.dump') - Treat::Learning::DataSet.from_marshal( - file: 'test.dump').should == data_set - FileUtils.rm('test.dump') - end - end - end - - describe "#to_mongo" do - - end - - describe "#self.unserialize" do - context "when asked to use a given adapter" do - it "calls the corresponding #to_something method" do - - end - end - end - - describe "#self.from_mongo" do - - end - -end \ No newline at end of file +end diff --git a/spec/learning/export.rb b/spec/learning/export.rb index ff9453f..8d33a24 100644 --- a/spec/learning/export.rb +++ b/spec/learning/export.rb @@ -1,52 +1,56 @@ -describe Treat::Learning::Export do +module Treat::Specs::Learning - describe "#initialize" do - context "when supplied with acceptable parameters" do - it "should give access to the parameters" do - export = Treat::Learning::Export.new(:name, 0, "->(e) { e }") - export.name.should eql :name - export.default.should eql 0 - export.proc_string.should eql "->(e) { e }" - export.proc.should be_instance_of Proc - export.proc.call('x').should eql 'x' + describe Treat::Learning::Export do + + describe "#initialize" do + context "when supplied with acceptable parameters" do + it "should give access to the parameters" do + export = Treat::Learning::Export.new(:name, 0, "->(e) { e }") + export.name.should eql :name + export.default.should eql 0 + export.proc_string.should eql "->(e) { e }" + export.proc.should be_instance_of Proc + export.proc.call('x').should eql 'x' + end + end + context "when supplied with wrong parameters" do + it "should raise an exception" do + # First argument should be a symbol representing the name of the export. + expect { Treat::Learning::Export.new(nil) }.to raise_error + # Third argument, if supplied, should be a string that + # evaluates to a proc (NOT a proc/lambda). + expect { Treat::Learning::Export.new(:name, 0, lambda { x } ) }.to raise_error + # Third argument should be proper ruby syntax. + expect { Treat::Learning::Export.new(:name, 0, "->(e) { ") }.to raise_error + # Third argument should evaluate to a proc. + expect { Treat::Learning::Export.new(:name, 0, "2") }.to raise_error + end end end - context "when supplied with wrong parameters" do - it "should raise an exception" do - # First argument should be a symbol representing the name of the export. - expect { Treat::Learning::Export.new(nil) }.to raise_error - # Third argument, if supplied, should be a string that - # evaluates to a proc (NOT a proc/lambda). - expect { Treat::Learning::Export.new(:name, 0, lambda { x } ) }.to raise_error - # Third argument should be proper ruby syntax. - expect { Treat::Learning::Export.new(:name, 0, "->(e) { ") }.to raise_error - # Third argument should evaluate to a proc. - expect { Treat::Learning::Export.new(:name, 0, "2") }.to raise_error + + describe "#==(question)" do + context "when supplied with an equal question" do + it "should return true" do + Treat::Learning::Export.new(:name). + should == Treat::Learning::Export.new(:name) + Treat::Learning::Export.new(:name, 0). + should == Treat::Learning::Export.new(:name, 0) + Treat::Learning::Export.new(:name, 0, "->(e) { }"). + should == Treat::Learning::Export.new(:name, 0, "->(e) { }") + end + end + context "when supplied with a different question" do + it "should return false" do + Treat::Learning::Export.new(:name). + should_not == Treat::Learning::Export.new(:name2) + Treat::Learning::Export.new(:name, 0). + should_not == Treat::Learning::Export.new(:name, 1) + Treat::Learning::Export.new(:name, 0, "->(e) { }"). + should_not == Treat::Learning::Export.new(:name, 0, "->(e) { x }") + end end end + end - describe "#==(question)" do - context "when supplied with an equal question" do - it "should return true" do - Treat::Learning::Export.new(:name). - should == Treat::Learning::Export.new(:name) - Treat::Learning::Export.new(:name, 0). - should == Treat::Learning::Export.new(:name, 0) - Treat::Learning::Export.new(:name, 0, "->(e) { }"). - should == Treat::Learning::Export.new(:name, 0, "->(e) { }") - end - end - context "when supplied with a different question" do - it "should return false" do - Treat::Learning::Export.new(:name). - should_not == Treat::Learning::Export.new(:name2) - Treat::Learning::Export.new(:name, 0). - should_not == Treat::Learning::Export.new(:name, 1) - Treat::Learning::Export.new(:name, 0, "->(e) { }"). - should_not == Treat::Learning::Export.new(:name, 0, "->(e) { x }") - end - end - end - -end \ No newline at end of file +end diff --git a/spec/learning/problem.rb b/spec/learning/problem.rb index 3a456b3..d79e3aa 100644 --- a/spec/learning/problem.rb +++ b/spec/learning/problem.rb @@ -1,144 +1,148 @@ -describe Treat::Learning::Problem do +module Treat::Specs::Learning - before do - @question = Treat::Learning::Question.new(:is_key_sentence, - :sentence, 0, :continuous) - @feature = Treat::Learning::Feature.new(:word_count, 0) - @tag = Treat::Learning::Tag.new(:paragraph_length, 0, - "->(e) { e.parent_paragraph.word_count }") - @paragraph = Treat::Entities::Paragraph.new( - "Ranga and I went to the store. Meanwhile, Ryan was sleeping.") - @paragraph.do :segment, :tokenize - @sentence = @paragraph.sentences[0] - @hash = {"question"=>{"name"=>:is_key_sentence, "target"=>:sentence, - "type"=>:continuous, "default"=>0}, "features"=>[ - {"proc_string"=>nil, "default"=>0, "name"=>:word_count, "proc"=>nil}], - "tags"=>[{"proc_string"=>"->(e) { e.parent_paragraph.word_count }", - "default"=>0, "name"=>:paragraph_length, "proc"=>nil}], "id"=>0} - end + describe Treat::Learning::Problem do - describe "#initialize" do - context "when supplied with proper arguments" do - it "initializes the problem and gives access to parameters" do - problem = Treat::Learning::Problem.new(@question, @feature, @tag) - problem.question.should eql @question - problem.features.should eql [@feature] - problem.tags.should eql [@tag] - problem.feature_labels.should eql [@feature.name] - problem.tag_labels.should eql [@tag.name] - # ID ??? FIXME - end + before do + @question = Treat::Learning::Question.new(:is_key_sentence, + :sentence, 0, :continuous) + @feature = Treat::Learning::Feature.new(:word_count, 0) + @tag = Treat::Learning::Tag.new(:paragraph_length, 0, + "->(e) { e.parent_paragraph.word_count }") + @paragraph = Treat::Entities::Paragraph.new( + "Ranga and I went to the store. Meanwhile, Ryan was sleeping.") + @paragraph.apply :segment, :tokenize + @sentence = @paragraph.sentences[0] + @hash = {"question"=>{"name"=>:is_key_sentence, "target"=>:sentence, + "type"=>:continuous, "default"=>0}, "features"=>[ + {"proc_string"=>nil, "default"=>0, "name"=>:word_count, "proc"=>nil}], + "tags"=>[{"proc_string"=>"->(e) { e.parent_paragraph.word_count }", + "default"=>0, "name"=>:paragraph_length, "proc"=>nil}], "id"=>0} end - context "when supplied with unacceptable arguments" do - it "raises an error" do - # First argument should be instance of Question. - expect { Treat::Learning::Problem.new('foo') }.to raise_error - # Arguments >= 2 should be instances of Export. - expect { Treat::Learning::Problem.new(@question, 'foo') }.to raise_error - # Should have at least one Feature in the arguments. - expect { Treat::Learning::Problem.new(@question, @tag) }.to raise_error - end - end - end - describe "#==(problem)" do - context "when supplied with an equal problem" do - it "should return true" do - Treat::Learning::Problem.new(@question, @feature). - should == Treat::Learning::Problem.new(@question, @feature) - Treat::Learning::Problem.new(@question, @feature, @tag). - should == Treat::Learning::Problem.new(@question, @feature, @tag) - end - end - context "when supplied with a different question" do - it "should return false" do - question = Treat::Learning::Question.new(:is_key_sentence, :sentence) - feature = Treat::Learning::Feature.new(:word_count, 999) - tag = Treat::Learning::Tag.new(:paragraph_length, 999) - Treat::Learning::Problem.new(@question, @feature). - should_not == Treat::Learning::Problem.new(question, @feature) - Treat::Learning::Problem.new(@question, @feature). - should_not == Treat::Learning::Problem.new(@question, feature) - Treat::Learning::Problem.new(@question, @feature, @tag). - should_not == Treat::Learning::Problem.new(@question, @feature, tag) - end - end - end - - describe "#export_tags(entity)" do - context "when called on a problem that has tags" do - context "and called with an entity of the proper type" do - it "returns an array of the tags" do + describe "#initialize" do + context "when supplied with proper arguments" do + it "initializes the problem and gives access to parameters" do problem = Treat::Learning::Problem.new(@question, @feature, @tag) - problem.export_tags(@sentence).should eql [11] + problem.question.should eql @question + problem.features.should eql [@feature] + problem.tags.should eql [@tag] + problem.feature_labels.should eql [@feature.name] + problem.tag_labels.should eql [@tag.name] + # ID ??? FIXME + end + end + context "when supplied with unacceptable arguments" do + it "raises an error" do + # First argument should be instance of Question. + expect { Treat::Learning::Problem.new('foo') }.to raise_error + # Arguments >= 2 should be instances of Export. + expect { Treat::Learning::Problem.new(@question, 'foo') }.to raise_error + # Should have at least one Feature in the arguments. + expect { Treat::Learning::Problem.new(@question, @tag) }.to raise_error end end end - context "when called on a problem that doesn't have tags" do - it "raises an error" do - problem = Treat::Learning::Problem.new(@question, @feature) - expect { problem.export_tags(@sentence) }.to raise_error + + describe "#==(problem)" do + context "when supplied with an equal problem" do + it "should return true" do + Treat::Learning::Problem.new(@question, @feature). + should == Treat::Learning::Problem.new(@question, @feature) + Treat::Learning::Problem.new(@question, @feature, @tag). + should == Treat::Learning::Problem.new(@question, @feature, @tag) + end + end + context "when supplied with a different question" do + it "should return false" do + question = Treat::Learning::Question.new(:is_key_sentence, :sentence) + feature = Treat::Learning::Feature.new(:word_count, 999) + tag = Treat::Learning::Tag.new(:paragraph_length, 999) + Treat::Learning::Problem.new(@question, @feature). + should_not == Treat::Learning::Problem.new(question, @feature) + Treat::Learning::Problem.new(@question, @feature). + should_not == Treat::Learning::Problem.new(@question, feature) + Treat::Learning::Problem.new(@question, @feature, @tag). + should_not == Treat::Learning::Problem.new(@question, @feature, tag) + end end end - end - describe "#export_features(entity, include_answer = true)" do - - context "when called with an entity of the proper type" do - context "and include_answer is set to true" do - context "and the answer is already set on the entity" do - it "returns an array of the exported features, with the answer" do - problem = Treat::Learning::Problem.new(@question, @feature) - @sentence.set :is_key_sentence, 1 - problem.export_features(@sentence).should eql [7, 1] - end - end - context "and the answer is not already set on the entity" do - it "returns an array of the exported features, with the question's default answer" do - problem = Treat::Learning::Problem.new(@question, @feature) - problem.export_features(@sentence).should eql [7, @question.default] + describe "#export_tags(entity)" do + context "when called on a problem that has tags" do + context "and called with an entity of the proper type" do + it "returns an array of the tags" do + problem = Treat::Learning::Problem.new(@question, @feature, @tag) + problem.export_tags(@sentence).should eql [11] end end end - context "and include_answer is set to false" do - it "returns an array of the exported features, without the answer" do + context "when called on a problem that doesn't have tags" do + it "raises an error" do problem = Treat::Learning::Problem.new(@question, @feature) - problem.export_features(@sentence, false).should eql [7] + expect { problem.export_tags(@sentence) }.to raise_error end end end - context "when supplied with an entity that is not of the proper type" do - it "raises an error" do - problem = Treat::Learning::Problem.new(@question, @feature) - word = Treat::Entities::Word.new('test') - expect { problem.export_features(word) }.to raise_error + + describe "#export_features(entity, include_answer = true)" do + + context "when called with an entity of the proper type" do + context "and include_answer is set to true" do + context "and the answer is already set on the entity" do + it "returns an array of the exported features, with the answer" do + problem = Treat::Learning::Problem.new(@question, @feature) + @sentence.set :is_key_sentence, 1 + problem.export_features(@sentence).should eql [7, 1] + end + end + context "and the answer is not already set on the entity" do + it "returns an array of the exported features, with the question's default answer" do + problem = Treat::Learning::Problem.new(@question, @feature) + problem.export_features(@sentence).should eql [7, @question.default] + end + end + end + context "and include_answer is set to false" do + it "returns an array of the exported features, without the answer" do + problem = Treat::Learning::Problem.new(@question, @feature) + problem.export_features(@sentence, false).should eql [7] + end + end + end + context "when supplied with an entity that is not of the proper type" do + it "raises an error" do + problem = Treat::Learning::Problem.new(@question, @feature) + word = Treat::Entities::Word.new('test') + expect { problem.export_features(word) }.to raise_error + end end end - end - describe "#to_hash" do - context "when called on a problem" do - it "returns a hash describing the problem" do - Treat::Learning::Problem.new(@question, @feature, @tag). - to_hash.tap { |e| e['id'] = 0 }.should eql @hash + describe "#to_hash" do + context "when called on a problem" do + it "returns a hash describing the problem" do + Treat::Learning::Problem.new(@question, @feature, @tag). + to_hash.tap { |e| e['id'] = 0 }.should eql @hash + end end end - end - describe "#self.from_hash" do - context "when called with a hash describing a problem" do - it "returns a problem based on the hash" do - problem = Treat::Learning::Problem.from_hash(@hash) - problem.question.name.should eql :is_key_sentence - problem.question.target.should eql :sentence - problem.question.type.should eql :continuous - problem.question.default.should eql 0 - problem.features[0].proc_string.should eql nil - problem.features[0].default.should eql 0 - problem.features[0].name.should eql :word_count - problem.features[0].proc.should eql nil + describe "#self.from_hash" do + context "when called with a hash describing a problem" do + it "returns a problem based on the hash" do + problem = Treat::Learning::Problem.from_hash(@hash) + problem.question.name.should eql :is_key_sentence + problem.question.target.should eql :sentence + problem.question.type.should eql :continuous + problem.question.default.should eql 0 + problem.features[0].proc_string.should eql nil + problem.features[0].default.should eql 0 + problem.features[0].name.should eql :word_count + problem.features[0].proc.should eql nil + end end end + end -end \ No newline at end of file +end diff --git a/spec/learning/question.rb b/spec/learning/question.rb index 1014e00..c594395 100644 --- a/spec/learning/question.rb +++ b/spec/learning/question.rb @@ -1,52 +1,56 @@ -describe Treat::Learning::Question do +module Treat::Specs::Learning - describe "#initialize" do - context "when supplied with acceptable parameters" do - it "should give access to the parameters" do - question = Treat::Learning::Question.new( - :is_keyword, :word, 0, :continuous) - question.name.should eql :is_keyword - question.target.should eql :word - question.type.should eql :continuous - question.default.should eql 0 + describe Treat::Learning::Question do + + describe "#initialize" do + context "when supplied with acceptable parameters" do + it "should give access to the parameters" do + question = Treat::Learning::Question.new( + :is_keyword, :word, 0, :continuous) + question.name.should eql :is_keyword + question.target.should eql :word + question.type.should eql :continuous + question.default.should eql 0 + end + end + context "when supplied with wrong parameters" do + it "should raise an exception" do + # Name should be a symbol + expect { Treat::Learning::Question.new( + nil, :sentence) }.to raise_error + # Target should be an actual entity type + expect { Treat::Learning::Question.new( + :name, :foo) }.to raise_error + # Distribution type should be continuous or discrete + expect { Treat::Learning::Question.new( + :name, :sentence, 0, :nonsense) }.to raise_error + end end end - context "when supplied with wrong parameters" do - it "should raise an exception" do - # Name should be a symbol - expect { Treat::Learning::Question.new( - nil, :sentence) }.to raise_error - # Target should be an actual entity type - expect { Treat::Learning::Question.new( - :name, :foo) }.to raise_error - # Distribution type should be continuous or discrete - expect { Treat::Learning::Question.new( - :name, :sentence, 0, :nonsense) }.to raise_error + + describe "#==(question)" do + context "when supplied with an equal question" do + it "should return true" do + Treat::Learning::Question.new( + :is_keyword, :word). + should == Treat::Learning::Question.new( + :is_keyword, :word) + end + end + context "when supplied with a different question" do + it "should return false" do + Treat::Learning::Question.new( + :is_keyword, :word). + should_not == Treat::Learning::Question.new( + :is_keyword, :sentence) + Treat::Learning::Question.new( + :is_keyword, :word, 0, :continuous). + should_not == Treat::Learning::Question.new( + :is_keyword, :word, 0, :discrete) + end end end + end - describe "#==(question)" do - context "when supplied with an equal question" do - it "should return true" do - Treat::Learning::Question.new( - :is_keyword, :word). - should == Treat::Learning::Question.new( - :is_keyword, :word) - end - end - context "when supplied with a different question" do - it "should return false" do - Treat::Learning::Question.new( - :is_keyword, :word). - should_not == Treat::Learning::Question.new( - :is_keyword, :sentence) - Treat::Learning::Question.new( - :is_keyword, :word, 0, :continuous). - should_not == Treat::Learning::Question.new( - :is_keyword, :word, 0, :discrete) - end - end - end - -end \ No newline at end of file +end diff --git a/spec/treat.rb b/spec/treat.rb index b2bcae5..ceee8c2 100644 --- a/spec/treat.rb +++ b/spec/treat.rb @@ -1,47 +1,51 @@ require_relative 'helper' -describe Treat do - - describe "Syntactic sugar:" do - - describe "#sweeten!, #unsweeten!" do - it "respectively turn on and off syntactic sugar and " + - "define/undefine entity builders as uppercase methods " + - "in the global namespace" do - Treat.core.entities.list.each do |type| - next if type == :symbol - - Treat::Config.sweeten! - Treat.core.syntax.sweetened.should eql true - Object.method_defined?( - :"#{type.to_s.capitalize}"). - should eql true - - Treat::Config.unsweeten! - Treat.core.syntax.sweetened.should eql false - Object.method_defined?( - type.to_s.capitalize.intern).should eql false - Object.method_defined?( - :"#{type.to_s.capitalize}"). - should eql false - end - end - end - - end +class Treat::Specs - describe "Paths:" do - - paths = Treat.core.paths.description - # Check IO for bin, files, tmp, models. Fix. - paths.each_pair do |path, files| - describe "##{path}" do - it "provides the path to the #{files}" do - Treat.paths[path].should be_instance_of String + describe Treat do + + describe "Syntactic sugar:" do + + describe "#sweeten!, #unsweeten!" do + it "respectively turn on and off syntactic sugar and " + + "define/undefine entity builders as uppercase methods " + + "in the global namespace" do + Treat.core.entities.list.each do |type| + next if type == :symbol + + Treat::Config.sweeten! + Treat.core.syntax.sweetened.should eql true + Object.method_defined?( + :"#{type.to_s.capitalize}"). + should eql true + + Treat::Config.unsweeten! + Treat.core.syntax.sweetened.should eql false + Object.method_defined?( + type.to_s.capitalize.intern).should eql false + Object.method_defined?( + :"#{type.to_s.capitalize}"). + should eql false + end end end + end - + + describe "Paths:" do + + paths = Treat.core.paths.description + # Check IO for bin, files, tmp, models. Fix. + paths.each_pair do |path, files| + describe "##{path}" do + it "provides the path to the #{files}" do + Treat.paths[path].should be_instance_of String + end + end + end + + end + end -end \ No newline at end of file +end diff --git a/spec/workers/agnostic.rb b/spec/workers/agnostic.rb index 8646e34..4fb7dd8 100644 --- a/spec/workers/agnostic.rb +++ b/spec/workers/agnostic.rb @@ -1,89 +1,86 @@ -module Treat::Specs::Workers +class Treat::Specs::Workers::Agnostic - class Agnostic + @@workers = Treat.languages.agnostic.workers - @@workers = Treat.languages.agnostic.workers - - describe Treat::Workers::Extractors::Language do - before do - @entities = ["Obama and Sarkozy will meet in Berlin."] - @languages = ["english"] + describe Treat::Workers::Extractors::Language do + before do + @entities = ["Obama and Sarkozy will meet in Berlin."] + @languages = ["english"] + end + context "when called on any textual entity" do + it "returns the language of the entity" do + # Treat.core.language.detect = true + @@workers.extractors.language.each do |extractor| + @entities.map(&:language).should eql @languages + end + # Treat.core.language.detect = false end - context "when called on any textual entity" do - it "returns the language of the entity" do - # Treat.core.language.detect = true - @@workers.extractors.language.each do |extractor| - @entities.map(&:language).should eql @languages - end - # Treat.core.language.detect = false + end + end + + describe Treat::Workers::Extractors::TopicWords do + + before do + @collections = ["./spec/workers/examples/english/economist"] + @topic_words = [["euro", "zone", "european", "mrs", "greece", "chancellor", + "berlin", "practice", "german", "germans"], ["bank", "minister", "central", + "bajnai", "mr", "hu", "orban", "commission", "hungarian", "government"], + ["bank", "mr", "central", "bajnai", "prime", "government", "brussels", + "responsibility", "national", "independence"], ["mr", "bank", "central", + "policies", "prime", "minister", "today", "financial", "government", "funds"], + ["euro", "merkel", "mr", "zone", "european", "greece", "german", "berlin", + "sarkozy", "government"], ["mr", "bajnai", "today", "orban", "government", + "forced", "independence", "part", "hand", "minister"], ["sarkozy", "mrs", + "zone", "euro", "fiscal", "called", "greece", "merkel", "german", "financial"], + ["mr", "called", "central", "policies", "financial", "bank", "european", + "prime", "minister", "shift"], ["bajnai", "orban", "prime", "mr", "government", + "independence", "forced", "commission", "-", "hvg"], ["euro", "sarkozy", "fiscal", + "merkel", "mr", "chancellor", "european", "german", "agenda", "soap"], ["mr", + "bank", "called", "central", "today", "prime", "government", "minister", "european", + "crisis"], ["mr", "fiscal", "mrs", "sarkozy", "merkel", "euro", "summit", "tax", + "leaders", "ecb"], ["called", "government", "financial", "policies", "part", "bank", + "central", "press", "mr", "president"], ["sarkozy", "merkel", "euro", "mr", "summit", + "mrs", "fiscal", "merkozy", "economic", "german"], ["mr", "prime", "minister", + "policies", "government", "financial", "crisis", "bank", "called", "part"], ["mr", + "bank", "government", "today", "called", "central", "minister", "prime", "issues", + "president"], ["mr", "orban", "central", "government", "parliament", "hungarian", + "minister", "hu", "personal", "bajnai"], ["government", "called", "central", "european", + "today", "bank", "prime", "financial", "part", "deficit"], ["mr", "orban", "government", + "hungarian", "bank", "hvg", "minister", "-", "fidesz", "hand"], ["mr", "bank", "european", + "minister", "policies", "crisis", "government", "president", "called", "shift"]] + end + + context "when #topic_words is called on a chunked, segmented and tokenized collection" do + it "annotates the collection with the topic words and returns them" do + @@workers.extractors.topic_words.each do |extractor| + @collections.map(&method(:collection)) + .map { |col| col.apply(:chunk,:segment,:tokenize) } + map { |col| col.topic_words }.should eql @topic_words end end end + end - describe Treat::Workers::Extractors::TopicWords do - - before do - @collections = ["./spec/workers/examples/english/economist"] - @topic_words = [["euro", "zone", "european", "mrs", "greece", "chancellor", - "berlin", "practice", "german", "germans"], ["bank", "minister", "central", - "bajnai", "mr", "hu", "orban", "commission", "hungarian", "government"], - ["bank", "mr", "central", "bajnai", "prime", "government", "brussels", - "responsibility", "national", "independence"], ["mr", "bank", "central", - "policies", "prime", "minister", "today", "financial", "government", "funds"], - ["euro", "merkel", "mr", "zone", "european", "greece", "german", "berlin", - "sarkozy", "government"], ["mr", "bajnai", "today", "orban", "government", - "forced", "independence", "part", "hand", "minister"], ["sarkozy", "mrs", - "zone", "euro", "fiscal", "called", "greece", "merkel", "german", "financial"], - ["mr", "called", "central", "policies", "financial", "bank", "european", - "prime", "minister", "shift"], ["bajnai", "orban", "prime", "mr", "government", - "independence", "forced", "commission", "-", "hvg"], ["euro", "sarkozy", "fiscal", - "merkel", "mr", "chancellor", "european", "german", "agenda", "soap"], ["mr", - "bank", "called", "central", "today", "prime", "government", "minister", "european", - "crisis"], ["mr", "fiscal", "mrs", "sarkozy", "merkel", "euro", "summit", "tax", - "leaders", "ecb"], ["called", "government", "financial", "policies", "part", "bank", - "central", "press", "mr", "president"], ["sarkozy", "merkel", "euro", "mr", "summit", - "mrs", "fiscal", "merkozy", "economic", "german"], ["mr", "prime", "minister", - "policies", "government", "financial", "crisis", "bank", "called", "part"], ["mr", - "bank", "government", "today", "called", "central", "minister", "prime", "issues", - "president"], ["mr", "orban", "central", "government", "parliament", "hungarian", - "minister", "hu", "personal", "bajnai"], ["government", "called", "central", "european", - "today", "bank", "prime", "financial", "part", "deficit"], ["mr", "orban", "government", - "hungarian", "bank", "hvg", "minister", "-", "fidesz", "hand"], ["mr", "bank", "european", - "minister", "policies", "crisis", "government", "president", "called", "shift"]] - end - - context "when #topic_words is called on a chunked, segmented and tokenized collection" do - it "annotates the collection with the topic words and returns them" do - @@workers.extractors.topic_words.each do |extractor| - @collections.map(&method(:collection)) - .map { |col| col.apply(:chunk,:segment,:tokenize) } - map { |col| col.topic_words }.should eql @topic_words - end - end + describe Treat::Workers::Formatters::Serializers do + before do + @texts = ["A test entity"] + end + context "when #serialize is called on any textual entity" do + it "serializes the entity to disk and returns a pointer to the location" do + # m = Treat::Entities::Entity.build + @texts.map(&:to_entity).map(&:serialize) + .map(&method(:entity)).map(&:to_s).should eql @texts end end + end - describe Treat::Workers::Formatters::Serializers do - before do - @texts = ["A test entity"] - end - context "when #serialize is called on any textual entity" do - it "serializes the entity to disk and returns a pointer to the location" do - # m = Treat::Entities::Entity.build - @texts.map(&:to_entity).map(&:serialize) - .map(&method(:entity)).map(&:to_s).should eql @texts - end - end + describe Treat::Workers::Formatters::Unserializers do + before do + @texts = ["A te"] end + context "when #unserialize is called with a selector on any textual entity" do + it "unserializes the file and loads it in the entity" do - describe Treat::Workers::Formatters::Unserializers do - before do - @texts = ["A te"] - end - context "when #unserialize is called with a selector on any textual entity" do - it "unserializes the file and loads it in the entity" do - - end end end end diff --git a/spec/workers/english.rb b/spec/workers/english.rb index ff5dda2..306712c 100644 --- a/spec/workers/english.rb +++ b/spec/workers/english.rb @@ -2,432 +2,428 @@ require 'rspec' require_relative '../../lib/treat' -module Treat::Specs::Workers +class Treat::Specs::Workers::English - class English + @@workers = Treat.languages.english.workers + Treat.core.language.default = 'english' - @@workers = Treat.languages.english.workers - Treat.core.language.default = 'english' + describe Treat::Workers::Processors::Segmenters do - describe Treat::Workers::Processors::Segmenters do + before do + @zones = ["Qala is first referred to in a fifteenth century portolan preserved at the Vatican library has taken its name from the qala or port of Mondoq ir-Rummien. It is the easternmost village of Gozo and has been inhabited since early times. The development of the present settlement began in the second half of the seventeenth century. It is a pleasant and rural place with many natural and historic attractions.", + "Originally Radio Lehen il-Qala transmitted on frequency 106.5FM. But when consequently a national radio started transmissions on a frequency quite close, it caused a hindrance to our community radio." "People were complaining that the voice of the local radio was no longer clear and they were experiencing difficulty in following the programmes. This was a further proof of the value of the radio. It was a confirmation that it was a good and modern means of bringing the Christian message to the whole community. An official request was therefore made to the Broadcasting Authority and Radio Lehen il-Qala was given a new frequency - 106.3FM."] + @groups = [ + ["Qala is first referred to in a fifteenth century portolan preserved at the Vatican library has taken its name from the qala or port of Mondoq ir-Rummien.", "It is the easternmost village of Gozo and has been inhabited since early times.", "The development of the present settlement began in the second half of the seventeenth century.", "It is a pleasant and rural place with many natural and historic attractions."], + ["Originally Radio Lehen il-Qala transmitted on frequency 106.5FM.", "But when consequently a national radio started transmissions on a frequency quite close, it caused a hindrance to our community radio.", "People were complaining that the voice of the local radio was no longer clear and they were experiencing difficulty in following the programmes.", "This was a further proof of the value of the radio.", "It was a confirmation that it was a good and modern means of bringing the Christian message to the whole community.", "An official request was therefore made to the Broadcasting Authority and Radio Lehen il-Qala was given a new frequency - 106.3FM."] + ] + end - before do - @zones = ["Qala is first referred to in a fifteenth century portolan preserved at the Vatican library has taken its name from the qala or port of Mondoq ir-Rummien. It is the easternmost village of Gozo and has been inhabited since early times. The development of the present settlement began in the second half of the seventeenth century. It is a pleasant and rural place with many natural and historic attractions.", - "Originally Radio Lehen il-Qala transmitted on frequency 106.5FM. But when consequently a national radio started transmissions on a frequency quite close, it caused a hindrance to our community radio." "People were complaining that the voice of the local radio was no longer clear and they were experiencing difficulty in following the programmes. This was a further proof of the value of the radio. It was a confirmation that it was a good and modern means of bringing the Christian message to the whole community. An official request was therefore made to the Broadcasting Authority and Radio Lehen il-Qala was given a new frequency - 106.3FM."] - @groups = [ - ["Qala is first referred to in a fifteenth century portolan preserved at the Vatican library has taken its name from the qala or port of Mondoq ir-Rummien.", "It is the easternmost village of Gozo and has been inhabited since early times.", "The development of the present settlement began in the second half of the seventeenth century.", "It is a pleasant and rural place with many natural and historic attractions."], - ["Originally Radio Lehen il-Qala transmitted on frequency 106.5FM.", "But when consequently a national radio started transmissions on a frequency quite close, it caused a hindrance to our community radio.", "People were complaining that the voice of the local radio was no longer clear and they were experiencing difficulty in following the programmes.", "This was a further proof of the value of the radio.", "It was a confirmation that it was a good and modern means of bringing the Christian message to the whole community.", "An official request was therefore made to the Broadcasting Authority and Radio Lehen il-Qala was given a new frequency - 106.3FM."] - ] + context "when #segment is called on a zone" do + it "segments the zone into groups" do + @@workers.processors.segmenters.each do |segmenter| + @zones.map { |zone| zone.segment(segmenter) } + .map { |zone| zone.groups.map(&:to_s) } + .should eql @groups + end end + end + end - context "when #segment is called on a zone" do - it "segments the zone into groups" do - @@workers.processors.segmenters.each do |segmenter| - @zones.map { |zone| zone.segment(segmenter) } - .map { |zone| zone.groups.map(&:to_s) } - .should eql @groups - end + describe Treat::Workers::Processors::Tokenizers do + + before do + @groups = [ + "Julius Obsequens was a Roman writer who is believed to have lived in the middle of the fourth century AD.", + "The only work associated with his name is the Liber de prodigiis (Book of Prodigies), completely extracted from an epitome, or abridgment, written by Livy; De prodigiis was constructed as an account of the wonders and portents that occurred in Rome between 249 BC-12 BC.", + "Of great importance was the edition by the Basle Humanist Conrad Lycosthenes (1552), trying to reconstruct lost parts and illustrating the text with wood-cuts.", + "These have been interpreted as reports of unidentified flying objects (UFOs), but may just as well describe meteors, and, since Obsequens, probably, writes in the 4th century, that is, some 400 years after the events he describes, they hardly qualify as eye-witness accounts.", + '"At Aenariae, while Livius Troso was promulgating the laws at the beginning of the Italian war, at sunrise, there came a terrific noise in the sky, and a globe of fire appeared burning in the north.' + ] + @tokens = [ + ["Julius", "Obsequens", "was", "a", "Roman", "writer", "who", "is", "believed", + "to", "have", "lived", "in", "the", "middle", "of", "the", "fourth", "century", "AD", "."], + ["The", "only", "work", "associated", "with", "his", "name", "is", "the", "Liber", + "de", "prodigiis", "(", "Book", "of", "Prodigies", ")", ",", "completely", "extracted", + "from", "an", "epitome", ",", "or", "abridgment", ",", "written", "by", "Livy", ";", + "De", "prodigiis", "was", "constructed", "as", "an", "account", "of", "the", "wonders", + "and", "portents", "that", "occurred", "in", "Rome", "between", "249", "BC-12", "BC", "."], + ["Of", "great", "importance", "was", "the", "edition", "by", "the", "Basle", "Humanist", + "Conrad", "Lycosthenes", "(", "1552", ")", ",", "trying", "to", "reconstruct", "lost", + "parts", "and", "illustrating", "the", "text", "with", "wood-cuts", "."], + ["These", "have", "been", "interpreted", "as", "reports", "of", "unidentified", "flying", + "objects", "(", "UFOs", ")", ",", "but", "may", "just", "as", "well", "describe", "meteors", + ",", "and", ",", "since", "Obsequens", ",", "probably", ",", "writes", "in", "the", "4th", + "century", ",", "that", "is", ",", "some", "400", "years", "after", "the", "events", "he", + "describes", ",", "they", "hardly", "qualify", "as", "eye-witness", "accounts", "."], + ["\"", "At", "Aenariae", ",", "while", "Livius", "Troso", "was", "promulgating", "the", + "laws", "at", "the", "beginning", "of", "the", "Italian", "war", ",", "at", "sunrise", + ",", "there", "came", "a", "terrific", "noise", "in", "the", "sky", ",", "and", "a", + "globe", "of", "fire", "appeared", "burning", "in", "the", "north", "."] + ] + end + context "when #tokenize is called on a group" do + it "separates the group into tokens" do + @@workers.processors.tokenizers.each do |tokenizer| + @groups.dup.map { |text| group(text).tokenize(tokenizer) } + .map { |group| group.tokens.map(&:to_s) } + .should eql @tokens + end + end + end + end + + describe Treat::Workers::Processors::Parsers do + before do + @groups = ["A sentence to tokenize."] + @phrases = [["A sentence to tokenize.", "A sentence", "to tokenize", "tokenize"]] + end + context "when #parse is called on a group" do + it "tokenizes and parses the group into its syntactical phrases" do + @@workers.processors.parsers.each do |parser| + @groups.dup.map { |text| group(text).parse(parser) } + .map { |group| group.phrases.map(&:to_s)} + .should eql @phrases + end + end + end + end + + describe Treat::Workers::Lexicalizers::Taggers do + before do + @groups = ["I was running"] + @group_tags = [["PRP", "VBD", "VBG"]] + @tokens = ["running", "man", "2", ".", "$"] + @token_tags = ["VBG", "NN", "CD", ".", "$"] + end + context "when #tag is is called on a tokenized group" do + it "annotates each token in the group with its tag and returns the tag 'G'" do + @@workers.lexicalizers.taggers.each do |tagger| + @groups.map { |txt| group(txt).tag(tagger) } + .all? { |tag| tag == 'G' }.should be_true + @groups.map { |txt| group(txt).tokenize } + .map { |g| g.tokens.map(&:tag) } + .should eql @group_tags + end + end + end + context "when #tag is called on a token" do + it "annotates the token with its tag and returns it" do + @@workers.lexicalizers.taggers.each do |tagger| + @tokens.map { |tok| token(tok).tag(tagger) } + .should eql @token_tags + end + end + end + end + + describe Treat::Workers::Lexicalizers::Sensers do + before do + @words = ["throw", "weak", "table", "furniture"] + @hyponyms = [ + ["slam", "flap down", "ground", "prostrate", "hurl", "hurtle", + "cast", "heave", "pelt", "bombard", "defenestrate", "deliver", + "pitch", "shy", "drive", "deep-six", "throw overboard", "ridge", + "jettison", "fling", "lob", "chuck", "toss", "skim", "skip", + "skitter", "juggle", "flip", "flick", "pass", "shed", "molt", + "exuviate", "moult", "slough", "abscise", "exfoliate", "autotomize", + "autotomise", "pop", "switch on", "turn on", "switch off", "cut", + "turn off", "turn out", "shoot", "demoralize", "perplex", "vex", + "stick", "get", "puzzle", "mystify", "baffle", "beat", "pose", + "bewilder", "disorient", "disorientate"], + [], + ["correlation table", "contents", "table of contents", "actuarial table", + "statistical table", "calendar", "file allocation table", "periodic table", + "altar", "communion table", "Lord's table", "booth", "breakfast table", + "card table", "coffee table", "cocktail table", "conference table", + "council table", "council board", "console table", "console", "counter", + "desk", "dressing table", "dresser", "vanity", "toilet table", "drop-leaf table", + "gaming table", "gueridon", "kitchen table", "operating table", "Parsons table", + "pedestal table", "pier table", "platen", "pool table", "billiard table", + "snooker table", "stand", "table-tennis table", "ping-pong table", + "pingpong table", "tea table", "trestle table", "worktable", "work table", + "dining table", "board", "training table"], + ["baby bed", "baby's bed", "bedroom furniture", "bedstead", "bedframe", + "bookcase", "buffet", "counter", "sideboard", "cabinet", "chest of drawers", + "chest", "bureau", "dresser", "dining-room furniture", "etagere", "fitment", + "hallstand", "lamp", "lawn furniture", "nest", "office furniture", "seat", + "sectional", "Sheraton", "sleeper", "table", "wall unit", "wardrobe", + "closet", "press", "washstand", "wash-hand stand"] + ] + @hypernyms = [ + ["propel", "impel", "move", "remove", "take", "take away", "withdraw", + "put", "set", "place", "pose", "position", "lay", "communicate", + "intercommunicate", "engage", "mesh", "lock", "operate", "send", + "direct", "upset", "discompose", "untune", "disconcert", "discomfit", + "express", "verbalize", "verbalise", "utter", "give tongue to", "shape", + "form", "work", "mold", "mould", "forge", "dislodge", "bump", "turn", "release", "be"], + [], + ["array", "furniture", "piece of furniture", "article of furniture", + "tableland", "plateau", "gathering", "assemblage", "fare"], + ["furnishing"] + ] + @antonyms = [[], ["strong"], [], []] + @synonyms = [ + ["throw", "shed", "cast", "cast off", "shake off", "throw off", "throw away", + "drop", "thrust", "give", "flip", "switch", "project", "contrive", "bewilder", + "bemuse", "discombobulate", "hurl", "hold", "have", "make", "confuse", "fox", + "befuddle", "fuddle", "bedevil", "confound"], + ["weak", "watery", "washy", "unaccented", "light", "fallible", "frail", "imperfect", + "decrepit", "debile", "feeble", "infirm", "rickety", "sapless", "weakly", "faint"], + ["table", "tabular array", "mesa", "board"], + ["furniture", "piece of furniture", "article of furniture"] + ] + end + + context "when #synonym is called on a word, or #sense is "+ + "called on a word with option :nym set to 'hyponyms'" do + it "returns the hyponyms of the word" do + @@workers.lexicalizers.sensers.each do |senser| + @words.map { |txt| word(txt) } + .map { |wrd| wrd.hyponyms(senser) }.should eql @hyponyms + @words.map { |txt| word(txt) } + .map { |wrd| wrd.sense(nym: 'hyponyms') } + .should eql @hyponyms end end end - describe Treat::Workers::Processors::Tokenizers do - - before do - @groups = [ - "Julius Obsequens was a Roman writer who is believed to have lived in the middle of the fourth century AD.", - "The only work associated with his name is the Liber de prodigiis (Book of Prodigies), completely extracted from an epitome, or abridgment, written by Livy; De prodigiis was constructed as an account of the wonders and portents that occurred in Rome between 249 BC-12 BC.", - "Of great importance was the edition by the Basle Humanist Conrad Lycosthenes (1552), trying to reconstruct lost parts and illustrating the text with wood-cuts.", - "These have been interpreted as reports of unidentified flying objects (UFOs), but may just as well describe meteors, and, since Obsequens, probably, writes in the 4th century, that is, some 400 years after the events he describes, they hardly qualify as eye-witness accounts.", - '"At Aenariae, while Livius Troso was promulgating the laws at the beginning of the Italian war, at sunrise, there came a terrific noise in the sky, and a globe of fire appeared burning in the north.' - ] - @tokens = [ - ["Julius", "Obsequens", "was", "a", "Roman", "writer", "who", "is", "believed", - "to", "have", "lived", "in", "the", "middle", "of", "the", "fourth", "century", "AD", "."], - ["The", "only", "work", "associated", "with", "his", "name", "is", "the", "Liber", - "de", "prodigiis", "(", "Book", "of", "Prodigies", ")", ",", "completely", "extracted", - "from", "an", "epitome", ",", "or", "abridgment", ",", "written", "by", "Livy", ";", - "De", "prodigiis", "was", "constructed", "as", "an", "account", "of", "the", "wonders", - "and", "portents", "that", "occurred", "in", "Rome", "between", "249", "BC-12", "BC", "."], - ["Of", "great", "importance", "was", "the", "edition", "by", "the", "Basle", "Humanist", - "Conrad", "Lycosthenes", "(", "1552", ")", ",", "trying", "to", "reconstruct", "lost", - "parts", "and", "illustrating", "the", "text", "with", "wood-cuts", "."], - ["These", "have", "been", "interpreted", "as", "reports", "of", "unidentified", "flying", - "objects", "(", "UFOs", ")", ",", "but", "may", "just", "as", "well", "describe", "meteors", - ",", "and", ",", "since", "Obsequens", ",", "probably", ",", "writes", "in", "the", "4th", - "century", ",", "that", "is", ",", "some", "400", "years", "after", "the", "events", "he", - "describes", ",", "they", "hardly", "qualify", "as", "eye-witness", "accounts", "."], - ["\"", "At", "Aenariae", ",", "while", "Livius", "Troso", "was", "promulgating", "the", - "laws", "at", "the", "beginning", "of", "the", "Italian", "war", ",", "at", "sunrise", - ",", "there", "came", "a", "terrific", "noise", "in", "the", "sky", ",", "and", "a", - "globe", "of", "fire", "appeared", "burning", "in", "the", "north", "."] - ] - end - context "when #tokenize is called on a group" do - it "separates the group into tokens" do - @@workers.processors.tokenizers.each do |tokenizer| - @groups.dup.map { |text| group(text).tokenize(tokenizer) } - .map { |group| group.tokens.map(&:to_s) } - .should eql @tokens - end + context "when #hypernyms is called on a word or #sense is "+ + "called on a word with option :nym set to 'hyponyms'" do + it "returns the hyponyms of the word" do + @@workers.lexicalizers.sensers.each do |senser| + @words.map { |txt| word(txt) } + .map { |wrd| wrd.hypernyms(senser) }.should eql @hypernyms + @words.map { |txt| word(txt) } + .map { |wrd| wrd.sense(senser, nym: 'hypernyms') } + .should eql @hypernyms end end end - describe Treat::Workers::Processors::Parsers do - before do - @groups = ["A sentence to tokenize."] - @phrases = [["A sentence to tokenize.", "A sentence", "to tokenize", "tokenize"]] - end - context "when #parse is called on a group" do - it "tokenizes and parses the group into its syntactical phrases" do - @@workers.processors.parsers.each do |parser| - @groups.dup.map { |text| group(text).parse(parser) } - .map { |group| group.phrases.map(&:to_s)} - .should eql @phrases - end + context "when #antonyms is called on a word or #sense is" + + "called on a word with option :nym set to 'antonyms'" do + it "returns the hyponyms of the word" do + @@workers.lexicalizers.sensers.each do |senser| + @words.map { |txt| word(txt) } + .map { |wrd| wrd.antonyms(senser) }.should eql @antonyms + @words.map { |txt| word(txt) } + .map { |wrd| wrd.sense(senser, nym: 'antonyms') } + .should eql @antonyms end end end - describe Treat::Workers::Lexicalizers::Taggers do - before do - @groups = ["I was running"] - @group_tags = [["PRP", "VBD", "VBG"]] - @tokens = ["running", "man", "2", ".", "$"] - @token_tags = ["VBG", "NN", "CD", ".", "$"] - end - context "when #tag is is called on a tokenized group" do - it "annotates each token in the group with its tag and returns the tag 'G'" do - @@workers.lexicalizers.taggers.each do |tagger| - @groups.map { |txt| group(txt).tag(tagger) } - .all? { |tag| tag == 'G' }.should be_true - @groups.map { |txt| group(txt).tokenize } - .map { |g| g.tokens.map(&:tag) } - .should eql @group_tags - end - end - end - context "when #tag is called on a token" do - it "annotates the token with its tag and returns it" do - @@workers.lexicalizers.taggers.each do |tagger| - @tokens.map { |tok| token(tok).tag(tagger) } - .should eql @token_tags - end - end - end - end - - describe Treat::Workers::Lexicalizers::Sensers do - before do - @words = ["throw", "weak", "table", "furniture"] - @hyponyms = [ - ["slam", "flap down", "ground", "prostrate", "hurl", "hurtle", - "cast", "heave", "pelt", "bombard", "defenestrate", "deliver", - "pitch", "shy", "drive", "deep-six", "throw overboard", "ridge", - "jettison", "fling", "lob", "chuck", "toss", "skim", "skip", - "skitter", "juggle", "flip", "flick", "pass", "shed", "molt", - "exuviate", "moult", "slough", "abscise", "exfoliate", "autotomize", - "autotomise", "pop", "switch on", "turn on", "switch off", "cut", - "turn off", "turn out", "shoot", "demoralize", "perplex", "vex", - "stick", "get", "puzzle", "mystify", "baffle", "beat", "pose", - "bewilder", "disorient", "disorientate"], - [], - ["correlation table", "contents", "table of contents", "actuarial table", - "statistical table", "calendar", "file allocation table", "periodic table", - "altar", "communion table", "Lord's table", "booth", "breakfast table", - "card table", "coffee table", "cocktail table", "conference table", - "council table", "council board", "console table", "console", "counter", - "desk", "dressing table", "dresser", "vanity", "toilet table", "drop-leaf table", - "gaming table", "gueridon", "kitchen table", "operating table", "Parsons table", - "pedestal table", "pier table", "platen", "pool table", "billiard table", - "snooker table", "stand", "table-tennis table", "ping-pong table", - "pingpong table", "tea table", "trestle table", "worktable", "work table", - "dining table", "board", "training table"], - ["baby bed", "baby's bed", "bedroom furniture", "bedstead", "bedframe", - "bookcase", "buffet", "counter", "sideboard", "cabinet", "chest of drawers", - "chest", "bureau", "dresser", "dining-room furniture", "etagere", "fitment", - "hallstand", "lamp", "lawn furniture", "nest", "office furniture", "seat", - "sectional", "Sheraton", "sleeper", "table", "wall unit", "wardrobe", - "closet", "press", "washstand", "wash-hand stand"] - ] - @hypernyms = [ - ["propel", "impel", "move", "remove", "take", "take away", "withdraw", - "put", "set", "place", "pose", "position", "lay", "communicate", - "intercommunicate", "engage", "mesh", "lock", "operate", "send", - "direct", "upset", "discompose", "untune", "disconcert", "discomfit", - "express", "verbalize", "verbalise", "utter", "give tongue to", "shape", - "form", "work", "mold", "mould", "forge", "dislodge", "bump", "turn", "release", "be"], - [], - ["array", "furniture", "piece of furniture", "article of furniture", - "tableland", "plateau", "gathering", "assemblage", "fare"], - ["furnishing"] - ] - @antonyms = [[], ["strong"], [], []] - @synonyms = [ - ["throw", "shed", "cast", "cast off", "shake off", "throw off", "throw away", - "drop", "thrust", "give", "flip", "switch", "project", "contrive", "bewilder", - "bemuse", "discombobulate", "hurl", "hold", "have", "make", "confuse", "fox", - "befuddle", "fuddle", "bedevil", "confound"], - ["weak", "watery", "washy", "unaccented", "light", "fallible", "frail", "imperfect", - "decrepit", "debile", "feeble", "infirm", "rickety", "sapless", "weakly", "faint"], - ["table", "tabular array", "mesa", "board"], - ["furniture", "piece of furniture", "article of furniture"] - ] - end - - context "when #synonym is called on a word, or #sense is "+ - "called on a word with option :nym set to 'hyponyms'" do - it "returns the hyponyms of the word" do - @@workers.lexicalizers.sensers.each do |senser| - @words.map { |txt| word(txt) } - .map { |wrd| wrd.hyponyms(senser) }.should eql @hyponyms - @words.map { |txt| word(txt) } - .map { |wrd| wrd.sense(nym: 'hyponyms') } - .should eql @hyponyms - end - end - end - - context "when #hypernyms is called on a word or #sense is "+ - "called on a word with option :nym set to 'hyponyms'" do - it "returns the hyponyms of the word" do - @@workers.lexicalizers.sensers.each do |senser| - @words.map { |txt| word(txt) } - .map { |wrd| wrd.hypernyms(senser) }.should eql @hypernyms - @words.map { |txt| word(txt) } - .map { |wrd| wrd.sense(senser, nym: 'hypernyms') } - .should eql @hypernyms - end - end - end - - context "when #antonyms is called on a word or #sense is" + - "called on a word with option :nym set to 'antonyms'" do - it "returns the hyponyms of the word" do - @@workers.lexicalizers.sensers.each do |senser| - @words.map { |txt| word(txt) } - .map { |wrd| wrd.antonyms(senser) }.should eql @antonyms - @words.map { |txt| word(txt) } - .map { |wrd| wrd.sense(senser, nym: 'antonyms') } - .should eql @antonyms - end - end - end - - context "when #synonyms is called on a word or #sense is" + - "called on a word with option :nym set to 'synonyms'" do - it "returns the hyponyms of the word" do - @@workers.lexicalizers.sensers.each do |senser| - @words.map { |txt| word(txt) } - .map { |wrd| wrd.synonyms(senser) }.should eql @synonyms - @words.map { |txt| word(txt) } - .map { |wrd| wrd.sense(senser, nym: 'synonyms') } - .should eql @synonyms - end - end - end - - end - - describe Treat::Workers::Lexicalizers::Categorizers do - - before do - @phrase = "I was running" - @fragment = "world. Hello" - @sentence = "I am running." - @group_categories = ["phrase", - "fragment", "sentence"] - @tokens = ["running"] - @token_tags = ["verb"] - end - - context "when #category is called on a tokenized and tagged group" do - it "returns a tag corresponding to the group name" do - @@workers.lexicalizers.categorizers.each do |categorizer| - [phrase(@phrase), fragment(@fragment), sentence(@sentence)] - .map { |grp| grp.apply(:tag).category(categorizer) } - .should eql @group_categories - end - end - end - - context "when #category is called called on a tagged token" do - it "returns the category corresponding to the token's tag" do - @@workers.lexicalizers.categorizers.each do |categorizer| - @tokens.map { |tok| token(tok).apply(:tag).category(categorizer) } - .should eql @token_tags - end - end - end - - end - - describe Treat::Workers::Inflectors::Ordinalizers, - Treat::Workers::Inflectors::Cardinalizers do - - before do - @numbers = [1, 2, 3] - @ordinal = ["first", "second", "third"] - @cardinal = ["one", "two", "three"] - end - - context "when #ordinal is called on a number" do - it "returns the ordinal form (e.g. 'first') of the number" do - @@workers.inflectors.ordinalizers.each do |ordinalizer| - @numbers.map { |num| number(num) } - .map { |num| num.ordinal(ordinalizer) }.should eql @ordinal - end - end - end - - context "when #cardinal is called on a number" do - it "returns the cardinal form (e.g. 'second' of the number)" do - @@workers.inflectors.cardinalizers.each do |cardinalizer| - @numbers.map { |num| number(num) } - .map { |num| num.cardinal(cardinalizer) }.should eql @cardinal - end - end - end - - end - - describe Treat::Workers::Inflectors::Stemmers do - before do - @words = ["running"] - @stems = ["run"] - end - context "when #stem is called on a word" do - it "annotates the word with its stem and returns the stem" do - @@workers.inflectors.stemmers.each do |stemmer| - @words.map { |wrd| wrd.stem(stemmer) }.should eql @stems - end - end - end - end - - describe Treat::Workers::Extractors::NameTag do - before do - @groups = ["Obama and Sarkozy will meet in Berlin."] - @tags = [["person", nil, "person", nil, nil, nil, "location", nil]] - end - - context "when #name_tag called on a tokenized group" do - it "tags each token with its name tag" do - @@workers.extractors.name_tag.each do |tagger| - @groups.map { |grp| grp.tokenize.apply(:name_tag) } - .map { |grp| grp.tokens.map { |t| t.get(:name_tag) } } - .should eql @tags - end - end - end - - end - - describe Treat::Workers::Extractors::Topics do - before do - @files = ["./spec/workers/examples/english/test.txt"] - @topics = [['household goods and hardware', - 'united states of america', 'corporate/industrial']] - end - context "when #topics is called on a chunked, segmented and tokenized document" do - it "annotates the document with its general topics and returns them" do - @@workers.extractors.topics.each do |extractor| - @files.map { |f| document(f).apply(:chunk, :segment, :tokenize) } - .map { |doc| doc.topics }.should eql @topics - end - end - end - end - - describe Treat::Workers::Extractors::Time do - before do - @expressions = ["14 June 2012"] - @days = [14] - @months = [6] - @years = [2012] - end - context "when called on a tokenized group representing a time expression" do - it "returns the DateTime object corresponding to the time" do - @@workers.extractors.time.each do |extractor| - times = @expressions.map(&:time) - times.all? { |t| t.is_a?(DateTime) }.should be_true - times.map { |time| time.day }.should eql @days - times.map { |time| time.month }.should eql @months - times.map { |time| time.year }.should eql @years - end - end - end - end - - describe Treat::Workers::Inflectors::Conjugators do - before do - @infinitives = ["run"] - @participles = ["running"] - end - - context "when #present_participle is called on a word or #conjugate " + - "is called on a word with option :form set to 'present_participle'" do - it "returns the present participle form of the verb" do - @@workers.inflectors.conjugators.each do |conjugator| - @participles.map { |verb| verb - .infinitive(conjugator) } - .should eql @infinitives - @participles.map { |verb| verb.conjugate( - conjugator, form: 'infinitive') } - .should eql @infinitives - end - end - end - - context "when #infinitive is called on a word or #conjugate is " + - "called on a word with option :form set to 'infinitive'" do - it "returns the infinitive form of the verb" do - @@workers.inflectors.conjugators.each do |conjugator| - @infinitives.map { |verb| verb - .present_participle(conjugator) } - .should eql @participles - @infinitives.map { |verb| verb.conjugate( - conjugator, form: 'present_participle') } - .should eql @participles - end - end - end - - end - - describe Treat::Workers::Inflectors::Declensors do - before do - @singulars = ["man"] - @plurals = ["men"] - end - context "when #plural is called on a word, or #declense "+ - "is called on a word with option :count set to 'plural'" do - it "returns the plural form of the word" do - @@workers.inflectors.declensors.each do |declensor| - @singulars.map { |word| word.plural(declensor) } - .should eql @plurals - @singulars.map { |word| word - .declense(declensor, count: 'plural') } - .should eql @plurals - end - end - end - context "when #singular is called on a word, or #declense " + - "is called on a word with option :count set to 'singular'" do - it "returns the singular form of the word" do - @@workers.inflectors.declensors.each do |declensor| - next if declensor == :linguistics - @plurals.map { |word| word.singular(declensor) } - .should eql @singulars - @singulars.map { |word| word - .declense(declensor, count: 'singular') } - .should eql @singulars - end + context "when #synonyms is called on a word or #sense is" + + "called on a word with option :nym set to 'synonyms'" do + it "returns the hyponyms of the word" do + @@workers.lexicalizers.sensers.each do |senser| + @words.map { |txt| word(txt) } + .map { |wrd| wrd.synonyms(senser) }.should eql @synonyms + @words.map { |txt| word(txt) } + .map { |wrd| wrd.sense(senser, nym: 'synonyms') } + .should eql @synonyms end end end end + describe Treat::Workers::Lexicalizers::Categorizers do + + before do + @phrase = "I was running" + @fragment = "world. Hello" + @sentence = "I am running." + @group_categories = ["phrase", + "fragment", "sentence"] + @tokens = ["running"] + @token_tags = ["verb"] + end + + context "when #category is called on a tokenized and tagged group" do + it "returns a tag corresponding to the group name" do + @@workers.lexicalizers.categorizers.each do |categorizer| + [phrase(@phrase), fragment(@fragment), sentence(@sentence)] + .map { |grp| grp.apply(:tag).category(categorizer) } + .should eql @group_categories + end + end + end + + context "when #category is called called on a tagged token" do + it "returns the category corresponding to the token's tag" do + @@workers.lexicalizers.categorizers.each do |categorizer| + @tokens.map { |tok| token(tok).apply(:tag).category(categorizer) } + .should eql @token_tags + end + end + end + + end + + describe Treat::Workers::Inflectors::Ordinalizers, + Treat::Workers::Inflectors::Cardinalizers do + + before do + @numbers = [1, 2, 3] + @ordinal = ["first", "second", "third"] + @cardinal = ["one", "two", "three"] + end + + context "when #ordinal is called on a number" do + it "returns the ordinal form (e.g. 'first') of the number" do + @@workers.inflectors.ordinalizers.each do |ordinalizer| + @numbers.map { |num| number(num) } + .map { |num| num.ordinal(ordinalizer) }.should eql @ordinal + end + end + end + + context "when #cardinal is called on a number" do + it "returns the cardinal form (e.g. 'second' of the number)" do + @@workers.inflectors.cardinalizers.each do |cardinalizer| + @numbers.map { |num| number(num) } + .map { |num| num.cardinal(cardinalizer) }.should eql @cardinal + end + end + end + + end + + describe Treat::Workers::Inflectors::Stemmers do + before do + @words = ["running"] + @stems = ["run"] + end + context "when #stem is called on a word" do + it "annotates the word with its stem and returns the stem" do + @@workers.inflectors.stemmers.each do |stemmer| + @words.map { |wrd| wrd.stem(stemmer) }.should eql @stems + end + end + end + end + + describe Treat::Workers::Extractors::NameTag do + before do + @groups = ["Obama and Sarkozy will meet in Berlin."] + @tags = [["person", nil, "person", nil, nil, nil, "location", nil]] + end + + context "when #name_tag called on a tokenized group" do + it "tags each token with its name tag" do + @@workers.extractors.name_tag.each do |tagger| + @groups.map { |grp| grp.tokenize.apply(:name_tag) } + .map { |grp| grp.tokens.map { |t| t.get(:name_tag) } } + .should eql @tags + end + end + end + + end + + describe Treat::Workers::Extractors::Topics do + before do + @files = ["./spec/workers/examples/english/test.txt"] + @topics = [['household goods and hardware', + 'united states of america', 'corporate/industrial']] + end + context "when #topics is called on a chunked, segmented and tokenized document" do + it "annotates the document with its general topics and returns them" do + @@workers.extractors.topics.each do |extractor| + @files.map { |f| document(f).apply(:chunk, :segment, :tokenize) } + .map { |doc| doc.topics }.should eql @topics + end + end + end + end + + describe Treat::Workers::Extractors::Time do + before do + @expressions = ["14 June 2012"] + @days = [14] + @months = [6] + @years = [2012] + end + context "when called on a tokenized group representing a time expression" do + it "returns the DateTime object corresponding to the time" do + @@workers.extractors.time.each do |extractor| + times = @expressions.map(&:time) + times.all? { |t| t.is_a?(DateTime) }.should be_true + times.map { |time| time.day }.should eql @days + times.map { |time| time.month }.should eql @months + times.map { |time| time.year }.should eql @years + end + end + end + end + + describe Treat::Workers::Inflectors::Conjugators do + before do + @infinitives = ["run"] + @participles = ["running"] + end + + context "when #present_participle is called on a word or #conjugate " + + "is called on a word with option :form set to 'present_participle'" do + it "returns the present participle form of the verb" do + @@workers.inflectors.conjugators.each do |conjugator| + @participles.map { |verb| verb + .infinitive(conjugator) } + .should eql @infinitives + @participles.map { |verb| verb.conjugate( + conjugator, form: 'infinitive') } + .should eql @infinitives + end + end + end + + context "when #infinitive is called on a word or #conjugate is " + + "called on a word with option :form set to 'infinitive'" do + it "returns the infinitive form of the verb" do + @@workers.inflectors.conjugators.each do |conjugator| + @infinitives.map { |verb| verb + .present_participle(conjugator) } + .should eql @participles + @infinitives.map { |verb| verb.conjugate( + conjugator, form: 'present_participle') } + .should eql @participles + end + end + end + + end + + describe Treat::Workers::Inflectors::Declensors do + before do + @singulars = ["man"] + @plurals = ["men"] + end + context "when #plural is called on a word, or #declense "+ + "is called on a word with option :count set to 'plural'" do + it "returns the plural form of the word" do + @@workers.inflectors.declensors.each do |declensor| + @singulars.map { |word| word.plural(declensor) } + .should eql @plurals + @singulars.map { |word| word + .declense(declensor, count: 'plural') } + .should eql @plurals + end + end + end + context "when #singular is called on a word, or #declense " + + "is called on a word with option :count set to 'singular'" do + it "returns the singular form of the word" do + @@workers.inflectors.declensors.each do |declensor| + next if declensor == :linguistics + @plurals.map { |word| word.singular(declensor) } + .should eql @singulars + @singulars.map { |word| word + .declense(declensor, count: 'singular') } + .should eql @singulars + end + end + end + end + end