Skip to content

Commit

Permalink
fflate
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiroshiba committed Dec 7, 2024
1 parent 946c9eb commit 4ccd8ca
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 59 deletions.
36 changes: 4 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"glob": "11.0.0",
"hotkeys-js": "3.13.6",
"immer": "9.0.21",
"kuromoji": "github:VOICEVOX/kuromoji.js#path.joinをなくす",
"kuromoji": "github:VOICEVOX/kuromoji.js#すべてを書き換えうr",
"markdown-it": "13.0.2",
"move-file": "3.0.0",
"multistream": "4.1.0",
Expand Down Expand Up @@ -94,7 +94,6 @@
"@types/async-lock": "1.4.0",
"@types/encoding-japanese": "1.0.18",
"@types/glob": "8.0.0",
"@types/kuromoji": "0.1.3",
"@types/markdown-it": "12.2.0",
"@types/multistream": "4.1.0",
"@types/semver": "7.3.9",
Expand Down
46 changes: 21 additions & 25 deletions src/mock/engineMock/talkModelMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/** テキストをトークン列に変換するトークナイザーを取得する */
Expand All @@ -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);
}
});
});
}

Expand Down

0 comments on commit 4ccd8ca

Please sign in to comment.