yomichan/ext/bg/js/mecab.js

122 lines
3.8 KiB
JavaScript
Raw Normal View History

2019-11-03 03:08:57 +00:00
/*
* Copyright (C) 2019-2021 Yomichan Authors
2019-11-03 03:08:57 +00:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2020-01-01 17:00:31 +00:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-11-03 03:08:57 +00:00
*/
class Mecab {
constructor() {
this.port = null;
2020-01-25 03:02:53 +00:00
this.listeners = new Map();
2019-11-03 03:08:57 +00:00
this.sequence = 0;
}
2019-11-23 17:18:29 +00:00
onError(error) {
yomichan.logError(error);
2019-11-23 17:18:29 +00:00
}
async checkVersion() {
try {
const {version} = await this.invoke('get_version', {});
if (version !== Mecab.version) {
this.stopListener();
throw new Error(`Unsupported MeCab native messenger version ${version}. Yomichan supports version ${Mecab.version}.`);
}
} catch (error) {
this.onError(error);
}
2019-11-23 17:18:29 +00:00
}
async parseText(text) {
const rawResults = await this.invoke('parse_text', {text});
// {
// 'mecab-name': [
// // line1
// [
// {str expression: 'expression', str reading: 'reading', str source: 'source'},
// {str expression: 'expression2', str reading: 'reading2', str source: 'source2'}
// ],
// line2,
// ...
// ],
// 'mecab-name2': [...]
// }
const results = {};
for (const [mecabName, parsedLines] of Object.entries(rawResults)) {
const result = [];
for (const parsedLine of parsedLines) {
const line = [];
for (const {expression, reading, source} of parsedLine) {
line.push({
expression: expression || '',
reading: reading || '',
source: source || ''
});
}
result.push(line);
}
results[mecabName] = result;
}
return results;
2019-11-03 03:08:57 +00:00
}
startListener() {
if (this.port !== null) { return; }
this.port = chrome.runtime.connectNative('yomichan_mecab');
2019-11-11 18:54:23 +00:00
this.port.onMessage.addListener(this.onNativeMessage.bind(this));
2019-11-23 17:18:29 +00:00
this.checkVersion();
2019-11-03 03:08:57 +00:00
}
stopListener() {
if (this.port === null) { return; }
this.port.disconnect();
this.port = null;
2020-01-25 03:02:53 +00:00
this.listeners.clear();
this.sequence = 0;
}
2019-11-11 18:54:23 +00:00
onNativeMessage({sequence, data}) {
2020-01-25 03:02:53 +00:00
const listener = this.listeners.get(sequence);
if (typeof listener === 'undefined') { return; }
const {callback, timer} = listener;
clearTimeout(timer);
callback(data);
this.listeners.delete(sequence);
2019-11-11 18:54:23 +00:00
}
2019-11-03 03:08:57 +00:00
invoke(action, params) {
2019-11-23 17:18:29 +00:00
if (this.port === null) {
return Promise.resolve({});
2019-11-23 17:18:29 +00:00
}
2019-11-03 03:08:57 +00:00
return new Promise((resolve, reject) => {
const sequence = this.sequence++;
2020-01-25 03:02:53 +00:00
this.listeners.set(sequence, {
2019-11-11 00:28:00 +00:00
callback: resolve,
2019-11-03 03:08:57 +00:00
timer: setTimeout(() => {
2020-01-25 03:02:53 +00:00
this.listeners.delete(sequence);
2019-11-11 00:28:00 +00:00
reject(new Error(`Mecab invoke timed out in ${Mecab.timeout} ms`));
2019-11-03 15:06:02 +00:00
}, Mecab.timeout)
2020-01-25 03:02:53 +00:00
});
2019-11-03 03:08:57 +00:00
this.port.postMessage({action, params, sequence});
});
}
}
2019-11-05 13:56:45 +00:00
Mecab.timeout = 5000;
2019-11-23 17:18:29 +00:00
Mecab.version = 1;