Skip to content

Commit 51083aa

Browse files
committed
refactor(cli): address PR review feedback
- Refactor nested conditionals to use guard clauses in command_train - Refactor nested conditionals to use guard clauses in command_classify - Remove redundant comments that restate what code already shows
1 parent db8a0a0 commit 51083aa

1 file changed

Lines changed: 17 additions & 20 deletions

File tree

lib/classifier/cli.rb

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,20 @@ def command_train
167167

168168
classifier = load_or_create_classifier
169169

170-
# For LSI, train with file paths as item keys
171170
if classifier.is_a?(LSI) && @args.any?
172171
train_lsi_from_files(classifier, category, @args)
173-
else
174-
text = read_training_input
175-
if text.empty?
176-
@error << 'Error: no training data provided'
177-
@exit_code = 2
178-
return
179-
end
180-
train_classifier(classifier, category, text)
172+
save_classifier(classifier)
173+
return
174+
end
175+
176+
text = read_training_input
177+
if text.empty?
178+
@error << 'Error: no training data provided'
179+
@exit_code = 2
180+
return
181181
end
182182

183+
train_classifier(classifier, category, text)
183184
save_classifier(classifier)
184185
end
185186

@@ -332,12 +333,12 @@ def command_related
332333
def command_classify
333334
text = @args.join(' ')
334335

335-
# No model and no input - show getting started guide
336+
if text.empty? && ($stdin.tty? || @stdin.nil?) && !File.exist?(@options[:model])
337+
show_getting_started
338+
return
339+
end
340+
336341
unless File.exist?(@options[:model])
337-
if text.empty? && ($stdin.tty? || @stdin.nil?)
338-
show_getting_started
339-
return
340-
end
341342
@error << "Error: model not found at #{@options[:model]}"
342343
@exit_code = 1
343344
return
@@ -347,10 +348,8 @@ def command_classify
347348

348349
if text.empty?
349350
lines = read_stdin_lines
350-
if lines.empty?
351-
show_model_usage(classifier)
352-
return
353-
end
351+
return show_model_usage(classifier) if lines.empty?
352+
354353
lines.each { |line| classify_and_output(classifier, line) }
355354
else
356355
classify_and_output(classifier, text)
@@ -384,7 +383,6 @@ def show_model_usage(classifier)
384383
def classify_and_output(classifier, text)
385384
return if text.strip.empty?
386385

387-
# LR requires explicit fit before classification
388386
if classifier.is_a?(LogisticRegression) && !classifier.fitted?
389387
raise StandardError, "Model not fitted. Run 'classifier fit' after training."
390388
end
@@ -486,7 +484,6 @@ def train_classifier(classifier, category, text)
486484
def train_lsi_from_files(classifier, category, files)
487485
files.each do |file|
488486
content = File.read(file)
489-
# Use file path as item key so search/related returns file paths
490487
classifier.add_item(file, category.to_sym) { content }
491488
end
492489
end

0 commit comments

Comments
 (0)