Class: RubyFastText::Classifier

Inherits:
Object
  • Object
show all
Extended by:
Commands
Defined in:
lib/ruby_fast_text/classifier.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Commands

params_to_s

Constructor Details

#initialize(params) ⇒ Classifier

Returns a new instance of Classifier.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ruby_fast_text/classifier.rb', line 10

def initialize(params)
  path = params[:path]
  model = params[:model]
  labels_number = params.fetch(:labels_number, 1)
  @probability = params[:probability]
  probability_param = probability ? 'predict-prob' : 'predict'
  #p File.expand_path File.dirname(__FILE__)
  #p $0
  @classifier = Dir.chdir(path) do
    IO.popen("fasttext #{probability_param} #{model} - #{labels_number}", 'r+')
  end
end

Instance Attribute Details

#classifierObject

Returns the value of attribute classifier.



3
4
5
# File 'lib/ruby_fast_text/classifier.rb', line 3

def classifier
  @classifier
end

#probabilityObject (readonly) Also known as: probability?

Returns the value of attribute probability.



5
6
7
# File 'lib/ruby_fast_text/classifier.rb', line 5

def probability
  @probability
end

Class Method Details

.test(params) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/ruby_fast_text/classifier.rb', line 35

def self.test(params)
  path = params[:path]
  model = params[:model]
  test_data = params[:test_data]
  k = params.fetch(:k, 1)
  Dir.chdir(path) do
    system("fasttext test #{model} #{test_data} #{k}")
  end
end

.train(params) ⇒ Object



28
29
30
31
32
33
# File 'lib/ruby_fast_text/classifier.rb', line 28

def self.train(params)
  path = params[:path]
  Dir.chdir(path) do
    system("fasttext supervised #{params_to_s(params)}")
  end
end

Instance Method Details

#predict(sentence) ⇒ Object



23
24
25
26
# File 'lib/ruby_fast_text/classifier.rb', line 23

def predict(sentence)
  classifier.puts(sentence)
  format_result(classifier.gets)
end