@chaffjs/lang-en 0.0.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.
- package/LICENSE +21 -0
- package/README.md +12 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +34 -0
- package/dist/index.js.map +1 -0
- package/dist/lexicons.d.ts +7 -0
- package/dist/lexicons.d.ts.map +1 -0
- package/dist/lexicons.js +26 -0
- package/dist/lexicons.js.map +1 -0
- package/lexicons/closing-cliche.yaml +8 -0
- package/lexicons/empty-intensifier.yaml +9 -0
- package/lexicons/padded-intro.yaml +8 -0
- package/package.json +41 -0
- package/src/index.ts +36 -0
- package/src/lexicons.ts +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 isamu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# @chaffjs/lang-en
|
|
2
|
+
|
|
3
|
+
Official English adapter for [chaff](https://www.npmjs.com/package/chaffjs). Bundled with `chaff`; you do not need to install it yourself.
|
|
4
|
+
|
|
5
|
+
It provides:
|
|
6
|
+
|
|
7
|
+
- Sentence splitting (handles `Dr.`, `e.g.`, `U.S.`, `$3.50` without breaking the sentence)
|
|
8
|
+
- L2 lexicons (empty intensifiers, padded openings, cliched closings)
|
|
9
|
+
|
|
10
|
+
To add a language: official adapters are `@chaffjs/<lang>`, third-party ones are `chaff-lang-<lang>` — the same split as `@typescript-eslint/*` and `eslint-plugin-*`.
|
|
11
|
+
|
|
12
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAgB,MAAM,gBAAgB,CAAC;AAOpE;;;GAGG;AACH,eAAO,MAAM,OAAO,EAAE,eAsBrB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { split, SentenceSplitterSyntax } from "sentence-splitter";
|
|
2
|
+
import { loadLexicons } from "./lexicons.js";
|
|
3
|
+
// chaff からは型だけを取る。実行時の値依存を作らない。アダプタは単体で動く。
|
|
4
|
+
const LATIN_LETTER = /[a-z]/giu;
|
|
5
|
+
const COUNTABLE = /\S/gu;
|
|
6
|
+
/**
|
|
7
|
+
* 英語は sentence-splitter の既定で足りる。"Dr." "e.g." "U.S." "$3.50" を
|
|
8
|
+
* いずれも文末と誤認しない。日本語のような後処理は要らない。spec §7.2。
|
|
9
|
+
*/
|
|
10
|
+
export const adapter = {
|
|
11
|
+
kind: "language",
|
|
12
|
+
id: "en",
|
|
13
|
+
apiVersion: 1,
|
|
14
|
+
capabilities: {
|
|
15
|
+
sentenceSplit: true,
|
|
16
|
+
wordSplit: true,
|
|
17
|
+
pos: false,
|
|
18
|
+
lemma: false,
|
|
19
|
+
lengthUnit: "word",
|
|
20
|
+
},
|
|
21
|
+
detect: (source) => {
|
|
22
|
+
const total = [...source.matchAll(COUNTABLE)].length;
|
|
23
|
+
if (total === 0)
|
|
24
|
+
return 0;
|
|
25
|
+
return [...source.matchAll(LATIN_LETTER)].length / total;
|
|
26
|
+
},
|
|
27
|
+
lexicons: loadLexicons(),
|
|
28
|
+
segment: (text) => ({
|
|
29
|
+
sentences: split(text)
|
|
30
|
+
.filter((node) => node.type === SentenceSplitterSyntax.Sentence)
|
|
31
|
+
.map((node) => ({ span: { start: node.range[0], end: node.range[1] }, text: text.slice(node.range[0], node.range[1]) })),
|
|
32
|
+
}),
|
|
33
|
+
};
|
|
34
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAClE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAG7C,2CAA2C;AAE3C,MAAM,YAAY,GAAG,UAAU,CAAC;AAChC,MAAM,SAAS,GAAG,MAAM,CAAC;AAEzB;;;GAGG;AACH,MAAM,CAAC,MAAM,OAAO,GAAoB;IACtC,IAAI,EAAE,UAAU;IAChB,EAAE,EAAE,IAAI;IACR,UAAU,EAAE,CAAC;IACb,YAAY,EAAE;QACZ,aAAa,EAAE,IAAI;QACnB,SAAS,EAAE,IAAI;QACf,GAAG,EAAE,KAAK;QACV,KAAK,EAAE,KAAK;QACZ,UAAU,EAAE,MAAM;KACnB;IACD,MAAM,EAAE,CAAC,MAAc,EAAU,EAAE;QACjC,MAAM,KAAK,GAAG,CAAC,GAAG,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC;QACrD,IAAI,KAAK,KAAK,CAAC;YAAE,OAAO,CAAC,CAAC;QAC1B,OAAO,CAAC,GAAG,MAAM,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC,CAAC,MAAM,GAAG,KAAK,CAAC;IAC3D,CAAC;IACD,QAAQ,EAAE,YAAY,EAAE;IACxB,OAAO,EAAE,CAAC,IAAY,EAAgB,EAAE,CAAC,CAAC;QACxC,SAAS,EAAE,KAAK,CAAC,IAAI,CAAC;aACnB,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,KAAK,sBAAsB,CAAC,QAAQ,CAAC;aAC/D,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;KAC3H,CAAC;CACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lexicons.d.ts","sourceRoot":"","sources":["../src/lexicons.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,OAAO,EAAgB,MAAM,gBAAgB,CAAC;AAY5D;;;GAGG;AACH,eAAO,MAAM,YAAY,GAAI,MAAK,MAAY,KAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAQ7D,CAAC"}
|
package/dist/lexicons.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { readFileSync, readdirSync } from "node:fs";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { parse } from "yaml";
|
|
5
|
+
const DIR = join(dirname(fileURLToPath(import.meta.url)), "..", "lexicons");
|
|
6
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
7
|
+
const toEntry = (raw) => {
|
|
8
|
+
if (!isRecord(raw) || typeof raw["pattern"] !== "string")
|
|
9
|
+
return undefined;
|
|
10
|
+
const weight = raw["weight"];
|
|
11
|
+
return { pattern: raw["pattern"], weight: typeof weight === "number" ? weight : undefined };
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* 語彙表はアダプタが持つ。detector は共通で、これだけが言語別。spec §11。
|
|
15
|
+
* 新しい言語のサポートは、ここを書くところから始まる。
|
|
16
|
+
*/
|
|
17
|
+
export const loadLexicons = (dir = DIR) => readdirSync(dir)
|
|
18
|
+
.filter((file) => file.endsWith(".yaml"))
|
|
19
|
+
.reduce((acc, file) => {
|
|
20
|
+
const raw = parse(readFileSync(join(dir, file), "utf8"));
|
|
21
|
+
if (!isRecord(raw) || typeof raw["id"] !== "string" || !Array.isArray(raw["entries"]))
|
|
22
|
+
return acc;
|
|
23
|
+
const entries = raw["entries"].map(toEntry).filter((entry) => entry !== undefined);
|
|
24
|
+
return { ...acc, [raw["id"]]: entries };
|
|
25
|
+
}, {});
|
|
26
|
+
//# sourceMappingURL=lexicons.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lexicons.js","sourceRoot":"","sources":["../src/lexicons.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,KAAK,EAAE,MAAM,MAAM,CAAC;AAG7B,MAAM,GAAG,GAAG,I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AClG,MAAM,OAAO,GAAG,GAAG,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC;IACnF,OAAO,EAAE,GAAG,GAAG,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC;AAC1C,CAAC,EAAE,EAAE,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@chaffjs/lang-en",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "English language adapter for chaff",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "isamu",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./src/index.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"LICENSE",
|
|
16
|
+
"README.md",
|
|
17
|
+
"dist",
|
|
18
|
+
"lexicons",
|
|
19
|
+
"src"
|
|
20
|
+
],
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=24"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsc -p tsconfig.build.json",
|
|
26
|
+
"prepack": "yarn build"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"sentence-splitter": "^5.0.1",
|
|
30
|
+
"yaml": "^2.9.0"
|
|
31
|
+
},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/isamu/lab.git",
|
|
35
|
+
"directory": "text/packages/lang-en"
|
|
36
|
+
},
|
|
37
|
+
"homepage": "https://github.com/isamu/lab/tree/main/text",
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { split, SentenceSplitterSyntax } from "sentence-splitter";
|
|
2
|
+
import { loadLexicons } from "./lexicons.ts";
|
|
3
|
+
import type { LanguageAdapter, Segmentation } from "chaffjs/plugin";
|
|
4
|
+
|
|
5
|
+
// chaff からは型だけを取る。実行時の値依存を作らない。アダプタは単体で動く。
|
|
6
|
+
|
|
7
|
+
const LATIN_LETTER = /[a-z]/giu;
|
|
8
|
+
const COUNTABLE = /\S/gu;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 英語は sentence-splitter の既定で足りる。"Dr." "e.g." "U.S." "$3.50" を
|
|
12
|
+
* いずれも文末と誤認しない。日本語のような後処理は要らない。spec §7.2。
|
|
13
|
+
*/
|
|
14
|
+
export const adapter: LanguageAdapter = {
|
|
15
|
+
kind: "language",
|
|
16
|
+
id: "en",
|
|
17
|
+
apiVersion: 1,
|
|
18
|
+
capabilities: {
|
|
19
|
+
sentenceSplit: true,
|
|
20
|
+
wordSplit: true,
|
|
21
|
+
pos: false,
|
|
22
|
+
lemma: false,
|
|
23
|
+
lengthUnit: "word",
|
|
24
|
+
},
|
|
25
|
+
detect: (source: string): number => {
|
|
26
|
+
const total = [...source.matchAll(COUNTABLE)].length;
|
|
27
|
+
if (total === 0) return 0;
|
|
28
|
+
return [...source.matchAll(LATIN_LETTER)].length / total;
|
|
29
|
+
},
|
|
30
|
+
lexicons: loadLexicons(),
|
|
31
|
+
segment: (text: string): Segmentation => ({
|
|
32
|
+
sentences: split(text)
|
|
33
|
+
.filter((node) => node.type === SentenceSplitterSyntax.Sentence)
|
|
34
|
+
.map((node) => ({ span: { start: node.range[0], end: node.range[1] }, text: text.slice(node.range[0], node.range[1]) })),
|
|
35
|
+
}),
|
|
36
|
+
};
|
package/src/lexicons.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { readFileSync, readdirSync } from "node:fs";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { parse } from "yaml";
|
|
5
|
+
import type { Lexicon, LexiconEntry } from "chaffjs/plugin";
|
|
6
|
+
|
|
7
|
+
const DIR = join(dirname(fileURLToPath(import.meta.url)), "..", "lexicons");
|
|
8
|
+
|
|
9
|
+
const isRecord = (value: unknown): value is Record<string, unknown> => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
10
|
+
|
|
11
|
+
const toEntry = (raw: unknown): LexiconEntry | undefined => {
|
|
12
|
+
if (!isRecord(raw) || typeof raw["pattern"] !== "string") return undefined;
|
|
13
|
+
const weight = raw["weight"];
|
|
14
|
+
return { pattern: raw["pattern"], weight: typeof weight === "number" ? weight : undefined };
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 語彙表はアダプタが持つ。detector は共通で、これだけが言語別。spec §11。
|
|
19
|
+
* 新しい言語のサポートは、ここを書くところから始まる。
|
|
20
|
+
*/
|
|
21
|
+
export const loadLexicons = (dir: string = DIR): Record<string, Lexicon> =>
|
|
22
|
+
readdirSync(dir)
|
|
23
|
+
.filter((file) => file.endsWith(".yaml"))
|
|
24
|
+
.reduce<Record<string, Lexicon>>((acc, file) => {
|
|
25
|
+
const raw: unknown = parse(readFileSync(join(dir, file), "utf8"));
|
|
26
|
+
if (!isRecord(raw) || typeof raw["id"] !== "string" || !Array.isArray(raw["entries"])) return acc;
|
|
27
|
+
const entries = raw["entries"].map(toEntry).filter((entry) => entry !== undefined);
|
|
28
|
+
return { ...acc, [raw["id"]]: entries };
|
|
29
|
+
}, {});
|