Move apiTextParseMecab implementation into Backend
This commit is contained in:
parent
ddad034aa6
commit
7091c8c5c0
@ -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) {
|
||||
|
@ -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}) {
|
||||
|
Loading…
Reference in New Issue
Block a user