tokana 0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 mnaoizy
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,164 @@
1
+ # tokana
2
+
3
+ Modern Japanese morphological analyzer for TypeScript. Works in Node.js and the browser.
4
+
5
+ - Zero runtime dependencies
6
+ - Full TypeScript support with typed tokens per dictionary format
7
+ - ESM and CommonJS
8
+ - Supports IPAdic, UniDic, and NEologd dictionaries
9
+ - Built-in CLI for compiling MeCab-compatible dictionary sources
10
+ - Browser support via `fetch` + `DecompressionStream`
11
+
12
+ ## Install
13
+
14
+ ```bash
15
+ npm install tokana
16
+ ```
17
+
18
+ ## Dictionary Setup
19
+
20
+ tokana requires compiled dictionary files. Use the CLI to compile from MeCab-compatible sources (e.g. [mecab-ipadic](https://taku910.github.io/mecab/)):
21
+
22
+ ```bash
23
+ # Download and extract MeCab IPAdic source, then compile:
24
+ npx tokana build ./mecab-ipadic-2.7.0-20070801 ./dict
25
+ ```
26
+
27
+ This generates `.dat.gz` files in the output directory.
28
+
29
+ ## Usage
30
+
31
+ ### Node.js
32
+
33
+ ```typescript
34
+ import { createTokenizer } from "tokana";
35
+
36
+ const tokenizer = await createTokenizer({
37
+ format: "ipadic",
38
+ dicPath: "./dict",
39
+ });
40
+
41
+ const tokens = tokenizer.tokenize("東京都に住んでいる");
42
+
43
+ for (const token of tokens) {
44
+ console.log(token.surface, token.pos, token.baseForm, token.reading);
45
+ }
46
+ // 東京 名詞 東京 トウキョウ
47
+ // 都 名詞 都 ト
48
+ // に 助詞 に ニ
49
+ // 住ん 動詞 住む スン
50
+ // で 助詞 で デ
51
+ // いる 動詞 いる イル
52
+ ```
53
+
54
+ ### Browser
55
+
56
+ ```typescript
57
+ import { createTokenizer } from "tokana";
58
+
59
+ // dicPath is a URL path — serve .dat.gz files from your static assets
60
+ const tokenizer = await createTokenizer({
61
+ format: "ipadic",
62
+ dicPath: "/dict",
63
+ });
64
+
65
+ const tokens = tokenizer.tokenize("形態素解析");
66
+ ```
67
+
68
+ ## Dictionary Formats
69
+
70
+ | Format | Description | Features |
71
+ |--------|-------------|----------|
72
+ | `ipadic` | MeCab IPAdic (default) | 9 fields: pos, posDetail1-3, conjugationType/Form, baseForm, reading, pronunciation |
73
+ | `unidic` | UniDic | 17 fields: pos1-4, cType/cForm, lemma, orth, pron, goshu, etc. |
74
+ | `neologd` | mecab-ipadic-NEologd | Same as ipadic, with added neologisms |
75
+
76
+ ```typescript
77
+ // UniDic example
78
+ const tokenizer = await createTokenizer({
79
+ format: "unidic",
80
+ dicPath: "./unidic-dict",
81
+ });
82
+ ```
83
+
84
+ ## API
85
+
86
+ ### `createTokenizer(options): Promise<Tokenizer<T>>`
87
+
88
+ Creates a tokenizer instance. Loads and parses dictionary files asynchronously.
89
+
90
+ | Option | Type | Default | Description |
91
+ |--------|------|---------|-------------|
92
+ | `dicPath` | `string` | (required) | Path to compiled dictionary directory |
93
+ | `format` | `"ipadic" \| "unidic" \| "neologd"` | `"ipadic"` | Dictionary format |
94
+
95
+ ### `Tokenizer<T>.tokenize(text: string): T[]`
96
+
97
+ Tokenizes input text into an array of typed token objects.
98
+
99
+ ### Token Types
100
+
101
+ **`IpadicToken`** (ipadic / neologd):
102
+
103
+ ```typescript
104
+ interface IpadicToken {
105
+ surface: string; // Surface form
106
+ pos: string; // Part-of-speech
107
+ posDetail1: string; // POS subcategory 1
108
+ posDetail2: string; // POS subcategory 2
109
+ posDetail3: string; // POS subcategory 3
110
+ conjugationType: string; // Conjugation type
111
+ conjugationForm: string; // Conjugation form
112
+ baseForm: string; // Base form (lemma)
113
+ reading: string; // Reading
114
+ pronunciation: string; // Pronunciation
115
+ // + wordId, type, offset, length, cost
116
+ }
117
+ ```
118
+
119
+ **`UnidicToken`** (unidic):
120
+
121
+ ```typescript
122
+ interface UnidicToken {
123
+ surface: string;
124
+ pos1: string; pos2: string; pos3: string; pos4: string;
125
+ cType: string; cForm: string;
126
+ lForm: string; lemma: string;
127
+ orth: string; orthBase: string;
128
+ pron: string; pronBase: string;
129
+ goshu: string;
130
+ iType: string; iForm: string;
131
+ fType: string; fForm: string;
132
+ // + wordId, type, offset, length, cost
133
+ }
134
+ ```
135
+
136
+ ## CLI
137
+
138
+ ```
139
+ tokana build <source-dir> <output-dir> Compile MeCab dictionary source
140
+ tokana info <dict-dir> Show dictionary info
141
+ ```
142
+
143
+ ### Compile a dictionary
144
+
145
+ ```bash
146
+ npx tokana build ./mecab-ipadic-2.7.0-20070801 ./dict
147
+ ```
148
+
149
+ Reads `matrix.def`, `char.def`, `unk.def`, and `*.csv` from the source directory and outputs compiled `.dat.gz` files.
150
+
151
+ ## Demo
152
+
153
+ A Vite + React demo app is included in [`demo/`](./demo):
154
+
155
+ ```bash
156
+ cd demo
157
+ npm install
158
+ npm run setup-dict # downloads and compiles mecab-ipadic
159
+ npm run dev # http://localhost:5173
160
+ ```
161
+
162
+ ## License
163
+
164
+ [MIT](./LICENSE)
package/dist/cli.js ADDED
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/cli.ts
4
+ var args = process.argv.slice(2);
5
+ var command = args[0];
6
+ async function main() {
7
+ switch (command) {
8
+ case "build":
9
+ case "compile": {
10
+ const { compile } = await import("./compile-VQJF62SJ.js");
11
+ await compile(args.slice(1));
12
+ break;
13
+ }
14
+ case "info": {
15
+ const { info } = await import("./info-OVE32SWZ.js");
16
+ await info(args.slice(1));
17
+ break;
18
+ }
19
+ default:
20
+ console.log(`tokana - Modern Japanese Morphological Analyzer
21
+
22
+ Usage:
23
+ tokana build <source-dir> <output-dir> Compile MeCab dictionary source
24
+ tokana info <dict-dir> Show dictionary info
25
+
26
+ Options:
27
+ --format <ipadic|unidic|neologd> Dictionary format (default: ipadic)
28
+ `);
29
+ if (command && command !== "help" && command !== "--help") {
30
+ console.error(`Unknown command: ${command}`);
31
+ process.exit(1);
32
+ }
33
+ }
34
+ }
35
+ main().catch((err) => {
36
+ console.error(err);
37
+ process.exit(1);
38
+ });
39
+ //# sourceMappingURL=cli.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/cli.ts"],"sourcesContent":["/**\n * CLI entry point for tokana dictionary compiler.\n */\n\nconst args = process.argv.slice(2);\nconst command = args[0];\n\nasync function main() {\n switch (command) {\n case \"build\":\n case \"compile\": {\n const { compile } = await import(\"./cli/commands/compile.js\");\n await compile(args.slice(1));\n break;\n }\n case \"info\": {\n const { info } = await import(\"./cli/commands/info.js\");\n await info(args.slice(1));\n break;\n }\n default:\n console.log(`tokana - Modern Japanese Morphological Analyzer\n\nUsage:\n tokana build <source-dir> <output-dir> Compile MeCab dictionary source\n tokana info <dict-dir> Show dictionary info\n\nOptions:\n --format <ipadic|unidic|neologd> Dictionary format (default: ipadic)\n`);\n if (command && command !== \"help\" && command !== \"--help\") {\n console.error(`Unknown command: ${command}`);\n process.exit(1);\n }\n }\n}\n\nmain().catch((err) => {\n console.error(err);\n process.exit(1);\n});\n"],"mappings":";;;AAIA,IAAM,OAAO,QAAQ,KAAK,MAAM,CAAC;AACjC,IAAM,UAAU,KAAK,CAAC;AAEtB,eAAe,OAAO;AACpB,UAAQ,SAAS;AAAA,IACf,KAAK;AAAA,IACL,KAAK,WAAW;AACd,YAAM,EAAE,QAAQ,IAAI,MAAM,OAAO,uBAA2B;AAC5D,YAAM,QAAQ,KAAK,MAAM,CAAC,CAAC;AAC3B;AAAA,IACF;AAAA,IACA,KAAK,QAAQ;AACX,YAAM,EAAE,KAAK,IAAI,MAAM,OAAO,oBAAwB;AACtD,YAAM,KAAK,KAAK,MAAM,CAAC,CAAC;AACxB;AAAA,IACF;AAAA,IACA;AACE,cAAQ,IAAI;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,CAQjB;AACK,UAAI,WAAW,YAAY,UAAU,YAAY,UAAU;AACzD,gBAAQ,MAAM,oBAAoB,OAAO,EAAE;AAC3C,gBAAQ,KAAK,CAAC;AAAA,MAChB;AAAA,EACJ;AACF;AAEA,KAAK,EAAE,MAAM,CAAC,QAAQ;AACpB,UAAQ,MAAM,GAAG;AACjB,UAAQ,KAAK,CAAC;AAChB,CAAC;","names":[]}