which-dialect 0.5.11 → 0.5.12

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.
Files changed (2) hide show
  1. package/dist/detect.js +8 -9
  2. package/package.json +1 -1
package/dist/detect.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // Which language each word of a mixed text is in: the target language or the learner's own (the base,
2
2
  // English by default), from the two languages' data alone. Shared by the checker and the journal review.
3
- import { plain } from "./text.js";
3
+ import { plain, syllablesByPlain } from "./text.js";
4
4
  /**
5
5
  * Marks each unit (of one sentence) with `lang` or `base`. A unit the target knows and the base doesn't
6
6
  * is the target's, and the other way round. A unit both know ("em", "an", "to") goes by how common it is
@@ -26,7 +26,10 @@ const BASE_MARGIN_ACCENTS = 1;
26
26
  export async function markLanguages(list, o) {
27
27
  const syllables = await o.target.syllables();
28
28
  const syllableLanguage = Object.keys(syllables).length > 0;
29
- const plainSyllables = syllableLanguage ? new Set(Object.keys(syllables).map(plain)) : new Set();
29
+ // Each syllable's accented forms by their plain letters, most common first: built once per language
30
+ // (syllablesByPlain caches it), since this runs for every sentence and stripping the accents from all
31
+ // ~7,000 Vietnamese syllables each time was a fifth of a journal check's CPU (2026-11-01).
32
+ const byPlain = syllablesByPlain(syllables);
30
33
  // 'exact': a target word as written; 'accents': one only with accents added ("khong" → "không").
31
34
  const targetKnows = async (u) => {
32
35
  const lower = u.text.toLowerCase();
@@ -38,7 +41,7 @@ export async function markLanguages(list, o) {
38
41
  }
39
42
  if (lower in syllables || lower !== plain(lower))
40
43
  return 'exact';
41
- return plainSyllables.has(lower) ? 'accents' : false;
44
+ return byPlain.has(lower) ? 'accents' : false;
42
45
  };
43
46
  const baseKnows = async (u) => (await o.translator.senses(u.text, { from: o.base }).catch(() => [])).length > 0;
44
47
  const marks = await Promise.all(list.map(async (u) => {
@@ -55,12 +58,8 @@ export async function markLanguages(list, o) {
55
58
  const accentedFrequency = async (u) => {
56
59
  if (!syllableLanguage)
57
60
  return (await o.target.variants(u.text.toLowerCase(), { limit: 1 }))[0]?.frequency ?? 0;
58
- const bare = plain(u.text.toLowerCase());
59
- let best = 0;
60
- for (const [s, f] of Object.entries(syllables))
61
- if (f > best && plain(s) === bare)
62
- best = f;
63
- return best;
61
+ const forms = byPlain.get(plain(u.text.toLowerCase()));
62
+ return forms ? Math.max(0, syllables[forms[0]]) : 0;
64
63
  };
65
64
  // How common a word is in each language (Zipf; 0 when unknown).
66
65
  const targetFrequency = async (u) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "which-dialect",
3
- "version": "0.5.11",
3
+ "version": "0.5.12",
4
4
  "description": "Translate words between languages and dialects, meaning by meaning, starting with Vietnamese (Northern, Central, Southern), Spanish (by country) and English, with Wiktionary data loaded on demand. More languages are planned.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",