move QueryParser.parseText to Backend
This commit is contained in:
parent
7f3e272839
commit
c613321a73
@ -85,7 +85,6 @@ class Backend {
|
||||
['kanjiFind', {handler: this._onApiKanjiFind.bind(this), async: true}],
|
||||
['termsFind', {handler: this._onApiTermsFind.bind(this), async: true}],
|
||||
['textParse', {handler: this._onApiTextParse.bind(this), async: true}],
|
||||
['textParseMecab', {handler: this._onApiTextParseMecab.bind(this), async: true}],
|
||||
['definitionAdd', {handler: this._onApiDefinitionAdd.bind(this), async: true}],
|
||||
['definitionsAddable', {handler: this._onApiDefinitionsAddable.bind(this), async: true}],
|
||||
['noteView', {handler: this._onApiNoteView.bind(this), async: true}],
|
||||
@ -315,6 +314,65 @@ class Backend {
|
||||
return await this.dictionaryImporter.import(this.database, archiveSource, onProgress, details);
|
||||
}
|
||||
|
||||
async _textParseScanning(text, options) {
|
||||
const results = [];
|
||||
while (text.length > 0) {
|
||||
const term = [];
|
||||
const [definitions, sourceLength] = await this.translator.findTerms(
|
||||
'simple',
|
||||
text.substring(0, options.scanning.length),
|
||||
{},
|
||||
options
|
||||
);
|
||||
if (definitions.length > 0) {
|
||||
dictTermsSort(definitions);
|
||||
const {expression, reading} = definitions[0];
|
||||
const source = text.substring(0, sourceLength);
|
||||
for (const {text: text2, furigana} of jp.distributeFuriganaInflected(expression, reading, source)) {
|
||||
const reading2 = jp.convertReading(text2, furigana, options.parsing.readingMode);
|
||||
term.push({text: text2, reading: reading2});
|
||||
}
|
||||
text = text.substring(source.length);
|
||||
} else {
|
||||
const reading = jp.convertReading(text[0], null, options.parsing.readingMode);
|
||||
term.push({text: text[0], reading});
|
||||
text = text.substring(1);
|
||||
}
|
||||
results.push(term);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
async _textParseMecab(text, options) {
|
||||
const results = [];
|
||||
const rawResults = await this.mecab.parseText(text);
|
||||
for (const [mecabName, parsedLines] of Object.entries(rawResults)) {
|
||||
const result = [];
|
||||
for (const parsedLine of parsedLines) {
|
||||
for (const {expression, reading, source} of parsedLine) {
|
||||
const term = [];
|
||||
if (expression !== null && reading !== null) {
|
||||
for (const {text: text2, furigana} of jp.distributeFuriganaInflected(
|
||||
expression,
|
||||
jp.convertKatakanaToHiragana(reading),
|
||||
source
|
||||
)) {
|
||||
const reading2 = jp.convertReading(text2, furigana, options.parsing.readingMode);
|
||||
term.push({text: text2, reading: reading2});
|
||||
}
|
||||
} else {
|
||||
const reading2 = jp.convertReading(source, null, options.parsing.readingMode);
|
||||
term.push({text: source, reading: reading2});
|
||||
}
|
||||
result.push(term);
|
||||
}
|
||||
result.push([{text: '\n'}]);
|
||||
}
|
||||
results.push([mecabName, result]);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
// Message handlers
|
||||
|
||||
_onApiYomichanCoreReady(_params, sender) {
|
||||
@ -406,61 +464,27 @@ class Backend {
|
||||
async _onApiTextParse({text, optionsContext}) {
|
||||
const options = this.getOptions(optionsContext);
|
||||
const results = [];
|
||||
while (text.length > 0) {
|
||||
const term = [];
|
||||
const [definitions, sourceLength] = await this.translator.findTerms(
|
||||
'simple',
|
||||
text.substring(0, options.scanning.length),
|
||||
{},
|
||||
options
|
||||
);
|
||||
if (definitions.length > 0) {
|
||||
dictTermsSort(definitions);
|
||||
const {expression, reading} = definitions[0];
|
||||
const source = text.substring(0, sourceLength);
|
||||
for (const {text: text2, furigana} of jp.distributeFuriganaInflected(expression, reading, source)) {
|
||||
const reading2 = jp.convertReading(text2, furigana, options.parsing.readingMode);
|
||||
term.push({text: text2, reading: reading2});
|
||||
}
|
||||
text = text.substring(source.length);
|
||||
} else {
|
||||
const reading = jp.convertReading(text[0], null, options.parsing.readingMode);
|
||||
term.push({text: text[0], reading});
|
||||
text = text.substring(1);
|
||||
}
|
||||
results.push(term);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
async _onApiTextParseMecab({text, optionsContext}) {
|
||||
const options = this.getOptions(optionsContext);
|
||||
const results = [];
|
||||
const rawResults = await this.mecab.parseText(text);
|
||||
for (const [mecabName, parsedLines] of Object.entries(rawResults)) {
|
||||
const result = [];
|
||||
for (const parsedLine of parsedLines) {
|
||||
for (const {expression, reading, source} of parsedLine) {
|
||||
const term = [];
|
||||
if (expression !== null && reading !== null) {
|
||||
for (const {text: text2, furigana} of jp.distributeFuriganaInflected(
|
||||
expression,
|
||||
jp.convertKatakanaToHiragana(reading),
|
||||
source
|
||||
)) {
|
||||
const reading2 = jp.convertReading(text2, furigana, options.parsing.readingMode);
|
||||
term.push({text: text2, reading: reading2});
|
||||
}
|
||||
} else {
|
||||
const reading2 = jp.convertReading(source, null, options.parsing.readingMode);
|
||||
term.push({text: source, reading: reading2});
|
||||
}
|
||||
result.push(term);
|
||||
}
|
||||
result.push([{text: '\n'}]);
|
||||
}
|
||||
results.push([mecabName, result]);
|
||||
if (options.parsing.enableScanningParser) {
|
||||
results.push({
|
||||
source: 'scanning-parser',
|
||||
id: 'scan',
|
||||
content: await this._textParseScanning(text, options)
|
||||
});
|
||||
}
|
||||
|
||||
if (options.parsing.enableMecabParser) {
|
||||
const mecabResults = await this._textParseMecab(text, options);
|
||||
for (const [mecabDictName, mecabDictResults] of mecabResults) {
|
||||
results.push({
|
||||
source: 'mecab',
|
||||
dictionary: mecabDictName,
|
||||
id: `mecab-${mecabDictName}`,
|
||||
content: mecabDictResults
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
|
@ -71,7 +71,16 @@ class QueryParserGenerator {
|
||||
for (const parseResult of parseResults) {
|
||||
const optionContainer = this._templateHandler.instantiate('select-option');
|
||||
optionContainer.value = parseResult.id;
|
||||
optionContainer.textContent = parseResult.name;
|
||||
switch (parseResult.source) {
|
||||
case 'scanning-parser':
|
||||
optionContainer.textContent = 'Scanning parser';
|
||||
break;
|
||||
case 'mecab':
|
||||
optionContainer.textContent = `MeCab: ${parseResult.dictionary}`;
|
||||
break;
|
||||
default:
|
||||
optionContainer.textContent = 'Unrecognized dictionary';
|
||||
}
|
||||
optionContainer.defaultSelected = selectedParser === parseResult.id;
|
||||
selectContainer.appendChild(optionContainer);
|
||||
}
|
||||
|
@ -21,7 +21,6 @@
|
||||
* apiOptionsSet
|
||||
* apiTermsFind
|
||||
* apiTextParse
|
||||
* apiTextParseMecab
|
||||
* docSentenceExtract
|
||||
*/
|
||||
|
||||
@ -128,7 +127,7 @@ class QueryParser extends TextScanner {
|
||||
|
||||
this.setPreview(text);
|
||||
|
||||
this.parseResults = await this.parseText(text);
|
||||
this.parseResults = await apiTextParse(text, this.getOptionsContext());
|
||||
this.refreshSelectedParser();
|
||||
|
||||
this.renderParserSelect();
|
||||
@ -137,28 +136,6 @@ class QueryParser extends TextScanner {
|
||||
this.setSpinnerVisible(false);
|
||||
}
|
||||
|
||||
async parseText(text) {
|
||||
const results = [];
|
||||
if (this.options.parsing.enableScanningParser) {
|
||||
results.push({
|
||||
name: 'Scanning parser',
|
||||
id: 'scan',
|
||||
parsedText: await apiTextParse(text, this.getOptionsContext())
|
||||
});
|
||||
}
|
||||
if (this.options.parsing.enableMecabParser) {
|
||||
const mecabResults = await apiTextParseMecab(text, this.getOptionsContext());
|
||||
for (const [mecabDictName, mecabDictResults] of mecabResults) {
|
||||
results.push({
|
||||
name: `MeCab: ${mecabDictName}`,
|
||||
id: `mecab-${mecabDictName}`,
|
||||
parsedText: mecabDictResults
|
||||
});
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
setPreview(text) {
|
||||
const previewTerms = [];
|
||||
for (let i = 0, ii = text.length; i < ii; i += 2) {
|
||||
@ -183,6 +160,6 @@ class QueryParser extends TextScanner {
|
||||
const parseResult = this.getParseResult();
|
||||
this.queryParser.textContent = '';
|
||||
if (!parseResult) { return; }
|
||||
this.queryParser.appendChild(this.queryParserGenerator.createParseResult(parseResult.parsedText));
|
||||
this.queryParser.appendChild(this.queryParserGenerator.createParseResult(parseResult.content));
|
||||
}
|
||||
}
|
||||
|
@ -44,10 +44,6 @@ function apiTextParse(text, optionsContext) {
|
||||
return _apiInvoke('textParse', {text, optionsContext});
|
||||
}
|
||||
|
||||
function apiTextParseMecab(text, optionsContext) {
|
||||
return _apiInvoke('textParseMecab', {text, optionsContext});
|
||||
}
|
||||
|
||||
function apiKanjiFind(text, optionsContext) {
|
||||
return _apiInvoke('kanjiFind', {text, optionsContext});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user