Class: USDA

Inherits:
Object
  • Object
show all
Defined in:
lib/data/sr24/classifier.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUSDA

Returns a new instance of USDA.



12
13
14
15
16
# File 'lib/data/sr24/classifier.rb', line 12

def initialize
  @classifier = Classifier::Bayes.new 
  add_categories
  train
end

Instance Attribute Details

#classifierObject

Returns the value of attribute classifier.



10
11
12
# File 'lib/data/sr24/classifier.rb', line 10

def classifier
  @classifier
end

Instance Method Details

#add_categoriesObject



19
20
21
22
23
24
25
26
# File 'lib/data/sr24/classifier.rb', line 19

def add_categories
  categories_path =  Dir[ FBayesDir.sr24_data + "*"]
  @categories = categories_path.map{|path| path.gsub(FBayesDir.sr24_data, "")}

  @categories.each do |c|
    @classifier.add_category c
  end
end

#c(word) ⇒ Object



42
43
44
# File 'lib/data/sr24/classifier.rb', line 42

def c(word)
  @classifier.classify(word)
end

#d(word) ⇒ Object



46
47
48
# File 'lib/data/sr24/classifier.rb', line 46

def d(word)
  @classifier.classifications(word)
end

#trainObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/data/sr24/classifier.rb', line 28

def train 
  total_size = 0

  @categories.each do |category|
    method = "train_" + category.downcase
    data = File.read( FBayesDir.sr24_data + category.to_s)
    puts category +": " + data.size.to_s
    @classifier.send(method, data)
    total_size += data.size.to_i
  end

  puts "TOTAL SIZE: #{total_size}"
end