CLI Basics

Use the classifier command-line tool with pre-trained models and train your own classifiers.

Command Line Interface

The classifier gem includes a powerful CLI that lets you classify text instantly with pre-trained models or train your own classifiers—no coding required.

Installation

Install the classifier gem which includes the CLI:

gem install classifier

Or install via Homebrew for CLI-only usage:

brew install classifier

Quick Start with Pre-trained Models

Classify text instantly using community-trained models:

# Detect spam
classifier -r sms-spam-filter "You won a free iPhone"
# => spam

# Analyze sentiment
classifier -r imdb-sentiment "This movie was absolutely amazing"
# => positive

# Detect emotions
classifier -r emotion-detection "I'm so happy today"
# => joy

List all available pre-trained models:

classifier models

Training Your Own Classifier

Build a custom classifier by training on your own data:

Train from Text

# Train the positive category
classifier train positive "I love this product"
classifier train positive "Excellent quality, highly recommend"

# Train the negative category
classifier train negative "Terrible experience"
classifier train negative "Complete waste of money"

# Classify new text
classifier "This is amazing"
# => positive

Train from Files

For larger datasets, train from text files:

# Train from individual files
classifier train positive reviews/good/*.txt
classifier train negative reviews/bad/*.txt

# Classify new text
classifier "Great product, highly recommend"
# => positive

Model Files

The CLI automatically saves your trained model to ./classifier.json. Use the -f flag to specify a different file:

# Train and save to custom file
classifier -f my-sentiment.json train positive "Great stuff"
classifier -f my-sentiment.json train negative "Terrible stuff"

# Use the model later
classifier -f my-sentiment.json "This is wonderful"
# => positive

Showing Probabilities

Use -p to see probability scores:

classifier -r imdb-sentiment -p "Hello world"
# => positive (0.94)

Commands

CommandDescription
classifier TEXTClassify text using the current model
classifier -r MODEL TEXTClassify using a remote model
classifier train CATEGORY [FILES...]Train a category from files or stdin
classifier infoShow model information
classifier fitFit the model (logistic regression)
classifier search QUERYSemantic search (LSI only)
classifier related ITEMFind related documents (LSI only)
classifier modelsList models in registry
classifier pull MODELDownload model from registry
classifier push FILEContribute model to registry

Options

# Model file (default: ./classifier.json)
classifier -f my-model.json "Text"

# Classifier type
classifier -m bayes "Text"    # Naive Bayes (default)
classifier -m lsi "Text"      # Latent Semantic Indexing
classifier -m knn "Text"      # K-Nearest Neighbors
classifier -m lr "Text"       # Logistic Regression

# Show probabilities
classifier -p "Text"

# KNN options
classifier -m knn -k 10 "Text"           # Number of neighbors
classifier -m knn --weighted "Text"      # Distance-weighted voting

# Logistic regression options
classifier -m lr --learning-rate 0.01 --max-iterations 200 "Text"

# Quiet mode
classifier -q "Text"

Piping and Scripting

The CLI works well in Unix pipelines:

# Classify from stdin
echo "This product is amazing" | classifier -r imdb-sentiment

# Batch classify a file
cat reviews.txt | while read line; do
  classifier -r imdb-sentiment "$line"
done

# Filter spam from a file
cat emails.txt | while read line; do
  result=$(classifier -r sms-spam-filter "$line")
  if [ "$result" = "ham" ]; then
    echo "$line"
  fi
done > clean_emails.txt

Next Steps