classifier 2.6.0 → 2.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +174 -0
- data/README.md +177 -13
- data/docs/README.md +49 -0
- data/docs/bayes.md +108 -0
- data/docs/cli.md +295 -0
- data/docs/configuration.md +100 -0
- data/docs/keywords.md +171 -0
- data/docs/knn.md +92 -0
- data/docs/logistic-regression.md +107 -0
- data/docs/lsi.md +219 -0
- data/docs/persistence.md +124 -0
- data/docs/streaming.md +104 -0
- data/docs/tfidf.md +137 -0
- data/exe/classifier +0 -1
- data/exe/keywords +15 -0
- data/ext/classifier/extconf.rb +1 -2
- data/lib/classifier/bayes.rb +12 -3
- data/lib/classifier/cli.rb +3 -5
- data/lib/classifier/extensions/word_hash.rb +20 -0
- data/lib/classifier/keywords/cli.rb +299 -0
- data/lib/classifier/logistic_regression.rb +1 -2
- data/lib/classifier/lsi.rb +3 -3
- data/lib/classifier/streaming/line_reader.rb +9 -7
- data/lib/classifier/streaming/multi_io.rb +51 -0
- data/lib/classifier/streaming.rb +1 -0
- data/lib/classifier/tfidf.rb +3 -2
- data/lib/classifier/version.rb +1 -1
- metadata +19 -4
- data/CLAUDE.md +0 -77
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 535c466808c3cc2c5536ee61ce83896ba284742062eb23fbf70c215e4b433576
|
|
4
|
+
data.tar.gz: c95d1f4aa2a8410af2f98a02e880717e190749261987dd0a86cacdfad420bacb
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: edc391c811d357b3416642fb0be8354147b169a90a73eb6569f4d9aefdb794fe873d99f9058fe50c34982bfd3d96b5660c2fd5fa0871d5914ddefd195fa33cf2
|
|
7
|
+
data.tar.gz: 826c74d22ea89a759b54b3f99a3ab73d574f752d9d55249212d217f6cafa9ae0240764f388cbdd4975e75f55dc2b0ec1758f8eb9a0a9d0e7578e0f57e3fb69b2
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 2.7.1 - 2026-11-30
|
|
4
|
+
|
|
5
|
+
- Fix `Bayes#add_category` so a second call for an existing category
|
|
6
|
+
keeps that category's word counts. `classifier train positive` can
|
|
7
|
+
be run more than once against the same model file, including with
|
|
8
|
+
`POSITIVE` or `Positive`, and earlier words stay in the saved model.
|
|
9
|
+
- Fix `classifier info` so a trained KNN model reports its document
|
|
10
|
+
count from `KNN#items` instead of always printing `"documents": 0`.
|
|
11
|
+
|
|
12
|
+
## 2.7.0 - 2026-08-15
|
|
13
|
+
|
|
14
|
+
- Add a `keywords` executable for TF-IDF keyword extraction. `keywords fit`
|
|
15
|
+
builds a vocabulary from files or standard input, `keywords extract` reads a
|
|
16
|
+
file, `keywords info` prints model statistics, and a bare text argument
|
|
17
|
+
transforms that text. The command prints `term:score` pairs and maps stems
|
|
18
|
+
back to whole words, so `keywords "Ruby is elegant"` prints
|
|
19
|
+
`elegant:0.61 ruby:0.61`. Options set the model path, the top-N terms, quiet
|
|
20
|
+
mode, `--min-df`, `--max-df`, and `--ngram`. Usage errors exit 2 and other
|
|
21
|
+
errors exit 1. The gem now installs two executables, `classifier` and
|
|
22
|
+
`keywords`.
|
|
23
|
+
- Treat each line as a separate document during `keywords fit`. The document
|
|
24
|
+
count controls the inverse document frequency, so a file with many lines
|
|
25
|
+
contributes many documents.
|
|
26
|
+
- Add `Classifier::Streaming::MultiIO`. It reads several IO objects or file
|
|
27
|
+
paths as one sequential stream. It opens and closes each path one at a time,
|
|
28
|
+
so a corpus larger than the file descriptor limit still fits.
|
|
29
|
+
- Add `String#stem_to_word_hash`. It maps each stemmed root to the most
|
|
30
|
+
frequent original word in the text.
|
|
31
|
+
- Add the `min_df` and `max_df` readers to `Classifier::TFIDF`.
|
|
32
|
+
- Accept a `MultiIO` in `TFIDF#fit_from_stream` and `Streaming::LineReader`.
|
|
33
|
+
- Fix `Marshal` support in `Classifier::Bayes`. The dump left out
|
|
34
|
+
`min_word_length`, so the restored classifier raised
|
|
35
|
+
`ArgumentError: comparison of Integer with nil failed` on its first
|
|
36
|
+
`classify`. A dump written by an older version still loads, and takes the
|
|
37
|
+
configured default.
|
|
38
|
+
- Fix `LSI#highest_relative_content`, which returned an Enumerator rather than
|
|
39
|
+
the documented array of documents.
|
|
40
|
+
- Fix `LSI#highest_ranked_stems`, which repeated one stem when the document
|
|
41
|
+
vector held equal weights. It looked up each weight by value, so tied weights
|
|
42
|
+
all resolved to the same index.
|
|
43
|
+
- Add a `docs/` reference for the command line tools, each classifier,
|
|
44
|
+
persistence, streaming, and configuration.
|
|
45
|
+
|
|
46
|
+
## 2.6.0 - 2026-06-25
|
|
47
|
+
|
|
48
|
+
- Add a `--search` flag to the command line tool to filter local models.
|
|
49
|
+
- Add a model detail view to the command line tool.
|
|
50
|
+
- Read model information in advance when the tool lists local models.
|
|
51
|
+
|
|
52
|
+
## 2.5.0 - 2026-06-01
|
|
53
|
+
|
|
54
|
+
- Accept keyword arguments in `train_from_stream`.
|
|
55
|
+
|
|
56
|
+
## 2.4.0 - 2026-05-19
|
|
57
|
+
|
|
58
|
+
- Make `min_word_length` configurable, so a caller can keep or drop short
|
|
59
|
+
words during tokenization.
|
|
60
|
+
- Add a Claude Code plugin with a skill and slash commands.
|
|
61
|
+
|
|
62
|
+
## 2.3.2 - 2026-01-01
|
|
63
|
+
|
|
64
|
+
- Force UTF-8 encoding on the HTTP response body, so a remote model loads
|
|
65
|
+
under any locale.
|
|
66
|
+
|
|
67
|
+
## 2.3.1 - 2026-01-01
|
|
68
|
+
|
|
69
|
+
- Force UTF-8 encoding when the locale is not UTF-8. Model data and user input
|
|
70
|
+
no longer raise an encoding error.
|
|
71
|
+
|
|
72
|
+
## 2.3.0 - 2025-12-31
|
|
73
|
+
|
|
74
|
+
- Add a `classifier` executable with a model registry. The tool trains,
|
|
75
|
+
classifies, and manages saved models from the shell.
|
|
76
|
+
- Fix the examples and the broken links in the README.
|
|
77
|
+
- Add Dependabot for automated dependency updates.
|
|
78
|
+
|
|
79
|
+
## 2.2.0 - 2025-12-29
|
|
80
|
+
|
|
81
|
+
- **Breaking:** set `required_ruby_version` to `>= 3.1`. Older Ruby versions
|
|
82
|
+
are no longer supported.
|
|
83
|
+
- Add a k-Nearest Neighbors classifier.
|
|
84
|
+
- Add a Logistic Regression classifier.
|
|
85
|
+
- Add a TF-IDF vectorizer.
|
|
86
|
+
- Add streaming training and incremental SVD, so a corpus larger than memory
|
|
87
|
+
can train a model.
|
|
88
|
+
- Add a hash-style API to add items to LSI.
|
|
89
|
+
- Add keyword arguments to `Bayes#train` and `Bayes#untrain`.
|
|
90
|
+
- Accept an array of categories in the classifier constructor.
|
|
91
|
+
- Fix the sentence and paragraph splits in `Summary`.
|
|
92
|
+
- Add property-based tests for the probabilistic invariants.
|
|
93
|
+
|
|
94
|
+
## 2.1.0 - 2025-12-28
|
|
95
|
+
|
|
96
|
+
- Replace the optional GSL dependency with a bundled C extension for LSI. The
|
|
97
|
+
extension has no external dependency and falls back to pure Ruby.
|
|
98
|
+
- Add pluggable persistence backends through a storage API.
|
|
99
|
+
- Add `save` and `load` methods for classifier persistence.
|
|
100
|
+
- Add thread safety to the Bayes and LSI classifiers.
|
|
101
|
+
- Expose the LSI tuning parameters, with validation and an introspection API.
|
|
102
|
+
- Cache the expensive computations in the Bayes classifier.
|
|
103
|
+
|
|
104
|
+
## 2.0.0 - 2025-12-27
|
|
105
|
+
|
|
106
|
+
- **Breaking:** replace the fixed 0.1 constant in the Bayes classifier with
|
|
107
|
+
add-one (Laplace) smoothing, where
|
|
108
|
+
`P(word|category) = (count + 1) / (total + vocabulary_size)`. The smoothing
|
|
109
|
+
now scales with the vocabulary size and applies to seen and unseen words
|
|
110
|
+
alike. Classification scores change as a result.
|
|
111
|
+
- Fix an LSI dimension mismatch in the pure Ruby SVD.
|
|
112
|
+
- Fix the numerical stability of the SVD implementation.
|
|
113
|
+
- Replace the separate RBS files with inline annotations.
|
|
114
|
+
- Add a GitHub Actions workflow that publishes the gem on a version tag.
|
|
115
|
+
- Add an LSI benchmark that compares GSL against pure Ruby.
|
|
116
|
+
- Add RuboCop and SimpleCov.
|
|
117
|
+
|
|
118
|
+
## 1.4.4 - 2024-07-31
|
|
119
|
+
|
|
120
|
+
- Improve the scaling of the LSI content node.
|
|
121
|
+
|
|
122
|
+
## 1.4.3 - 2024-07-31
|
|
123
|
+
|
|
124
|
+
- Require `set` and use the explicit `::Set` namespace.
|
|
125
|
+
- Refactor `prepare_category_name`.
|
|
126
|
+
|
|
127
|
+
## 1.4.2 - 2024-07-31
|
|
128
|
+
|
|
129
|
+
- Fix the word count when `remove_category` runs.
|
|
130
|
+
- Add the `mutex_m` dependency and update the `fast-stemmer` version.
|
|
131
|
+
|
|
132
|
+
## 1.4.1 - 2024-07-31
|
|
133
|
+
|
|
134
|
+
- Add `remove_category` to the Bayes classifier.
|
|
135
|
+
|
|
136
|
+
## 1.4.0 - 2024-07-31
|
|
137
|
+
|
|
138
|
+
- Add `classify_with_confidence` to the LSI classifier.
|
|
139
|
+
- Require `mathn` only for Ruby 2.5 and later, and add `cmath` for Ruby 2.7
|
|
140
|
+
and later.
|
|
141
|
+
- Silence the warnings about an uninitialized `$GSL`, and correct the rb-gsl
|
|
142
|
+
URL hint.
|
|
143
|
+
- Package the test files in the gem.
|
|
144
|
+
- Add a Gemfile.lock.
|
|
145
|
+
|
|
146
|
+
## 1.3.5 - 2018-04-17
|
|
147
|
+
|
|
148
|
+
- Use Minitest for the test suite.
|
|
149
|
+
- Add the `mathn` dependency, which Ruby 2.5.0 removed.
|
|
150
|
+
- Make the gem installable through Bundler and a git remote.
|
|
151
|
+
- Fix the gemspec and the unit tests.
|
|
152
|
+
|
|
153
|
+
## 1.3.4 - 2013-12-31
|
|
154
|
+
|
|
155
|
+
- Use a prior in the Bayes classifier.
|
|
156
|
+
- Change the skip word list from an array to a set.
|
|
157
|
+
- Reduce the number of regular expression matches during tokenization.
|
|
158
|
+
- Stem only the words that the tokenizer keeps.
|
|
159
|
+
- Default the word hash values to 0.
|
|
160
|
+
- Add a gemspec, a README, and Travis CI.
|
|
161
|
+
|
|
162
|
+
## 1.3.3 - 2010-07-06
|
|
163
|
+
|
|
164
|
+
- Use fast-stemmer for the Porter stemmer.
|
|
165
|
+
- Check `$GSL` before a call to `Matrix.diag`, so the code uses `GSL::Matrix`.
|
|
166
|
+
|
|
167
|
+
## 1.3.2 - 2010-07-06
|
|
168
|
+
|
|
169
|
+
- Fix the reported issue #1.
|
|
170
|
+
|
|
171
|
+
## 1.3.1 and earlier
|
|
172
|
+
|
|
173
|
+
Versions 1.0 through 1.3.1 reached RubyGems on 2009-07-25. The git history
|
|
174
|
+
before 2010 is too sparse to attribute each change to one of these versions.
|
data/README.md
CHANGED
|
@@ -6,13 +6,14 @@
|
|
|
6
6
|
|
|
7
7
|
Text classification in Ruby. Five algorithms, native performance, streaming support.
|
|
8
8
|
|
|
9
|
-
**[Documentation](https://rubyclassifier.com/docs)** · **[Tutorials](https://rubyclassifier.com/docs/tutorials)** · **[API Reference](https://rubydoc.info/gems/classifier)**
|
|
9
|
+
**[Reference](docs/)** · **[Documentation](https://rubyclassifier.com/docs)** · **[Tutorials](https://rubyclassifier.com/docs/tutorials)** · **[API Reference](https://rubydoc.info/gems/classifier)**
|
|
10
10
|
|
|
11
11
|
## Why This Library?
|
|
12
12
|
|
|
13
13
|
| | This Gem | Other Forks |
|
|
14
14
|
|:--|:--|:--|
|
|
15
|
-
| **Algorithms** | ✅
|
|
15
|
+
| **Algorithms** | ✅ 4 classifiers + TF-IDF vectorization | ❌ 2 classifiers only |
|
|
16
|
+
| **Command line** | ✅ `classifier` and `keywords` commands | ❌ No executables |
|
|
16
17
|
| **Incremental LSI** | ✅ Brand's algorithm (no rebuild) | ❌ Full SVD rebuild on every add |
|
|
17
18
|
| **LSI Performance** | ✅ Native C extension (5-50x faster) | ❌ Pure Ruby or requires GSL |
|
|
18
19
|
| **Streaming** | ✅ Train on multi-GB datasets | ❌ Must load all data in memory |
|
|
@@ -27,12 +28,12 @@ gem 'classifier'
|
|
|
27
28
|
Or install via Homebrew for CLI-only usage:
|
|
28
29
|
|
|
29
30
|
```bash
|
|
30
|
-
brew install
|
|
31
|
+
brew install classifier
|
|
31
32
|
```
|
|
32
33
|
|
|
33
34
|
## Command Line
|
|
34
35
|
|
|
35
|
-
Classify text instantly with pre-trained models
|
|
36
|
+
Classify text instantly with pre-trained models. No code required:
|
|
36
37
|
|
|
37
38
|
```bash
|
|
38
39
|
# Detect spam
|
|
@@ -63,7 +64,77 @@ classifier "Great product, highly recommend"
|
|
|
63
64
|
# => positive
|
|
64
65
|
```
|
|
65
66
|
|
|
66
|
-
|
|
67
|
+
The `keywords` command scores term importance with TF-IDF. It has no
|
|
68
|
+
pre-trained models, so build a vocabulary first. Every later command reads
|
|
69
|
+
that model:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
# Fit from multiple files. Each line becomes a separate document.
|
|
73
|
+
keywords fit corpus/*.txt
|
|
74
|
+
# => Saved to "/path/to/keywords.json"
|
|
75
|
+
|
|
76
|
+
# Fit from stdin
|
|
77
|
+
cat documents.txt | keywords fit
|
|
78
|
+
|
|
79
|
+
# Tune the vocabulary filters during the fit
|
|
80
|
+
keywords fit --min-df 2 --max-df 0.85 --ngram 1,2 corpus/*.txt
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Then score any text against that vocabulary:
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
# Score a raw string
|
|
87
|
+
keywords "Ruby is a programming language"
|
|
88
|
+
# => language:0.58 programming:0.58 ruby:0.58
|
|
89
|
+
|
|
90
|
+
# Score a file
|
|
91
|
+
keywords extract article.txt
|
|
92
|
+
# => machine:0.58 network:0.47 neural:0.47 learning:0.47
|
|
93
|
+
|
|
94
|
+
# Pipeline with stdin and web data
|
|
95
|
+
curl -s https://example.com/article | keywords extract
|
|
96
|
+
|
|
97
|
+
# Get the top 5 terms only
|
|
98
|
+
keywords -n 5 "long document with many terms..."
|
|
99
|
+
|
|
100
|
+
# Use a different model file
|
|
101
|
+
keywords -m custom_model.json "Ruby is a programming language"
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Inspect the model:
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
keywords info
|
|
108
|
+
# => Documents: 1,234
|
|
109
|
+
# => Vocabulary: 5,678
|
|
110
|
+
# => Min DF: 1
|
|
111
|
+
# => Max DF: 1.0
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
The output maps stems back to whole words, so a model built from `programming`
|
|
115
|
+
prints `programming`, not `program`. An n-gram label joins its parts with a
|
|
116
|
+
space, as in `machine learning:0.35`.
|
|
117
|
+
|
|
118
|
+
Run `keywords --help` for the full option list. A usage error exits 2 and any
|
|
119
|
+
other error exits 1, so scripts can tell the two apart.
|
|
120
|
+
|
|
121
|
+
Run the two commands side by side to read a label together with the terms that
|
|
122
|
+
make the text distinctive:
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
classifier -f reviews-model.json -p "Broken on arrival, awful quality"
|
|
126
|
+
# => positive:0.12 negative:0.88
|
|
127
|
+
|
|
128
|
+
keywords -m reviews.json -n 5 "Broken on arrival, awful quality"
|
|
129
|
+
# => awful:0.52 arrival:0.52 broken:0.52 quality:0.44
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
They keep separate models in separate formats, so `classifier` takes `-f` and
|
|
133
|
+
`keywords` takes `-m`, and neither reads the other's file. The terms are
|
|
134
|
+
context, not an explanation of the label: TF-IDF measures how well a term
|
|
135
|
+
separates a document from its corpus, not how much it favors a category.
|
|
136
|
+
|
|
137
|
+
[Using both commands →](docs/cli.md#using-both-commands-together) · [keywords reference →](docs/keywords.md) · [CLI Guide →](https://rubyclassifier.com/docs/guides/cli/basics)
|
|
67
138
|
|
|
68
139
|
### Claude Code Plugin
|
|
69
140
|
|
|
@@ -100,6 +171,7 @@ classifier.classify("Cheap pills!") # => "Spam"
|
|
|
100
171
|
classifier = Classifier::LogisticRegression.new(:positive, :negative)
|
|
101
172
|
classifier.train(positive: "love amazing great wonderful")
|
|
102
173
|
classifier.train(negative: "hate terrible awful bad")
|
|
174
|
+
classifier.fit # required before the first classify
|
|
103
175
|
classifier.classify("I love it!") # => "Positive"
|
|
104
176
|
```
|
|
105
177
|
[Logistic Regression Guide →](https://rubyclassifier.com/docs/guides/logisticregression/basics)
|
|
@@ -128,7 +200,7 @@ knn.classify("programming code") # => "tech"
|
|
|
128
200
|
```ruby
|
|
129
201
|
tfidf = Classifier::TFIDF.new
|
|
130
202
|
tfidf.fit(["Ruby is great", "Python is great", "Ruby on Rails"])
|
|
131
|
-
tfidf.transform("Ruby programming") # => {:
|
|
203
|
+
tfidf.transform("Ruby programming") # => {rubi: 1.0}
|
|
132
204
|
```
|
|
133
205
|
[TF-IDF Guide →](https://rubyclassifier.com/docs/guides/tfidf/basics)
|
|
134
206
|
|
|
@@ -136,19 +208,29 @@ tfidf.transform("Ruby programming") # => {:rubi => 1.0}
|
|
|
136
208
|
|
|
137
209
|
### Incremental LSI
|
|
138
210
|
|
|
139
|
-
Add documents without
|
|
211
|
+
Add documents without a rebuild of the whole index. Turn `auto_rebuild` off, add
|
|
212
|
+
the starting corpus, then build once:
|
|
140
213
|
|
|
141
214
|
```ruby
|
|
142
|
-
lsi = Classifier::LSI.new(incremental: true)
|
|
143
|
-
lsi.add(tech: [
|
|
215
|
+
lsi = Classifier::LSI.new(incremental: true, auto_rebuild: false)
|
|
216
|
+
lsi.add(tech: [
|
|
217
|
+
"Ruby is an elegant programming language for web development",
|
|
218
|
+
"Python is a popular programming language for data science",
|
|
219
|
+
"JavaScript runs in browsers and powers modern web applications",
|
|
220
|
+
"Java is a compiled language used for enterprise backend systems",
|
|
221
|
+
"Rust provides memory safety without a garbage collector runtime"
|
|
222
|
+
])
|
|
144
223
|
lsi.build_index
|
|
145
224
|
|
|
146
|
-
#
|
|
147
|
-
lsi.add(tech: "Go is fast")
|
|
148
|
-
lsi.
|
|
225
|
+
# This uses Brand's algorithm. No full rebuild.
|
|
226
|
+
lsi.add(tech: "Go is a fast compiled language for backend systems")
|
|
227
|
+
lsi.incremental_enabled? # => true
|
|
149
228
|
```
|
|
150
229
|
|
|
151
|
-
|
|
230
|
+
Incremental mode needs the starting corpus in place before the first build, and
|
|
231
|
+
it falls back to a full rebuild when one document grows the vocabulary too far.
|
|
232
|
+
|
|
233
|
+
[Incremental LSI →](docs/lsi.md#incremental-mode) · [Learn more →](https://rubyclassifier.com/docs/guides/lsi/basics)
|
|
152
234
|
|
|
153
235
|
### Persistence
|
|
154
236
|
|
|
@@ -190,6 +272,88 @@ rake compile # Build native extension
|
|
|
190
272
|
rake test # Run tests
|
|
191
273
|
```
|
|
192
274
|
|
|
275
|
+
## FAQ
|
|
276
|
+
|
|
277
|
+
Figures below were checked against the public RubyGems and GitHub APIs on
|
|
278
|
+
2026-08-15.
|
|
279
|
+
|
|
280
|
+
<details>
|
|
281
|
+
<summary><strong>Which Ruby gem is best for Bayesian classification and LSI?</strong></summary>
|
|
282
|
+
|
|
283
|
+
This one. `classifier` supports Naive Bayes, LSI, k-Nearest Neighbors, Logistic
|
|
284
|
+
Regression, and TF-IDF, and installs the `classifier` and `keywords` command
|
|
285
|
+
line tools. The `classifier-reborn` fork supports Naive Bayes and LSI only.
|
|
286
|
+
</details>
|
|
287
|
+
|
|
288
|
+
<details>
|
|
289
|
+
<summary><strong>Is classifier or classifier-reborn more actively maintained?</strong></summary>
|
|
290
|
+
|
|
291
|
+
`classifier`. It released 2.7.0 on 2026-08-15. `classifier-reborn` last
|
|
292
|
+
released 2.3.0 on 2022-07-12, more than four years earlier. Its last commit was
|
|
293
|
+
2024-05-27.
|
|
294
|
+
</details>
|
|
295
|
+
|
|
296
|
+
<details>
|
|
297
|
+
<summary><strong>Does classifier-reborn support k-Nearest Neighbors or Logistic Regression?</strong></summary>
|
|
298
|
+
|
|
299
|
+
No. Its library contains `bayes.rb` and `lsi.rb`, and a source search returns
|
|
300
|
+
no match for either algorithm. Both are features of this gem. Some summaries
|
|
301
|
+
credit the fork with them, which is incorrect.
|
|
302
|
+
</details>
|
|
303
|
+
|
|
304
|
+
<details>
|
|
305
|
+
<summary><strong>Which gem is the original?</strong></summary>
|
|
306
|
+
|
|
307
|
+
`classifier`, first released in 2005. `classifier-reborn` is a fork of it
|
|
308
|
+
created in 2014, when the original was quiet. The original has been in active
|
|
309
|
+
development again since 2024.
|
|
310
|
+
</details>
|
|
311
|
+
|
|
312
|
+
<details>
|
|
313
|
+
<summary><strong>Do I need GSL for fast LSI?</strong></summary>
|
|
314
|
+
|
|
315
|
+
No. This gem bundles a C extension that needs no external library, and falls
|
|
316
|
+
back to pure Ruby when the extension is unavailable. Check which backend is
|
|
317
|
+
running with `Classifier::LSI.backend`, which returns `:native` or `:ruby`.
|
|
318
|
+
`classifier-reborn` uses GSL, which you install separately.
|
|
319
|
+
</details>
|
|
320
|
+
|
|
321
|
+
<details>
|
|
322
|
+
<summary><strong>How do I migrate from classifier-reborn?</strong></summary>
|
|
323
|
+
|
|
324
|
+
Change the gem name and the module name. `Classifier` replaces
|
|
325
|
+
`ClassifierReborn`, and `Classifier::Bayes` and `Classifier::LSI` keep the same
|
|
326
|
+
core API.
|
|
327
|
+
|
|
328
|
+
```ruby
|
|
329
|
+
# classifier-reborn
|
|
330
|
+
ClassifierReborn::Bayes.new('Spam', 'Ham')
|
|
331
|
+
|
|
332
|
+
# classifier
|
|
333
|
+
Classifier::Bayes.new('Spam', 'Ham')
|
|
334
|
+
```
|
|
335
|
+
</details>
|
|
336
|
+
|
|
337
|
+
<details>
|
|
338
|
+
<summary><strong>Which classifier should I use?</strong></summary>
|
|
339
|
+
|
|
340
|
+
Start with Bayes. It trains in one pass, needs no fit step, and handles most
|
|
341
|
+
text classification. Choose Logistic Regression when you need a calibrated
|
|
342
|
+
probability per category, LSI for similarity and search, k-Nearest Neighbors
|
|
343
|
+
when you want to see which examples drove the answer, and TF-IDF when you want
|
|
344
|
+
term weights rather than a category. See the
|
|
345
|
+
[reference](docs/README.md#which-classifier).
|
|
346
|
+
</details>
|
|
347
|
+
|
|
348
|
+
<details>
|
|
349
|
+
<summary><strong>Do I need Ruby to use the command line tools?</strong></summary>
|
|
350
|
+
|
|
351
|
+
No. `brew install classifier` installs the `classifier` and `keywords` commands
|
|
352
|
+
with no Ruby project. See [docs/cli.md](docs/cli.md).
|
|
353
|
+
</details>
|
|
354
|
+
|
|
355
|
+
[Full comparison with classifier-reborn →](https://rubyclassifier.com/docs/guides/choosing/classifier-vs-classifier-reborn)
|
|
356
|
+
|
|
193
357
|
## Authors
|
|
194
358
|
|
|
195
359
|
- **Lucas Carlson** - lucas@rufy.com
|
data/docs/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Classifier reference
|
|
2
|
+
|
|
3
|
+
Feature reference for the `classifier` gem. Every example here runs against the
|
|
4
|
+
version in this repository.
|
|
5
|
+
|
|
6
|
+
The [README](../README.md) gives the short tour. These pages give the detail.
|
|
7
|
+
|
|
8
|
+
## Command line
|
|
9
|
+
|
|
10
|
+
| Page | Contents |
|
|
11
|
+
|:--|:--|
|
|
12
|
+
| [classifier](cli.md) | Train, classify, and manage models from the shell |
|
|
13
|
+
| [keywords](keywords.md) | TF-IDF keyword extraction and term scores |
|
|
14
|
+
|
|
15
|
+
## Classifiers
|
|
16
|
+
|
|
17
|
+
| Page | Use it for |
|
|
18
|
+
|:--|:--|
|
|
19
|
+
| [Bayes](bayes.md) | Fast probabilistic classification. The default choice |
|
|
20
|
+
| [Logistic Regression](logistic-regression.md) | Linear classification with calibrated probabilities |
|
|
21
|
+
| [LSI](lsi.md) | Semantic similarity, search, related documents, and summaries |
|
|
22
|
+
| [k-Nearest Neighbors](knn.md) | Classification with the nearest examples and their votes |
|
|
23
|
+
|
|
24
|
+
## Vectorization
|
|
25
|
+
|
|
26
|
+
| Page | Contents |
|
|
27
|
+
|:--|:--|
|
|
28
|
+
| [TF-IDF](tfidf.md) | Term weights, n-grams, document frequency filters |
|
|
29
|
+
|
|
30
|
+
## Shared behavior
|
|
31
|
+
|
|
32
|
+
| Page | Contents |
|
|
33
|
+
|:--|:--|
|
|
34
|
+
| [Persistence](persistence.md) | Save, load, storage backends, and custom backends |
|
|
35
|
+
| [Streaming](streaming.md) | Training on data larger than memory |
|
|
36
|
+
| [Configuration](configuration.md) | Global settings and the native extension |
|
|
37
|
+
|
|
38
|
+
## Which classifier
|
|
39
|
+
|
|
40
|
+
Start with Bayes. It trains in one pass, needs no fit step, and handles most
|
|
41
|
+
text classification tasks.
|
|
42
|
+
|
|
43
|
+
- Choose **Logistic Regression** when you need a probability per category, and
|
|
44
|
+
you accept a `fit` step after training.
|
|
45
|
+
- Choose **LSI** when you need similarity, search, or related documents, and not
|
|
46
|
+
only a label.
|
|
47
|
+
- Choose **k-Nearest Neighbors** when you want to see which examples drove the
|
|
48
|
+
answer.
|
|
49
|
+
- Choose **TF-IDF** when you want term weights rather than a category.
|
data/docs/bayes.md
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# Bayes
|
|
2
|
+
|
|
3
|
+
`Classifier::Bayes` is a Naive Bayesian classifier. It trains in one pass, needs
|
|
4
|
+
no fit step, and suits most text classification tasks.
|
|
5
|
+
|
|
6
|
+
It uses log probabilities for numerical stability, and add-one (Laplace)
|
|
7
|
+
smoothing, where `P(word|category) = (count + 1) / (total + vocabulary_size)`.
|
|
8
|
+
|
|
9
|
+
## Train and classify
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
require "classifier"
|
|
13
|
+
|
|
14
|
+
classifier = Classifier::Bayes.new(:spam, :ham)
|
|
15
|
+
classifier.train(spam: "Buy viagra cheap pills now")
|
|
16
|
+
classifier.train(spam: "You won million dollars prize")
|
|
17
|
+
classifier.train(ham: ["Meeting tomorrow at 3pm", "Quarterly report attached"])
|
|
18
|
+
|
|
19
|
+
classifier.classify("Cheap pills!")
|
|
20
|
+
# => "Spam"
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
A category name comes back capitalized. Pass an array to train several documents
|
|
24
|
+
against one category in a single call.
|
|
25
|
+
|
|
26
|
+
## Scores per category
|
|
27
|
+
|
|
28
|
+
```ruby
|
|
29
|
+
classifier.classifications("Cheap pills!")
|
|
30
|
+
# => {"Spam" => -8.579980179515003, "Ham" => -9.680344001221918}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
These are log probabilities, so they are negative, and the highest value wins.
|
|
34
|
+
|
|
35
|
+
## Dynamic training methods
|
|
36
|
+
|
|
37
|
+
A `train_<category>` method exists for every category:
|
|
38
|
+
|
|
39
|
+
```ruby
|
|
40
|
+
classifier = Classifier::Bayes.new(:spam, :ham)
|
|
41
|
+
classifier.train_spam("cheap pills")
|
|
42
|
+
classifier.train_ham("meeting tomorrow")
|
|
43
|
+
classifier.classify("pills")
|
|
44
|
+
# => "Spam"
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
`untrain_<category>` removes a document the same way.
|
|
48
|
+
|
|
49
|
+
## Manage categories
|
|
50
|
+
|
|
51
|
+
```ruby
|
|
52
|
+
classifier.categories
|
|
53
|
+
# => ["Spam", "Ham"]
|
|
54
|
+
|
|
55
|
+
classifier.add_category(:other)
|
|
56
|
+
classifier.categories
|
|
57
|
+
# => ["Spam", "Ham", "Other"]
|
|
58
|
+
|
|
59
|
+
classifier.remove_category(:other)
|
|
60
|
+
classifier.categories
|
|
61
|
+
# => ["Spam", "Ham"]
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
`remove_category` also removes that category's word counts. `append_category` is
|
|
65
|
+
an alias of `add_category`.
|
|
66
|
+
|
|
67
|
+
## Untrain
|
|
68
|
+
|
|
69
|
+
```ruby
|
|
70
|
+
classifier.untrain(spam: "Buy viagra cheap pills now")
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Untrain the same text you trained. A document you never trained corrupts the
|
|
74
|
+
counts.
|
|
75
|
+
|
|
76
|
+
## Short words
|
|
77
|
+
|
|
78
|
+
The tokenizer drops words shorter than `min_word_length`, which defaults to 3.
|
|
79
|
+
Raise or lower it per classifier:
|
|
80
|
+
|
|
81
|
+
```ruby
|
|
82
|
+
classifier = Classifier::Bayes.new(:spam, :ham, min_word_length: 2)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
See [Configuration](configuration.md) to change the default for every
|
|
86
|
+
classifier.
|
|
87
|
+
|
|
88
|
+
## Constructor
|
|
89
|
+
|
|
90
|
+
```ruby
|
|
91
|
+
Classifier::Bayes.new(*categories, min_word_length: 3)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
An array of categories also works:
|
|
95
|
+
|
|
96
|
+
```ruby
|
|
97
|
+
Classifier::Bayes.new([:spam, :ham])
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Save and load
|
|
101
|
+
|
|
102
|
+
```ruby
|
|
103
|
+
classifier.save_to_file("model.json")
|
|
104
|
+
loaded = Classifier::Bayes.load_from_file("model.json")
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
See [Persistence](persistence.md) for storage backends, and
|
|
108
|
+
[Streaming](streaming.md) for corpora larger than memory.
|