Class: TinyClassifier::Command::GenerateClassifier
- Inherits:
-
Base
- Object
- Base
- TinyClassifier::Command::GenerateClassifier
show all
- Defined in:
- lib/tiny-classifier/command/generate-classifier.rb
Instance Attribute Summary
Attributes inherited from Base
#classifier, #tokenizer
Instance Method Summary
collapse
Methods inherited from Base
#data_file_name, #data_file_path, #parse_command_line_options, run
Constructor Details
Returns a new instance of GenerateClassifier.
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/tiny-classifier/command/generate-classifier.rb', line 23
def initialize(argv=[])
super
@output_dir = Dir.pwd
option_parser.on("-o PATH", "--output-dir=PATH",
"Path to the classifier command to be saved (default=current directory)") do |output_dir|
@output_dir = output_dir
end
parse_command_line_options(argv)
end
|
Instance Method Details
#classifier_name ⇒ Object
64
65
66
|
# File 'lib/tiny-classifier/command/generate-classifier.rb', line 64
def classifier_name
@classifier_name ||= "tc-classify-#{@categories.basename}"
end
|
#output_file_path ⇒ Object
68
69
70
|
# File 'lib/tiny-classifier/command/generate-classifier.rb', line 68
def output_file_path
@output_file_path ||= prepare_output_file_path
end
|
#run ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/tiny-classifier/command/generate-classifier.rb', line 35
def run
super
unless data_file_path.exist?
raise NoTrainingData.new(data_file_path)
end
unless prepare_output_file_path.parent.exist?
raise InvalidOutputDir.new(prepare_output_file_path.parent)
end
FileUtils.mkdir_p(output_file_path.parent)
File.open(output_file_path, "w") do |file|
file.puts("#!/usr/bin/env ruby")
file.puts("require \"base64\"")
file.puts("require \"classifier-reborn\"")
file.puts("require \"tiny-classifier/command/classify\"")
file.puts("classifier_code = Base64.strict_decode64(\"#{encoded_classifier}\")")
file.puts("command = TinyClassifier::Command::Classify.new([")
file.puts(" \"--categories=#{@categories.all.join(",")}\",")
file.puts(" \"--tokenizer=#{@tokenizer.type}\",")
file.puts("])")
file.puts("command.classifier = Marshal.load(classifier_code)")
file.puts("command.run")
end
FileUtils.chmod("a+x", output_file_path)
true
rescue StandardError => error
handle_error(error)
end
|