whatlanguage 3.0.0 → 3.1.0

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: 93967e970b5e62eb58b9cb835866d4abe885f98761f7a339d0817dad8414eac8
4
- data.tar.gz: b7e983bcd0a56f7b6f77675fdedd6d7724da71b42e26e3229014a92a3c32166b
3
+ metadata.gz: 76f240c6854e63eb9a9356ece7e04d8488ba25cf1a62129f36a8f90dd0786875
4
+ data.tar.gz: 2c402b582ffba0da27a402a921bd72785c5ec71d297236c1f702a2e0b08d7d1e
5
5
  SHA512:
6
- metadata.gz: f7c259a244e6c0fe0aa9084037fb29381bc96c9b5a4e549382b660b95943d82bf651d5e706fc4290cb93ff2bc820b1c15495f70bec025c5b98bfa3fb0ff91d6a
7
- data.tar.gz: 6b2d67686a98ec148d4a3f3314cd270beee7ad3b3437411b8a516d69d6078d3a1a3bd230a4d7e06a5904f7b286339e8be1b2cfef29ba9df73e0da76efb0044a3
6
+ metadata.gz: 20c7be1cfb4148e3c00d426092424dab4b1a90ce74679fb0e80cc55be42d0288d025369010c513231bf86d42837d63cac872d434a7b62ce4b3f0192775bae246
7
+ data.tar.gz: c651b1dee1b791afb78cc1c375a9c74022d8e5b15809cfd8686ccdb27c555a7913fc22b66801a959f102cc6c7b832a83730f74c13fce3705f685ee5cd5083438
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 3.1.0 / 2026-12-02
4
+
5
+ - Give equally frequent input trigrams the same rank, fixing an English sentence
6
+ misclassified as Latin (#52). Detection scores and rankings can change for the
7
+ same input.
8
+ - Correct ASCII trigram counts on TruffleRuby 40.1.
9
+
3
10
  ## 3.0.0 / 2026-10-20
4
11
 
5
12
  ### Breaking change: nine ISO output codes
data/README.md CHANGED
@@ -28,11 +28,11 @@ Digits, punctuation, and emoji do not count toward the minimum text length and a
28
28
 
29
29
  The trigram profiles are vendored from [whatlang](https://github.com/greyblake/whatlang-rs), a port of [Franc](https://github.com/wooorm/franc), whose models are built from the public-domain UDHR corpus (see Credits). The model is a ~220 KB JSON file.
30
30
 
31
- Repeated calls over the full language set use a shared trigram index. A rolling
32
- window counts input trigrams without building a character array. Scores and
33
- rankings are unchanged. See the [indexing benchmarks](benchmark/README.md) and
34
- [long-input benchmarks](benchmark/long-inputs.md) for timings, allocations, and
35
- memory costs.
31
+ Input trigrams with the same frequency share a rank. Repeated calls over the full
32
+ language set use a shared trigram index, and a rolling window counts input trigrams
33
+ without building a character array. See the [indexing benchmarks](benchmark/README.md)
34
+ and [long-input benchmarks](benchmark/long-inputs.md) for timings, allocations,
35
+ and memory costs.
36
36
 
37
37
  ## Usage
38
38
 
@@ -44,8 +44,8 @@ text = "Die Stadt plant neue Investitionen in den öffentlichen Verkehr"
44
44
  result = wl.detect(text)
45
45
  result.language # => :german
46
46
  result.iso # => :de
47
- result.score # => 79018
48
- result.ranked # => [[:german, 79018], [:dutch, 77631], ... ]
47
+ result.score # => 79140
48
+ result.ranked # => [[:german, 79140], [:dutch, 77600], ... ]
49
49
  ```
50
50
 
51
51
  `result.iso` and `language_iso` return ISO 639-1 symbols with an ISO 639-3
@@ -61,8 +61,8 @@ the language names.
61
61
  Return ranked scores, or the score hash:
62
62
 
63
63
  ```ruby
64
- wl.ranked(text) # => [[:german, 79018], [:dutch, 77631], ... ]
65
- wl.score_hash(text) # => { german: 79018, dutch: 77631, ... }
64
+ wl.ranked(text) # => [[:german, 79140], [:dutch, 77600], ... ]
65
+ wl.score_hash(text) # => { german: 79140, dutch: 77600, ... }
66
66
  ```
67
67
 
68
68
  Restrict candidate languages:
@@ -76,10 +76,9 @@ a built-in preference. These five were selected using the
76
76
  [2025 total-speaker ranking](https://www.visualcapitalist.com/ranked-the-worlds-most-spoken-languages-in-2025/)
77
77
  (including second-language speakers). Eligible trigram candidates receive the
78
78
  same bonus: 600 ranking points through 10 letters, decreasing linearly to zero
79
- at 50 letters. Longer texts keep their original scores. The preference is not
80
- configurable; it respects `only:` and script detection. Chinese already resolves
81
- from its script, so it does not need a statistical bonus. Scores exposed by the
82
- API include the bonus and remain ranking values, not probabilities.
79
+ at 50 letters. The fixed preference respects `only:` and script detection.
80
+ Chinese resolves directly from its script. API scores include the bonus and
81
+ indicate relative rank.
83
82
 
84
83
  Short shared-script fragments are ignored by default because there is not enough signal to rank their languages reliably. The threshold applies to the statistical trigram stage; scripts that identify a single supported language, such as Greek, Korean, or Thai, can still resolve from shorter text. The threshold can be adjusted:
85
84
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class WhatLanguage
4
- VERSION = '3.0.0'
4
+ VERSION = '3.1.0'
5
5
  end
data/lib/whatlanguage.rb CHANGED
@@ -288,9 +288,10 @@ class WhatLanguage
288
288
  [counts.max_by { |_name, n| n }.first, letter_count]
289
289
  end
290
290
 
291
- # Text trigrams ranked by descending frequency, mapped to their rank index.
292
- # Non-letter/mark characters become spaces. Preserve combining marks used
293
- # by the profiles; bound the stream by spaces and ignore repeated spaces.
291
+ # Text trigrams ranked by descending frequency. Ties share their midpoint
292
+ # rank so alphabetical order cannot affect their relative weight. The
293
+ # secondary sort keeps the selection deterministic at the trigram limit.
294
+ # Non-letter/mark characters become spaces, with repeated spaces collapsed.
294
295
  def trigram_positions(text)
295
296
  text = text.downcase
296
297
  occurrences = text.ascii_only? ? ascii_trigram_counts(text) : unicode_trigram_counts(text)
@@ -298,8 +299,20 @@ class WhatLanguage
298
299
  order = b[1] <=> a[1]
299
300
  order.zero? ? b[0] <=> a[0] : order
300
301
  end.first(TEXT_TRIGRAMS_SIZE)
302
+
301
303
  positions = {}
302
- ranked.each_with_index { |(trigram, _count), i| positions[trigram] = i }
304
+ start = 0
305
+ while start < ranked.size
306
+ finish = start + 1
307
+ frequency = ranked[start][1]
308
+ finish += 1 while finish < ranked.size && ranked[finish][1] == frequency
309
+
310
+ midpoint = (start + finish - 1) / 2
311
+ while start < finish
312
+ positions[ranked[start][0]] = midpoint
313
+ start += 1
314
+ end
315
+ end
303
316
  positions
304
317
  end
305
318
 
@@ -327,31 +340,29 @@ class WhatLanguage
327
340
  occurrences
328
341
  end
329
342
 
330
- # Lowercased ASCII has only a-z letters and no combining marks. Use byte
331
- # values and a reusable three-byte key to avoid allocating character strings.
343
+ # Count ASCII trigrams as packed integers. Decode each distinct key after
344
+ # counting to avoid mutating a string used for hash lookups.
332
345
  def ascii_trigram_counts(text)
333
346
  occurrences = Hash.new(0)
334
- trigram = +"\0\0\0"
335
347
  c1 = nil
336
348
  c2 = 32
337
349
  text.each_byte do |byte|
338
350
  c3 = byte.between?(97, 122) ? byte : 32
339
351
  if c1 && !(c2 == 32 && (c1 == 32 || c3 == 32))
340
- trigram.setbyte(0, c1)
341
- trigram.setbyte(1, c2)
342
- trigram.setbyte(2, c3)
352
+ trigram = (c1 << 16) | (c2 << 8) | c3
343
353
  occurrences[trigram] += 1
344
354
  end
345
355
  c1 = c2
346
356
  c2 = c3
347
357
  end
348
358
  if c1 && c2 != 32
349
- trigram.setbyte(0, c1)
350
- trigram.setbyte(1, c2)
351
- trigram.setbyte(2, 32)
359
+ trigram = (c1 << 16) | (c2 << 8) | 32
352
360
  occurrences[trigram] += 1
353
361
  end
354
- occurrences
362
+
363
+ occurrences.transform_keys! do |trigram|
364
+ [trigram >> 16, (trigram >> 8) & 0xff, trigram & 0xff].pack('C3')
365
+ end
355
366
  end
356
367
 
357
368
  # Out-of-place distance between a language's ordered trigram profile and the
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: whatlanguage
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Cooper
@@ -74,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  requirements: []
77
- rubygems_version: 4.0.6
77
+ rubygems_version: 4.0.20
78
78
  specification_version: 4
79
79
  summary: Natural language detection for text samples
80
80
  test_files: []