### TypeScript Types for Kuromojin Tokenizer and Tokens Source: https://context7.com/azu/kuromojin/llms.txt Demonstrates the usage of Kuromojin's TypeScript types for creating, configuring, and utilizing tokenizers, as well as processing analyzed tokens with guaranteed type safety. Includes examples of async operations and type assertions for token properties. ```typescript import { getTokenizer, tokenize, Tokenizer, KuromojiToken, getTokenizerOption } from "kuromojin"; // Type-safe tokenizer usage async function analyzeText(text: string): Promise[]>> { return tokenize(text); } // Custom tokenizer with options const options: getTokenizerOption = { dicPath: "https://cdn.jsdelivr.net/npm/kuromoji@0.1.2/dict", noCacheTokenize: false }; // Typed token processing analyzeText("型安全な解析").then((tokens) => { tokens.forEach((token: Readonly) => { // TypeScript ensures all properties exist const info: string = `${token.surface_form} [${token.pos}]`; const pronunciation: string = token.pronunciation; console.log(info, pronunciation); }); }); // Type-safe tokenizer instance let tokenizer: Tokenizer; getTokenizer(options).then(t => { tokenizer = t; const result: KuromojiToken[] = tokenizer.tokenize("テスト"); result.forEach(token => { // All KuromojiToken properties are typed const wordId: number = token.word_id; const wordType: "KNOWN" | "UNKNOWN" = token.word_type; const pos: string = token.pos; }); }); ``` -------------------------------- ### Initialize Japanese Tokenizer with kuromojin Source: https://context7.com/azu/kuromojin/llms.txt Initializes and retrieves a kuromoji.js tokenizer instance. It handles dictionary loading, caches the tokenizer globally, and supports custom dictionary paths via configuration or environment variables. Concurrent calls will return the same instance. ```typescript import { getTokenizer } from "kuromojin"; // Basic usage - uses default dictionary path getTokenizer().then(tokenizer => { const tokens = tokenizer.tokenize("これはテストです"); console.log(tokens); // Output: Array of token objects with linguistic information }); // Using custom dictionary path from CDN getTokenizer({ dicPath: "https://cdn.jsdelivr.net/npm/kuromoji@0.1.2/dict" }).then(tokenizer => { const tokens = tokenizer.tokenizeForSentence("日本語を解析する"); console.log(tokens); }).catch(error => { console.error("Failed to load tokenizer:", error); }); // Multiple concurrent calls return the same tokenizer instance Promise.all([ getTokenizer(), getTokenizer(), getTokenizer() ]).then(([t1, t2, t3]) => { console.log(t1 === t2 && t2 === t3); // true - same instance }); // Environment variable configuration (Node.js) // Set KUROMOJIN_DIC_PATH=/path/to/dict before running process.env.KUROMOJIN_DIC_PATH = "./local/dict"; getTokenizer().then(tokenizer => { // Uses dictionary from ./local/dict }); ``` -------------------------------- ### Initialize Japanese Tokenizer (getTokenizer) Source: https://context7.com/azu/kuromojin/llms.txt Initializes and returns a Kuromoji.js tokenizer instance. This function handles dictionary loading, caches the instance globally, and ensures efficient dictionary usage. It can accept an optional configuration object for custom dictionary paths. ```APIDOC ## GET /getTokenizer ### Description Initializes and returns a Kuromoji.js tokenizer instance. This function handles dictionary loading, caches the instance globally, and ensures efficient dictionary usage. It can accept an optional configuration object for custom dictionary paths. ### Method GET ### Endpoint /getTokenizer ### Parameters #### Query Parameters - **dicPath** (string) - Optional - Specifies a custom dictionary path for CDN or local dictionaries. ### Request Example ```javascript import { getTokenizer } from "kuromojin"; // Basic usage getTokenizer().then(tokenizer => { const tokens = tokenizer.tokenize("これはテストです"); console.log(tokens); }); // Using custom dictionary path getTokenizer({ dicPath: "https://cdn.jsdelivr.net/npm/kuromoji@0.1.2/dict" }).then(tokenizer => { const tokens = tokenizer.tokenizeForSentence("日本語を解析する"); console.log(tokens); }).catch(error => { console.error("Failed to load tokenizer:", error); }); ``` ### Response #### Success Response (200) - **tokenizer** (object) - A Kuromoji.js tokenizer instance with a `tokenize` method. #### Response Example ```json { "tokens": [ { "surface_form": "これ", "part_of_speech": "名詞,代名詞,一般", "infl_type": "", "infl_form": "", "base_form": "これ", "reading": "コレ", "pronunciation": "コレ" }, { "surface_form": "は", "part_of_speech": "助詞,係助詞", "infl_type": "", "infl_form": "", "base_form": "は", "reading": "ハ", "pronunciation": "ワ" } ] } ``` ``` -------------------------------- ### Configure kuromojin Dictionary Path (JavaScript) Source: https://github.com/azu/kuromojin/blob/master/README.md Allows setting a default dictionary path for kuromojin. This can be done globally via `window.kuromojin.dicPath` or passed as an option to `getTokenizer`. This is useful for specifying CDN paths or custom dictionary locations. ```javascript import { getTokenizer } from "kuromojin"; // Affect all module that are used kuromojin. window.kuromojin = { dicPath: "https://cdn.jsdelivr.net/npm/kuromoji@0.1.2/dict" }; // this `getTokenizer` function use "https://kuromojin.netlify.com/dict" getTokenizer(); // === getTokenizer({dicPath: "https://cdn.jsdelivr.net/npm/kuromoji@0.1.2/dict"}) ``` -------------------------------- ### Configure Kuromojin Global Dictionary Path (Browser) Source: https://context7.com/azu/kuromojin/llms.txt Sets a global default dictionary path for all kuromojin instances within a browser environment by configuring `window.kuromojin.dicPath`. This avoids the need to pass the `dicPath` option to every function call. Individual calls can still override this global setting. ```javascript // Set global dictionary path before using kuromojin window.kuromojin = { dicPath: "https://cdn.jsdelivr.net/npm/kuromoji@0.1.2/dict" }; import { getTokenizer, tokenize } from "kuromojin"; // Uses the globally configured dictionary path getTokenizer().then(tokenizer => { // tokenizer loaded from CDN path }); tokenize("テキスト解析").then(tokens => { // Uses CDN dictionary automatically console.log(tokens); }); // Override global configuration per call tokenize("特別な処理", { dicPath: "https://kuromojin.netlify.com/dict" }).then(tokens => { // Uses custom path instead of global configuration }); // HTML integration example // // ``` -------------------------------- ### Backward Compatibility for Older kuromojin Versions (JavaScript) Source: https://github.com/azu/kuromojin/blob/master/README.md Demonstrates how to handle backward compatibility for kuromojin versions prior to v2.0.0, where `tokenize` was the default export. It recommends using the named import `tokenize` for modern usage. ```javascript import kuromojin from "kuromojin"; // kuromojin === tokenize // Recommended: use `import {tokenize} from "kuromojin"` instead of it import { tokenize } from "kuromojin"; ``` -------------------------------- ### Tokenize Japanese Text with Kuromojin (TypeScript) Source: https://context7.com/azu/kuromojin/llms.txt Analyzes Japanese text into morphological tokens using the kuromojin library. It returns a Promise resolving to an array of token objects, each containing detailed linguistic information. Caching is enabled by default with an LRU cache of 10,000 entries for performance. Options can disable caching or specify dictionary paths. ```typescript import { tokenize } from "kuromojin"; // Basic tokenization tokenize("黒文字は日本の植物です").then(tokens => { console.log(tokens); /* [ // ... token details ... ] */ }); // Disable caching for unique texts tokenize("一度だけ処理する文章", { dicPath: "https://cdn.jsdelivr.net/npm/kuromoji@0.1.2/dict", noCacheTokenize: true }).then(tokens => { // Result not cached tokens.forEach(token => { console.log(`${token.surface_form}: ${token.pos} (${token.reading})`); }); }); // Processing multiple texts efficiently const texts = ["こんにちは", "ありがとう", "さようなら"]; Promise.all(texts.map(text => tokenize(text))).then(results => { results.forEach((tokens, index) => { console.log(`Text ${index + 1}:`, tokens.map(t => t.surface_form).join(" | ")); }); }); // Extract readings for furigana generation tokenize("東京に行きました").then(tokens => { const furigana = tokens.map(token => ({ word: token.surface_form, reading: token.reading !== "*" ? token.reading : token.surface_form })); console.log(furigana); // [{ word: '東京', reading: 'トウキョウ' }, { word: 'に', reading: 'に' }, ...] }); // Filter tokens by part of speech tokenize("美しい花が咲いている").then(tokens => { const nouns = tokens.filter(token => token.pos === "名詞"); const verbs = tokens.filter(token => token.pos === "動詞"); console.log("Nouns:", nouns.map(t => t.surface_form)); // ['花'] console.log("Verbs:", verbs.map(t => t.basic_form)); // ['咲く'] }); ``` -------------------------------- ### Tokenize Japanese Text with kuromojin (JavaScript) Source: https://github.com/azu/kuromojin/blob/master/README.md Analyzes Japanese text into tokens using the kuromojin library. It returns a Promise resolved with an array of token objects, each containing detailed morphological information. The returned data is read-only to ensure immutability. ```javascript import { tokenize, getTokenizer } from "kuromojin"; getTokenizer().then(tokenizer => { // kuromoji.js's `tokenizer` instance }); tokenize(text).then(tokens => { console.log(tokens); /* [ { word_id: 509800, // 辞書内での単語ID word_type: 'KNOWN', // 単語タイプ(辞書に登録されている単語ならKNOWN, 未知語ならUNKNOWN) word_position: 1, // 単語の開始位置 surface_form: '黒文字', // 表層形 pos: '名詞', // 品詞 pos_detail_1: '一般', // 品詞細分類1 pos_detail_2: '*', // 品詞細分類2 pos_detail_3: '*', // 品詞細分類3 conjugated_type: '*', // 活用型 conjugated_form: '*', // 活用形 basic_form: '黒文字', // 基本形 reading: 'クロモジ', // 読み pronunciation: 'クロモジ' // 発音 } ] */ }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.