From 55d89fae7285d0bcf6f2af4d639a2643e8d6af55 Mon Sep 17 00:00:00 2001 From: Luis Parravicini Date: Sat, 26 Dec 2009 14:24:52 -0200 Subject: [PATCH] bugfix: after adding @stemmer on Classifier/LSI the objects couldnt be marshalled. --- lib/classifier/bayes.rb | 9 +++++++++ lib/classifier/lsi.rb | 11 +++++++++++ luisparravicini-classifier.gemspec | 2 +- test/bayes/bayesian_test.rb | 10 ++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/classifier/bayes.rb b/lib/classifier/bayes.rb index 1ec7cd6..6a0059d 100644 --- a/lib/classifier/bayes.rb +++ b/lib/classifier/bayes.rb @@ -131,6 +131,15 @@ def add_category(category) end alias append_category add_category + + def marshal_dump + [@categories, @total_words, @options ] + end + + def marshal_load(data) + @categories, @total_words, @options = data + end + end end diff --git a/lib/classifier/lsi.rb b/lib/classifier/lsi.rb index 4bdb707..d5a5cf4 100644 --- a/lib/classifier/lsi.rb +++ b/lib/classifier/lsi.rb @@ -295,6 +295,17 @@ def highest_ranked_stems( doc, count=3 ) return top_n.collect { |x| @word_list.word_for_index(arr.index(x))} end + def marshal_dump + [ @auto_rebuild, @word_list, @items, @version, @built_at_version, + @options, + ] + end + + def marshal_load(data) + @auto_rebuild, @word_list, @items, @version, @built_at_version, + @options = data + end + private def build_reduced_matrix( matrix, cutoff=0.75 ) # TODO: Check that M>=N on these dimensions! Transpose helps assure this diff --git a/luisparravicini-classifier.gemspec b/luisparravicini-classifier.gemspec index 4547944..c1e44b5 100644 --- a/luisparravicini-classifier.gemspec +++ b/luisparravicini-classifier.gemspec @@ -5,7 +5,7 @@ Gem::Specification.new do |s| s.name = %q{luisparravicini-classifier} - s.version = "1.3.7" + s.version = "1.3.8" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Yury Korolev"] diff --git a/test/bayes/bayesian_test.rb b/test/bayes/bayesian_test.rb index 48faa4e..4bd7986 100644 --- a/test/bayes/bayesian_test.rb +++ b/test/bayes/bayesian_test.rb @@ -55,4 +55,14 @@ def test_case_insensitive assert_equal c.classifications("ХОРОШО"), c.classifications("хорошо") assert_equal c.classifications("плОХО"), c.classifications("плохо") end + + def test_serialize + txt = "this can be serialized" + b = Classifier::Bayes.new(:categories => ['Interesting', 'Uninteresting']) + b.train_interesting(txt) + b.train_uninteresting("really uninteresting") + + b2 = Marshal::load(Marshal::dump(b)) + assert_equal b.classify(txt), b2.classify(txt) + end end