classifier 2.7.0 → 2.7.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 28ad3396d95e315e11525b87393aaf3d081cf537f63dee0ba89fdd1a8acab4ee
4
- data.tar.gz: 03b0b12d04d9ed05878a1b5a659383b72eb4eba12b6eaecf6b02b0356ee524b9
3
+ metadata.gz: b98ea62d5d8726c4b65f03b4de99766e75482b29ce0e1877254b30cfa680a048
4
+ data.tar.gz: afcf755c52f1438a1a84e245561f2f13ae03fd9d959d1e7d4e92716e5606fbc6
5
5
  SHA512:
6
- metadata.gz: 3675894eaf015c2a2b1f49fcc62d4ad55d487955f5978628d48563f90d5513b20f0ed3e2d7f6b2fc9498c99971c71482c67a1342b3eb39f027e20ca0f2fa6ee5
7
- data.tar.gz: ce38803fe759fce8f02013a9d5a6bc613af95ccf4c132ff8c203f961dffabcc0b1eca7db642fad4e3e488fdac5093dd62c8fdafde1831f2d9aa97d15f07fcb62
6
+ metadata.gz: 968dfdd51cb9b9f4cbb81ca2f81c894efbf390e60969dbb368807677c43586a2ab4e9ccfef415176757d672de5c72c86cbac75bab927a953a3c124f337591a77
7
+ data.tar.gz: 81bb882ce8b78e7ccafba5e34158747c8858777884098b76361637f27ecd6c164d034502e709ef690f1636428ea2542cd800a42a277fa684a4c223662e5124d9
data/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # Changelog
2
2
 
3
+ ## 2.7.2 - 2026-11-30
4
+
5
+ - Fix LSI and KNN JSON save/load so `min_word_length` is preserved.
6
+ Models trained with a non-default minimum no longer tokenize new
7
+ items with the config default after reload. JSON written before
8
+ this change still loads, and uses the current config default.
9
+
10
+ ## 2.7.1 - 2026-11-30
11
+
12
+ - Fix `Bayes#add_category` so a second call for an existing category
13
+ keeps that category's word counts. `classifier train positive` can
14
+ be run more than once against the same model file, including with
15
+ `POSITIVE` or `Positive`, and earlier words stay in the saved model.
16
+ - Fix `classifier info` so a trained KNN model reports its document
17
+ count from `KNN#items` instead of always printing `"documents": 0`.
18
+
3
19
  ## 2.7.0 - 2026-08-15
4
20
 
5
21
  - Add a `keywords` executable for TF-IDF keyword extraction. `keywords fit`
data/README.md CHANGED
@@ -12,7 +12,8 @@ Text classification in Ruby. Five algorithms, native performance, streaming supp
12
12
 
13
13
  | | This Gem | Other Forks |
14
14
  |:--|:--|:--|
15
- | **Algorithms** | ✅ 5 classifiers | ❌ 2 only |
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,7 +28,7 @@ gem 'classifier'
27
28
  Or install via Homebrew for CLI-only usage:
28
29
 
29
30
  ```bash
30
- brew install cardmagic/tap/classifier
31
+ brew install classifier
31
32
  ```
32
33
 
33
34
  ## Command Line
@@ -117,7 +118,23 @@ space, as in `machine learning:0.35`.
117
118
  Run `keywords --help` for the full option list. A usage error exits 2 and any
118
119
  other error exits 1, so scripts can tell the two apart.
119
120
 
120
- [keywords reference →](docs/keywords.md) · [CLI Guide →](https://rubyclassifier.com/docs/guides/cli/basics)
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)
121
138
 
122
139
  ### Claude Code Plugin
123
140
 
@@ -255,6 +272,88 @@ rake compile # Build native extension
255
272
  rake test # Run tests
