aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-12-09 21:21:17 -0500
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-12-13 23:11:56 -0500
commit7091c8c5c0a48630b1ca3b2ab500c131cec19a14 (patch)
treeea1383bce6089b61796e66c40dc5b0176b4878d6 /ext/bg/js
parentddad034aa6b8d3b67871af9bc2409412da29ec84 (diff)
Move apiTextParseMecab implementation into Backend
Diffstat (limited to 'ext/bg/js')
-rw-r--r--ext/bg/js/api.js33
-rw-r--r--ext/bg/js/backend.js31
2 files changed, 31 insertions, 33 deletions
diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js
index 7a4c2dcf..3be6ac56 100644
--- a/ext/bg/js/api.js
+++ b/ext/bg/js/api.js
@@ -41,37 +41,8 @@ function apiTextParse(text, optionsContext) {
return utilBackend()._onApiTextParse({text, optionsContext});
}
-async function apiTextParseMecab(text, optionsContext) {
- const options = await apiOptionsGet(optionsContext);
- const mecab = utilBackend().mecab;
-
- const results = {};
- const rawResults = await mecab.parseText(text);
- for (const mecabName in rawResults) {
- const result = [];
- for (const parsedLine of rawResults[mecabName]) {
- for (const {expression, reading, source} of parsedLine) {
- const term = [];
- if (expression !== null && reading !== null) {
- for (const {text, furigana} of jpDistributeFuriganaInflected(
- expression,
- jpKatakanaToHiragana(reading),
- source
- )) {
- const reading = jpConvertReading(text, furigana, options.parsing.readingMode);
- term.push({text, reading});
- }
- } else {
- const reading = jpConvertReading(source, null, options.parsing.readingMode);
- term.push({text: source, reading});
- }
- result.push(term);
- }
- result.push([{text: '\n'}]);
- }
- results[mecabName] = result;
- }
- return results;
+function apiTextParseMecab(text, optionsContext) {
+ return utilBackend()._onApiTextParseMecab({text, optionsContext});
}
function apiKanjiFind(text, optionsContext) {
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index 56bd4fca..67197cf7 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -284,8 +284,35 @@ class Backend {
return results;
}
- _onApiTextParseMecab({text, optionsContext}) {
- return apiTextParseMecab(text, optionsContext);
+ async _onApiTextParseMecab({text, optionsContext}) {
+ const options = await this.getOptions(optionsContext);
+ const results = {};
+ const rawResults = await this.mecab.parseText(text);
+ for (const mecabName in rawResults) {
+ const result = [];
+ for (const parsedLine of rawResults[mecabName]) {
+ for (const {expression, reading, source} of parsedLine) {
+ const term = [];
+ if (expression !== null && reading !== null) {
+ for (const {text, furigana} of jpDistributeFuriganaInflected(
+ expression,
+ jpKatakanaToHiragana(reading),
+ source
+ )) {
+ const reading = jpConvertReading(text, furigana, options.parsing.readingMode);
+ term.push({text, reading});
+ }
+ } else {
+ const reading = jpConvertReading(source, null, options.parsing.readingMode);
+ term.push({text: source, reading});
+ }
+ result.push(term);
+ }
+ result.push([{text: '\n'}]);
+ }
+ results[mecabName] = result;
+ }
+ return results;
}
_onApiDefinitionAdd({definition, mode, context, optionsContext}) {