@desert-ant-labs/tongue

中文文档
4.3.0发布于 2026/11/06SEE LICENSE IN LICENSE.mdTypeScriptES Module零依赖近 30 天下载 8517

On-device language identification for short text, across 84 languages. Pure JavaScript: no wasm, no inference runtime.

原始使用文档
文档目录8 节
  1. Install
  2. Saying "I don't know"
  3. API
  4. CommonJS and TypeScript
  5. Serving the model in a browser
  6. The cross-platform contract
  7. Model
  8. License

tongue — JavaScript / TypeScript SDK

On-device language identification for short text, across 84 languages. Runs in the browser and in Node from one import.

import { Tongue } from "@desert-ant-labs/tongue";

const tongue = await Tongue.load();
tongue.detect("kann ich das haben").language;   // "de"
tongue.detect("안녕하세요").language;             // "ko"

No wasm, no inference runtime, no dependencies. A detection is an int8 embedding gather, a sum, one 59×32 matmul and a masked softmax — a few thousand multiply-adds in plain JavaScript. That is why this package has a single entry point where emo-js needs separate browser and Node builds: there is no runtime to swap, only two ways to read 2 MB of weights.

Install

npm i @desert-ant-labs/tongue

Node ≥18, or any modern browser.

// Node: reads the bundled weights from the package directory.
const tongue = await Tongue.load();

// Browser: serve tongue_int8.bin and tongue_meta.json and point at them.
const tongue = await Tongue.load({ from: "/models/tongue" });

// Or supply the bytes yourself.
const tongue = Tongue.fromBytes(metadata, weightBytes);

Saying "I don't know"

Short input is often genuinely undecidable, and the SDK says so rather than guessing. Reliability is keyed off evidence — input length and how far the top candidate leads the runner-up — not raw softmax confidence, which is badly overconfident on two words.

const detection = tongue.detect("la casa");
detection.isTooCloseToCall;   // true — equally Italian and Spanish
detection.reliability;        // "tentative"
detection.candidates;         // [it 0.31, es 0.29, …]

Present both when isTooCloseToCall is set. Treat "tentative" as "unknown" rather than as an answer, and ask for more text where the product allows it.

API

Tongue.load(options?)load the model; options.from is a directory or base URL
Tongue.fromBytes(metadata, bytes)load from bytes you already have
detect(text, topK?)Detection
Detection.languagetop candidate, or null on empty input
Detection.candidatesPrediction[] with probabilities
Detection.reliability"confident" · "likely" · "tentative" · "empty"
Detection.isTooCloseToCalltop two ranked within 0.12, whatever topK is
Detection.route.verdict"decisive" (script alone settled it) · "narrowing" · "ambiguous"

normalize, route, fnv1a and buckets are also exported, for anyone reproducing the feature pipeline.

CommonJS and TypeScript

The package is ESM-only. require() of it works natively on Node 20.19+, so a CJS server needs no changes at runtime. TypeScript in a CJS project should set "moduleResolution": "nodenext" (TS 5.8+) — the older node16 setting rejects the import with TS1479 even though the emitted require() runs. On Node older than 20.19, use a dynamic import().

Serving the model in a browser

On Node the weights load out of the package with no configuration. A bundler does not serve files from node_modules, so in a browser you serve the two model files yourself and point load at them:

const tongue = await Tongue.load({ from: "/models/tongue" });

Both files are exported, so a bundler can fingerprint and hash them rather than needing a copy step:

import binUrl from "@desert-ant-labs/tongue/model/tongue_int8.bin?url";   // Vite
import metaUrl from "@desert-ant-labs/tongue/model/tongue_meta.json?url";

Or copy node_modules/@desert-ant-labs/tongue/dist/tongue_{int8.bin,meta.json} into your static directory as a build step.

Calling Tongue.load() with no from in a browser resolves against the page URL, which on a single-page app hits the history fallback and returns index.html. The error says so explicitly if it happens.

The cross-platform contract

The normalizer, hasher and router are a frozen specification shared with the Python reference and the Swift and Kotlin SDKs. test/golden.test.js replays the same vector files all of them do, including whole-bag hasher equality:

npm test

Regenerate the Unicode tables rather than editing them:

python3 scripts/gen_ts_tables.py --reference ../tongue-training

Model

Weights, benchmarks and limitations: huggingface.co/desert-ant-labs/tongue. Live demo: desert-ant-labs-tongue-demo.static.hf.space.

Read the model card's failure modes before deploying: one or two words is often genuinely ambiguous, Malay and Indonesian are not reliably separable at this size, Mongolian works only in the traditional script, and brand names and version strings are not language at all.

License

Desert Ant Labs Source-Available License. Free for most apps, and a commercial license is required at scale. Full terms are at the link. Licensing: [email protected].

Third-party notices for the SDK: THIRD_PARTY_NOTICES.md.

Third-party notices for the weights: THIRD_PARTY_NOTICES.md.

版本与发布记录

@desert-ant-labs/tongue 共发布 19 个版本,首次发布于 2026/07/28;过去 12 个月发布了 19 个版本。

版本发布日期距今
4.3.0latest2026/11/066 天前
4.2.32026/10/3112 天前
4.2.22026/10/3112 天前
4.2.12026/10/2815 天前
4.2.02026/10/2716 天前
4.1.02026/10/2023 天前

关于 @desert-ant-labs/tongue 的常见问题

@desert-ant-labs/tongue 是什么?

On-device language identification for short text, across 84 languages. Pure JavaScript: no wasm, no inference runtime.

如何安装 @desert-ant-labs/tongue?

在项目目录运行 npm install @desert-ant-labs/tongue,或使用 pnpm add / yarn add / bun add。

@desert-ant-labs/tongue 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@desert-ant-labs/tongue 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@desert-ant-labs/tongue 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@desert-ant-labs/tongue 可以商用吗?

@desert-ant-labs/tongue 使用 SEE LICENSE IN LICENSE.md 许可。许可条款写在包内的独立文件中,请阅读该文件确认可否商用。以上为概括,不构成法律意见,请以许可原文为准。

@desert-ant-labs/tongue 的下载量有多少?

根据 npm 官方下载统计,@desert-ant-labs/tongue 在 2026/10/12 – 2026/11/10 这 30 天内被下载 8,517 次,日均约 284 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@desert-ant-labs/tongue 还在维护吗?

最新版本 4.3.0 发布于 2026/11/06(6 天前);至今共发布 19 个版本,其中过去 12 个月发布了 19 个;首次发布于 2026/07/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。