Skip to content

Commit

Permalink
make import/export work with simple hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ch4s3 committed Mar 6, 2018
1 parent 2ca9dfd commit 74fd30d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
33 changes: 23 additions & 10 deletions lib/classifier-reborn/bayes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# License:: LGPL

require 'set'
require 'yaml'

require_relative 'extensions/tokenizer/whitespace'
require_relative 'extensions/token_filter/stopword'
Expand Down Expand Up @@ -262,17 +261,31 @@ def reset
populate_initial_categories
end

# Read the yaml_data_file and populate the classifier
def self.import!(file_path)
data = File.read(file_path)
YAML.load(data)
# 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

# 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
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

private
Expand Down
10 changes: 6 additions & 4 deletions test/bayes/bayesian_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 10 additions & 10 deletions test/fixtures/reference.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--- !ruby/object:ClassifierReborn::Bayes
categories:
---
:auto_categorize: false
:categories:
:Interesting:
:dutch: 1
:paint: 2
Expand Down Expand Up @@ -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

0 comments on commit 74fd30d

Please sign in to comment.