aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/backend.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-02-08 17:53:12 -0500
committerGitHub <noreply@github.com>2021-02-08 17:53:12 -0500
commit69e8addb4722fa0b35bb7a8bb1d0107038a6d249 (patch)
tree179d850d5ef1866ce5fd7b8b68ff3981712d4c40 /ext/bg/js/backend.js
parent7b28c2c5329b0bb0fa74872f160ec6b2974654d0 (diff)
Mecab refactor (#1357)
* Use private variables * Use local variables * Remove onError * Use private functions * Move results parsing * Set up disconnect observation * Add _clearPort * Update API * Rename * Fix iterator * Update parseText invocation * Update parseText format * Reorganize * Update implementation and docs * Fix some port setup issues
Diffstat (limited to 'ext/bg/js/backend.js')
-rw-r--r--ext/bg/js/backend.js23
1 files changed, 13 insertions, 10 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index 55905fab..67b17cc9 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -877,11 +877,7 @@ class Backend {
this._anki.server = options.anki.server;
this._anki.enabled = options.anki.enable;
- if (options.parsing.enableMecabParser) {
- this._mecab.startListener();
- } else {
- this._mecab.stopListener();
- }
+ this._mecab.setEnabled(options.parsing.enableMecabParser);
if (options.clipboard.enableBackgroundMonitor) {
this._clipboardMonitor.start();
@@ -988,12 +984,19 @@ class Backend {
async _textParseMecab(text, options) {
const jp = this._japaneseUtil;
const {parsing: {readingMode}} = options;
+
+ let parseTextResults;
+ try {
+ parseTextResults = await this._mecab.parseText(text);
+ } catch (e) {
+ return [];
+ }
+
const results = [];
- const rawResults = await this._mecab.parseText(text);
- for (const [mecabName, parsedLines] of Object.entries(rawResults)) {
+ for (const {name, lines} of parseTextResults) {
const result = [];
- for (const parsedLine of parsedLines) {
- for (const {expression, reading, source} of parsedLine) {
+ for (const line of lines) {
+ for (const {expression, reading, source} of line) {
const term = [];
for (const {text: text2, furigana} of jp.distributeFuriganaInflected(
expression.length > 0 ? expression : source,
@@ -1007,7 +1010,7 @@ class Backend {
}
result.push([{text: '\n', reading: ''}]);
}
- results.push([mecabName, result]);
+ results.push([name, result]);
}
return results;
}