256
273
  ```
257
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
+
258
357
  ## Authors
259
358
 
260
359
  - **Lucas Carlson** - lucas@rufy.com
data/docs/cli.md CHANGED
@@ -135,10 +135,161 @@ $ classifier -m lsi related article.txt
135
135
  | `-v`, `--version` | Print the gem version |
136
136
  | `-h`, `--help` | Print the full usage |
137
137
 
138
+ ## Using both commands together
139
+
140
+ `classifier` and `keywords` answer different questions about the same text.
141
+ `classifier` gives a category. `keywords` names the terms that make the text
142
+ distinctive against its corpus. Run them side by side to read a label together
143
+ with what the document is about.
144
+
145
+ Fit a vocabulary from the same corpus you train on, and the two views line up:
146
+
147
+ ```console
148
+ $ keywords fit -m reviews.json reviews/good/*.txt reviews/bad/*.txt
149
+ $ keywords info -m reviews.json
150
+ Documents: 8
151
+ Vocabulary: 37
152
+ Min DF: 1
153
+ Max DF: 1.0
154
+ ```
155
+
156
+ `keywords info` reports the corpus before you commit to training. A vocabulary
157
+ of 37 terms over 8 documents says the corpus is far too small, and no
158
+ classifier fixes that.
159
+
160
+ Then train, and read the two answers together:
161
+
162
+ ```console
163
+ $ classifier -f reviews-model.json train positive reviews/good/*.txt
164
+ $ classifier -f reviews-model.json train negative reviews/bad/*.txt
165
+
166
+ $ classifier -f reviews-model.json -p "Broken on arrival, awful quality and useless customer service"
167
+ positive:0.07 negative:0.93
168
+
169
+ $ keywords -m reviews.json -n 5 "Broken on arrival, awful quality and useless customer service"
170
+ useless:0.4 awful:0.4 arrival:0.4 broken:0.4 service:0.34
171
+ ```
172
+
173
+ The first line is the verdict. The second is the document in shorthand, which
174
+ tells you what the classifier was reading when a result surprises you.
175
+
176
+ **The second line is not an explanation of the first.** The two commands hold
177
+ separate models. `keywords` reports TF-IDF weight, which measures how well a
178
+ term separates this document from the rest of the corpus. It never sees the
179
+ classifier, and it does not know which category a term favors. A term can top
180
+ the list and carry no weight in the decision.
181
+
182
+ Read it as context, not as attribution. When a label looks wrong, the terms
183
+ tell you whether the document says what you assumed, which is usually the real
184
+ problem. For the weights a model actually holds, use
185
+ `Classifier::LogisticRegression#weights` from Ruby, which returns the learned
186
+ weight per term and per category:
187
+
188
+ ```ruby
189
+ classifier.weights("positive", limit: 5)
190
+ ```
191
+
192
+ No command line flag reports per-term weights for a Bayes model.
193
+
194
+ ### Find the words your corpus wastes on itself
195
+
196
+ A term that appears in nearly every document tells a classifier nothing, and
197
+ every corpus grows its own. A review corpus repeats `delivery`, a support
198
+ corpus repeats `ticket`. These are stopwords that no general stopword list
199
+ knows about, because they are specific to your data.
200
+
201
+ `keywords` finds them, because `--max-df` drops a term that appears in more
202
+ than the given ratio of documents. Compare the vocabulary size before and
203
+ after:
204
+
205
+ ```console
206
+ $ keywords fit -m default.json good.txt bad.txt
207
+ $ keywords info -m default.json
208
+ Documents: 12
209
+ Vocabulary: 42
210
+
211
+ $ keywords fit -m pruned.json --max-df 0.5 good.txt bad.txt
212
+ $ keywords info -m pruned.json
213
+ Documents: 12
214
+ Vocabulary: 41
215
+ ```
216
+
217
+ One term went. Score a document under each model to see which:
218
+
219
+ ```console
220
+ $ keywords -m default.json -n 3 "delivery was awful and broken"
221
+ broken:0.69 awful:0.69 delivery:0.24
222
+
223
+ $ keywords -m pruned.json -n 3 "delivery was awful and broken"
224
+ broken:0.71 awful:0.71
225
+ ```
226
+
227
+ `delivery` sat in all 12 documents and still drew weight. Dropping it sharpens
228
+ every term that carries real signal.
229
+
230
+ Watch the vocabulary count as you tune, because these bounds cut fast:
231
+
232
+ ```console
233
+ $ keywords fit -m tight.json --min-df 2 good.txt bad.txt
234
+ $ keywords info -m tight.json
235
+ Documents: 12
236
+ Vocabulary: 7
237
+ ```
238
+
239
+ `--min-df 2` took 42 terms down to 7. That is no longer a vocabulary, it is a
240
+ handful of words. Move one bound at a time and read `keywords info` after each
241
+ change.
242
+
243
+ ### Two commands, two models
244
+
245
+ The models are separate files in separate formats, and neither command reads
246
+ the other's:
247
+
248
+ ```console
249
+ $ classifier -f reviews.json "broken awful"
250
+ Error: Unknown classifier type in model: tfidf
251
+
252
+ $ keywords -m reviews-model.json "broken awful"
253
+ Error: Invalid vectorizer type: bayes
254
+ ```
255
+
256
+ Note the flags differ too. `classifier` takes `-f`, and `keywords` takes `-m`.
257
+
258
+ | | `classifier` | `keywords` |
259
+ |:--|:--|:--|
260
+ | Answers | Which category | Which terms matter |
261
+ | Model flag | `-f` | `-m` |
262
+ | Default model | `./classifier.json` | `./keywords.json` |
263
+ | Builds a model with | `train` | `fit` |
264
+ | Pre-trained models | Yes, through `-r` | No |
265
+
266
+ ### Do not pipe one into the other
267
+
268
+ `keywords` prints `term:score` pairs, which is not text to classify. Feeding
269
+ its output to `classifier` throws away the rest of the document and weakens the
270
+ result:
271
+
272
+ ```console
273
+ $ classifier -f reviews-model.json -p "$LONG_REVIEW"
274
+ positive:0.08 negative:0.92 # the whole review
275
+
276
+ $ keywords -m reviews.json -n 4 "$LONG_REVIEW"
277
+ arrived:0.6 refund:0.3 useless:0.3 build:0.3
278
+
279
+ $ classifier -f reviews-model.json -p "arrived refund useless build"
280
+ positive:0.21 negative:0.79 # weaker, from the top terms alone
281
+ ```
282
+
283
+ Confidence drops from 0.92 to 0.79. TF-IDF ranks a term by how much it
284
+ distinguishes one document from the rest of the corpus, which is not the same
285
+ as how much it signals a category. Here it puts `arrived` first, a neutral
286
+ word about delivery. Classify the full text, and read `keywords` alongside it
287
+ for context rather than for attribution.
288
+
138
289
  ## Install without Ruby
139
290
 
140
291
  Homebrew installs the command line tools on their own:
141
292
 
142
293
  ```bash
143
- brew install cardmagic/tap/classifier
294
+ brew install classifier
144
295
  ```
@@ -1,6 +1,6 @@
1
1
  require 'mkmf'
2
2
 
3
- # rubocop:disable Style/GlobalVars
3
+ # rubocop:disable-next Style/GlobalVars
4
4
  if ENV['COVERAGE']
5
5
  # Coverage flags: disable optimization for accurate line coverage
6
6
  $CFLAGS << ' -O0 -g --coverage -Wall'
@@ -9,7 +9,6 @@ else
9
9
  # Optimization flags for performance
10
10
  $CFLAGS << ' -O3 -ffast-math -Wall'
11
11
  end
12
- # rubocop:enable Style/GlobalVars
13
12
 
14
13
  # Create the Makefile
15
14
  create_makefile('classifier/classifier_ext')
@@ -260,6 +260,9 @@ module Classifier
260
260
  # For example:
261
261
  # b.add_category "Not spam"
262
262
  #
263
+ # If the category already exists after name normalization, this is a
264
+ # no-op: existing word counts and totals are left unchanged.
265
+ #
263
266
  # WARNING: Adding categories to a trained classifier will
264
267
  # result in an undertrained category that will tend to match
265
268
  # more criteria than the trained selective categories. In short,
@@ -268,9 +271,12 @@ module Classifier
268
271
  # @rbs (String | Symbol) -> Hash[Symbol, Integer]
269
272
  def add_category(category)
270
273
  synchronize do
274
+ name = category.prepare_category_name
275
+ return @categories[name] if @categories.key?(name)
276
+
271
277
  invalidate_caches
272
278
  @dirty = true
273
- @categories[category.prepare_category_name] = {}
279
+ @categories[name] = {}
274
280
  end
275
281
  end
276
282
 
@@ -270,10 +270,7 @@ module Classifier
270
270
  end
271
271
 
272
272
  def add_knn_info(info, classifier)
273
- data = classifier.instance_variable_get(:@data) || []
274
- info[:documents] = data.size
275
- categories = data.map { |d| d[:category] }.uniq
276
- info[:categories] = categories.map(&:to_s) unless categories.empty?
273
+ info[:documents] = classifier.items.size
277
274
  end
278
275
 
279
276
  def command_fit
@@ -770,7 +767,8 @@ module Classifier
770
767
  def train_classifier(classifier, category, text)
771
768
  case classifier
772
769
  when Bayes, LogisticRegression
773
- classifier.add_category(category) unless classifier.categories.include?(category)
770
+ prepared = category.prepare_category_name.to_s
771
+ classifier.add_category(category) unless classifier.categories.include?(prepared)
774
772
  text.each_line { |line| classifier.train(category, line.strip) unless line.strip.empty? }
775
773
  when LSI
776
774
  text.each_line do |line|
@@ -58,7 +58,7 @@ module Classifier
58
58
  #
59
59
  # @rbs (*String | Symbol | Array[String | Symbol], ?learning_rate: Float, ?regularization: Float,
60
60
  # ?max_iterations: Integer, ?tolerance: Float, ?min_word_length: Integer) -> void
61
- # rubocop:disable Metrics/ParameterLists
61
+ # rubocop:disable-next Metrics/ParameterLists
62
62
  def initialize(*categories, learning_rate: DEFAULT_LEARNING_RATE,
63
63
  regularization: DEFAULT_REGULARIZATION,
64
64
  max_iterations: DEFAULT_MAX_ITERATIONS,
@@ -80,7 +80,6 @@ module Classifier
80
80
  @storage = nil
81
81
  @min_word_length = min_word_length
82
82
  end
83
- # rubocop:enable Metrics/ParameterLists
84
83
 
85
84
  # Trains the classifier with text for a category.
86
85
  #
@@ -517,6 +517,7 @@ module Classifier
517
517
  version: 1,
518
518
  type: 'lsi',
519
519
  auto_rebuild: @auto_rebuild,
520
+ min_word_length: @min_word_length,
520
521
  items: items_data
521
522
  }
522
523
  end
@@ -539,7 +540,10 @@ module Classifier
539
540
  raise ArgumentError, "Invalid classifier type: #{data['type']}" unless data['type'] == 'lsi'
540
541
 
541
542
  # Create instance with auto_rebuild disabled during loading
542
- instance = new(auto_rebuild: false)
543
+ instance = new(
544
+ auto_rebuild: false,
545
+ min_word_length: data['min_word_length'] || Classifier.config.min_word_length
546
+ )
543
547
 
544
548
  # Restore items (categories stay as strings, matching original storage)
545
549
  data['items'].each do |item_key, item_data|
@@ -762,6 +766,7 @@ module Classifier
762
766
 
763
767
  # Restore settings
764
768
  @auto_rebuild = data['auto_rebuild']
769
+ @min_word_length = data['min_word_length'] || Classifier.config.min_word_length
765
770
  @version += 1
766
771
  @built_at_version = -1
767
772
  @word_list = WordList.new
@@ -1,3 +1,3 @@
1
1
  module Classifier
2
- VERSION = '2.7.0'.freeze
2
+ VERSION = '2.7.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: classifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.0
4
+ version: 2.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lucas Carlson
@@ -229,7 +229,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
229
229
  - !ruby/object:Gem::Version
230
230
  version: '0'
231
231
  requirements: []
232
- rubygems_version: 4.0.16
232
+ rubygems_version: 4.0.20
233
233
  specification_version: 4
234
234
  summary: Text classification with Bayesian, LSI, Logistic Regression, kNN, and TF-IDF
235
235
  vectorization.