-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
26 additions
and
59 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,32 +3,27 @@ | |
* 値は適当だが、テストで使えるよう決定論的に決まるようにしたり、UIのバグに気づけるようある程度規則を持たせている。 | ||
*/ | ||
|
||
import kuromoji, { IpadicFeatures, Tokenizer } from "kuromoji"; | ||
import { builder, IpadicFeatures, Tokenizer } from "kuromoji"; | ||
import { moraToPhonemes } from "./phonemeMock"; | ||
import { moraPattern } from "@/domain/japanese"; | ||
import { AccentPhrase, Mora } from "@/openapi"; | ||
|
||
/** Nodeとして動いてほしいかを判定する */ | ||
const isNode = | ||
// window.documentがなければNode | ||
typeof window == "undefined" || | ||
typeof window.document == "undefined" || | ||
// happy-domのときはNode | ||
typeof (window as { happyDOM?: unknown }).happyDOM != "undefined"; | ||
|
||
let _tokenizer: Tokenizer<IpadicFeatures> | undefined; | ||
|
||
/** kuromoji用の辞書のパスを取得する */ | ||
function getDicPath() { | ||
// ブラウザのときはCDNから辞書を取得し、Nodeのときはローカルから取得する | ||
|
||
const pathForBrowser = `https://cdn.jsdelivr.net/npm/[email protected]/dict`; | ||
const pathForNode = "node_modules/kuromoji/dict"; | ||
|
||
// window.documentがなければNode | ||
if (typeof window == "undefined" || typeof window.document == "undefined") { | ||
return pathForNode; | ||
} | ||
|
||
// happy-domのときはNode | ||
if (typeof (window as { happyDOM?: unknown }).happyDOM != "undefined") { | ||
return pathForNode; | ||
} | ||
|
||
// それ以外はブラウザ | ||
return pathForBrowser; | ||
return isNode ? pathForNode : pathForBrowser; | ||
} | ||
|
||
/** テキストをトークン列に変換するトークナイザーを取得する */ | ||
|
@@ -38,16 +33,17 @@ async function createOrGetTokenizer() { | |
} | ||
|
||
return new Promise<Tokenizer<IpadicFeatures>>((resolve, reject) => { | ||
kuromoji | ||
.builder({ dicPath: getDicPath() }) | ||
.build((err: Error, tokenizer: Tokenizer<IpadicFeatures>) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
_tokenizer = tokenizer; | ||
resolve(tokenizer); | ||
} | ||
}); | ||
builder({ | ||
dicPath: getDicPath(), | ||
nodeOrBrowser: isNode ? "node" : "browser", | ||
}).build((err: Error, tokenizer: Tokenizer<IpadicFeatures>) => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
_tokenizer = tokenizer; | ||
resolve(tokenizer); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
|