diff --git a/.gitignore b/.gitignore index 9275fad..a3e145b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,2 @@ Gemfile.lock pkg - -test/fixtures/export.yml \ No newline at end of file diff --git a/lib/classifier-reborn/bayes.rb b/lib/classifier-reborn/bayes.rb index 9ce3459..3107e4b 100644 --- a/lib/classifier-reborn/bayes.rb +++ b/lib/classifier-reborn/bayes.rb @@ -210,17 +210,31 @@ def add_category(category) alias_method :append_category, :add_category - # Read the yaml_data_file and populate the classifier - def self.import!(file_path) - data = File.read(file_path) - YAML.load(data) - end - - # Writes a file by calling #to_yaml on self - def export(file_path) - yaml = to_yaml - File.open(file_path, 'w') { |f| f.write(yaml) } - yaml + # Read the data and populate the backend in use + def import!(data) + @auto_categorize = data[:auto_categorize] + @categories = data[:categories] + @category_counts = data[:category_counts] + @category_word_count = data[:category_word_count] + @enable_stemmer = data[:enable_stemmer] + @enable_threshold = data[:enable_threshold] + @language = data[:language] + @threshold = data[:threshold] + @total_words = data[:total_words] + end + + def export + { + auto_categorize: @auto_categorize, + categories: @categories, + category_counts: @category_counts, + category_word_count: @category_word_count, + enable_stemmer: @enable_stemmer, + enable_threshold: @enable_threshold, + language: @language, + threshold: @threshold, + total_words: @total_words + } end end end diff --git a/test/bayes/bayesian_test.rb b/test/bayes/bayesian_test.rb index 1b8c832..ab09236 100755 --- a/test/bayes/bayesian_test.rb +++ b/test/bayes/bayesian_test.rb @@ -129,13 +129,15 @@ def test_export most lacks the idealization" @classifier.train_uninteresting %"Grasslands such as savannah and prairie where grasses are dominant are estimated to constitute forty percent of the land area of the Earth" - yaml = @classifier.export('test/fixtures/export.yml') - reference_file = File.read('test/fixtures/reference.yml') - assert_equal(yaml, reference_file) + exported_data = @classifier.export + reference_data = YAML.load(File.read('test/fixtures/reference.yml')) + assert_equal(exported_data, reference_data) end def test_import - classifier = ClassifierReborn::Bayes.import!('test/fixtures/reference.yml') + classifier = ClassifierReborn::Bayes.new + reference_data = YAML.load(File.read('test/fixtures/reference.yml')) + classifier.import!(reference_data) assert_equal('Interesting', classifier.classify('Dutch painting of the Golden Age')) end end diff --git a/test/fixtures/reference.yml b/test/fixtures/reference.yml index 1519826..9ed3c40 100644 --- a/test/fixtures/reference.yml +++ b/test/fixtures/reference.yml @@ -1,5 +1,6 @@ ---- !ruby/object:ClassifierReborn::Bayes -categories: +--- +:auto_categorize: false +:categories: :Interesting: :dutch: 1 :paint: 2 @@ -33,15 +34,14 @@ categories: :land: 1 :area: 1 :earth: 1 -total_words: 32 -category_counts: +:category_counts: :Interesting: 1 :Uninteresting: 1 -category_word_count: +:category_word_count: :Interesting: 18 :Uninteresting: 14 -language: en -auto_categorize: false -enable_threshold: false -threshold: 0.0 -enable_stemmer: true +:enable_stemmer: true +:enable_threshold: false +:language: en +:threshold: 0.0 +:total_words: 32