### Package.json Postbuild Script for Dictionary Copying (POSIX)
Source: https://github.com/shuruni/kuroshiro-browser/blob/main/README.md
An example of a 'postbuild' script for package.json that copies dictionary files from 'node_modules/kuroshiro-browser/dist/dict' to a local 'dist/dict' directory. This script is specifically for POSIX-compatible systems (Linux, macOS) and automates the process of making dictionaries available after building the project.
```json
{
"scripts": {
"postbuild": "mkdir -p ./dist/dict; cp -r ./node_modules/kuroshiro-browser/dist/dict/* ./dist/dict;"
}
}
```
--------------------------------
### Basic Text Conversion with Kuroshiro (JavaScript)
Source: https://github.com/shuruni/kuroshiro-browser/blob/main/README.md
Illustrates a basic text conversion using the Kuroshiro library, demonstrating the `convert` method with specified options for mode and target. This example assumes the `kuroshiro` instance has already been initialized. It's a fallback to the original Kuroshiro API.
```javascript
// normal
await kuroshiro.convert("感じ取れたら手を繋ごう、重なるのは人生のライン and レミリア最高!", {mode:"okurigana", to:"hiragana"});
// result:かんじとれたらてをつなごう、かさなるのはじんせいのライン and レミリアさいこう!
```
--------------------------------
### Kuroshiro Initialization and Usage
Source: https://github.com/shuruni/kuroshiro-browser/blob/main/README.md
Demonstrates how to initialize Kuroshiro using different methods (async/await and Promises) and convert text to furigana.
```APIDOC
## Initialization and Usage Examples
### Using Async/Await
This example shows how to initialize Kuroshiro with Kuromoji using async/await and get furigana for a given text.
```javascript
import { Kuroshiro } from "kuroshiro-browser"
const IS_PROD = (import.meta.env.MODE == 'production')
const textToConvert = "感じ取れたら手を繋ごう、重なるのは人生のライン and レミリア最高!"
const kuroshiro = await Kuroshiro.buildAndInitWithKuromoji(IS_PROD)
const furigana = await kuroshiro.getFurigana(textToConvert)
console.log(furigana)
// 感じ取れたら手を繋ごう、重なるのは人生のライン and レミリア最高!
```
### Using Promises
This example demonstrates initializing Kuroshiro using Promises and retrieving furigana.
```javascript
import { Kuroshiro } from "kuroshiro-browser"
const IS_PROD = (import.meta.env.MODE == 'production')
const textToConvert = "感じ取れたら手を繋ごう、重なるのは人生のライン and レミリア最高!"
Kuroshiro.buildAndInitWithKuromoji(IS_PROD).then((kuroshiro) => {
kuroshiro.getFurigana(textToConvert).then((furigana) => {
console.log(furigana)
// 感じ取れたら手を繋ごう、重なるのは人生のライン and レミリア最高!
})
})
```
### Standard Conversion
This example shows a standard conversion using the `convert` method with options.
```javascript
// normal
await kuroshiro.convert("感じ取れたら手を繋ごう、重なるのは人生のライン and レミリア最高!", {mode:"okurigana", to:"hiragana"});
// result:かんじとれたらてをつなごう、かさなるのはじんせいのライン and レミリアさいこう!
```
```
--------------------------------
### Importing Kuroshiro Browser Components
Source: https://github.com/shuruni/kuroshiro-browser/blob/main/README.md
Demonstrates how to import core components from the 'kuroshiro-browser' package. This includes Kuroshiro itself, the Kuromoji analyzer, and Kuromoji. Note that importing Kuroshiro is generally sufficient due to the integrated nature of the package.
```javascript
import { Kuroshiro, KuroshiroAnalyzerKuromoji, Kuromoji } from "kuroshiro-browser"
```
--------------------------------
### Convert Japanese Text to Furigana using Promises (JavaScript)
Source: https://github.com/shuruni/kuroshiro-browser/blob/main/README.md
Shows how to convert Japanese text to furigana using the Kuroshiro browser library with traditional Promises. It initializes the library using `buildAndInitWithKuromoji` and then chains `getFurigana` calls. This method is suitable for environments where async/await is not preferred or available. Requires the 'kuroshiro-browser' package.
```javascript
import { Kuroshiro } from "kuroshiro-browser"
const IS_PROD = (import.meta.env.MODE == 'production')
const textToConvert = "感じ取れたら手を繋ごう、重なるのは人生のライン and レミリア最高!"
Kuroshiro.buildAndInitWithKuromoji(IS_PROD).then((kuroshiro) => {
kuroshiro.getFurigana(textToConvert).then((furigana) => {
console.log(furigana)
// 感じ取れたら手を繋ごう、重なるのは人生のライン and レミリア最高!
})
})
```
--------------------------------
### Convert Japanese Text to Furigana using Async/Await (JavaScript)
Source: https://github.com/shuruni/kuroshiro-browser/blob/main/README.md
Demonstrates converting Japanese text to furigana using the Kuroshiro browser library with async/await syntax. It initializes the library with Kuromoji and then calls the `getFurigana` method. Requires the 'kuroshiro-browser' package and a JavaScript environment that supports ES modules.
```javascript
import { Kuroshiro } from "kuroshiro-browser"
const IS_PROD = (import.meta.env.MODE == 'production')
const textToConvert = "感じ取れたら手を繋ごう、重なるのは人生のライン and レミリア最高!"
const kuroshiro = await Kuroshiro.buildAndInitWithKuromoji(IS_PROD)
const furigana = await kuroshiro.getFurigana(textToConvert)
console.log(furigana)
// 感じ取れたら手を繋ごう、重なるのは人生のライン and レミリア最高!
```
--------------------------------
### Breaking Changes and Dictionary Loading
Source: https://github.com/shuruni/kuroshiro-browser/blob/main/README.md
Explains a major breaking change related to handling static data files (dictionaries) in a browser environment and the solution implemented using `DictionaryLoader`.
```APIDOC
## RE: Breaking Change to API
### Dictionary File Loading in Browser Environment
**Issue:** When running in a browser, Node.js's `fs` module is unavailable for accessing local files. Dictionaries, which are static data files, need to be fetched via HTTP requests.
**Problem:** When `kuroshiro-browser` is installed as a package, the dictionary files are not located at the project's root URL, leading to broken fetch requests.
**Solution:**
1. **`DictionaryLoader` Modifications:** A static field was added to `DictionaryLoader` in Kuromoji to store dictionary URLs.
2. **`DictionaryLoader.generateDictUrls(IS_PROD)`:** A static method was introduced that dynamically sets the correct dictionary URLs based on whether the application is running in production (`IS_PROD`).
3. **Environment Variable `import.meta.env.MODE`:** This Vite environment variable is used to determine the production status. It's crucial to pass the correct `IS_PROD` value from the *using project* to `Kuroshiro.buildAndInitWithKuromoji`.
**Impact:** If the `IS_PROD` parameter is not correctly provided to the initialization method, the dictionary URLs will resolve incorrectly, and the library may fail to load necessary data.
```
--------------------------------
### Kuroshiro API Breakdown and Utilities (JavaScript)
Source: https://github.com/shuruni/kuroshiro-browser/blob/main/README.md
Outlines the structure and methods available within the Kuroshiro JavaScript library, including existing and added APIs. It details utility functions for checking Japanese text properties and converting between Kana forms, along with the preferred constructor `buildAndInitWithKuromoji`.
```javascript
Kuroshiro {
// Existing API
async init(analyzer, IS_PROD = (import.meta.env.MODE == 'production')) //Modified to set dictUrls based on env
async convert(str, options)
Util = {
isHiragana,
isKatakana,
isKana,
isKanji,
isJapanese,
hasHiragana,
hasKatakana,
hasKana,
hasKanji,
hasJapanese,
kanaToHiragna,
kanaToKatakana,
kanaToRomaji
};
// Added
static buildAndInitWithKuromoji(IS_PROD = (import.meta.env.MODE == 'production')) // Preferred Constructor
getFurigana(text, debug={...console, success:console.log}) //Shorthand Command with optional debug callback
}
// No API Changes
KuroshiroAnalyzerKuromoji {
init()
parse(str = "")
}
// No API Changes
Kuromoji {
builder()
dictionaryBuilder()
}
```
--------------------------------
### Kuroshiro API Breakdown
Source: https://github.com/shuruni/kuroshiro-browser/blob/main/README.md
Details the available methods and properties of the Kuroshiro class, including both existing and newly added functionalities.
```APIDOC
## Kuroshiro API Breakdown
### Kuroshiro Class
This section details the methods and properties available within the `Kuroshiro` class.
#### Existing API (Potentially Modified)
* `async init(analyzer, IS_PROD = (import.meta.env.MODE == 'production'))`: Initializes the Kuroshiro instance. Modified to set `dictUrls` based on the environment.
* `async convert(str, options)`: Converts Japanese text based on the provided options.
* `Util`: An object containing utility functions:
* `isHiragana(str)`
* `isKatakana(str)`
* `isKana(str)`
* `isKanji(str)`
* `isJapanese(str)`
* `hasHiragana(str)`
* `hasKatakana(str)`
* `hasKana(str)`
* `hasKanji(str)`
* `hasJapanese(str)`
* `kanaToHiragna(str)`
* `kanaToKatakana(str)`
* `kanaToRomaji(str)`
#### Added API
* `static buildAndInitWithKuromoji(IS_PROD = (import.meta.env.MODE == 'production'))`: The preferred constructor for building and initializing Kuroshiro with Kuromoji.
* `getFurigana(text, debug={...console, success:console.log})`: A shorthand method for getting furigana, with an optional debug callback.
### KuroshiroAnalyzerKuromoji Class
This class is part of the underlying analyzer and its API remains unchanged.
* `init()`: Initializes the analyzer.
* `parse(str = "")`: Parses the input string.
### Kuromoji Class
This class provides utilities for Kuromoji dictionary building and its API remains unchanged.
* `builder()`: Creates a builder instance.
* `dictionaryBuilder()`: Creates a dictionary builder instance.
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.