From 955e131f9673e006556bc2c5e0b3551a614ccc48 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Tue, 5 Nov 2019 15:56:45 +0200 Subject: add parser selection options --- ext/bg/settings.html | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'ext/bg/settings.html') diff --git a/ext/bg/settings.html b/ext/bg/settings.html index bdcc11d3..8505567b 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -587,6 +587,35 @@ +
+

Text Parsing Options

+ +

+ Yomichan can attempt to parse entire sentences or longer text blocks on the search page, + adding furigana above words and a small space between words. +

+ +

+ Two types of parsers are supported. The first one, enabled by default, works using the built-in + scanning functionality by automatically advancing in the sentence after a matching word. +

+ +

+ The second type is an external program called MeCab + that uses its own dictionaries and a special parsing algorithm. To get it working, you must first + install it and a native messaging component + that acts as a bridge between the program and Yomichan. +

+ +
+ +
+ +
+ +
+
+
-- cgit v1.2.3 From c1d24208d383e687f443c4e7dfc7bfda81d191bd Mon Sep 17 00:00:00 2001 From: siikamiika Date: Fri, 8 Nov 2019 00:49:20 +0200 Subject: start mecab only after enabling the setting --- ext/bg/js/backend.js | 4 ++++ ext/bg/js/mecab.js | 14 ++++++++++++- ext/bg/js/settings.js | 15 +++++++++++++ ext/bg/settings.html | 58 +++++++++++++++++++++++++-------------------------- 4 files changed, 61 insertions(+), 30 deletions(-) (limited to 'ext/bg/settings.html') diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index e97f32b5..027cc250 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -98,6 +98,10 @@ class Backend { } this.anki = options.anki.enable ? new AnkiConnect(options.anki.server) : new AnkiNull(); + + if (options.parsing.enableMecabParser) { + this.mecab.startListener(); + } } async getFullOptions() { diff --git a/ext/bg/js/mecab.js b/ext/bg/js/mecab.js index fba9b2eb..4c62c2b0 100644 --- a/ext/bg/js/mecab.js +++ b/ext/bg/js/mecab.js @@ -19,16 +19,20 @@ class Mecab { constructor() { + this.port = null; this.listeners = {}; this.sequence = 0; - this.startListener(); } async parseText(text) { + if (this.port === null) { + return {}; + } return await this.invoke('parse_text', {text}); } startListener() { + if (this.port !== null) { return; } this.port = chrome.runtime.connectNative('yomichan_mecab'); this.port.onMessage.addListener((message) => { const {sequence, data} = message; @@ -41,6 +45,14 @@ class Mecab { }); } + stopListener() { + if (this.port === null) { return; } + this.port.disconnect(); + this.port = null; + this.listeners = {}; + this.sequence = 0; + } + invoke(action, params) { return new Promise((resolve, reject) => { const sequence = this.sequence++; diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index f4fe032a..0013291a 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -154,6 +154,7 @@ async function formWrite(options) { function formSetupEventListeners() { $('input, select, textarea').not('.anki-model').not('.ignore-form-changes *').change(utilAsync(onFormOptionsChanged)); + $('#parsing-mecab-enable').change(onParseMecabChanged); $('.anki-model').change(utilAsync(onAnkiModelChanged)); } @@ -440,6 +441,20 @@ function onMessage({action, params}, sender, callback) { } +/* + * Text parsing + */ + +function onParseMecabChanged(e) { + const mecab = utilBackend().mecab; + if (e.target.checked) { + mecab.startListener(); + } else { + mecab.stopListener(); + } +} + + /* * Anki */ diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 8505567b..08b9b6c1 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -410,6 +410,35 @@
+
+

Text Parsing Options

+ +

+ Yomichan can attempt to parse entire sentences or longer text blocks on the search page, + adding furigana above words and a small space between words. +

+ +

+ Two types of parsers are supported. The first one, enabled by default, works using the built-in + scanning functionality by automatically advancing in the sentence after a matching word. +

+ +

+ The second type is an external program called MeCab + that uses its own dictionaries and a special parsing algorithm. To get it working, you must first + install it and a native messaging component + that acts as a bridge between the program and Yomichan. +

+ +
+ +
+ +
+ +
+
+
@@ -587,35 +616,6 @@
-
-

Text Parsing Options

- -

- Yomichan can attempt to parse entire sentences or longer text blocks on the search page, - adding furigana above words and a small space between words. -

- -

- Two types of parsers are supported. The first one, enabled by default, works using the built-in - scanning functionality by automatically advancing in the sentence after a matching word. -

- -

- The second type is an external program called MeCab - that uses its own dictionaries and a special parsing algorithm. To get it working, you must first - install it and a native messaging component - that acts as a bridge between the program and Yomichan. -

- -
- -
- -
- -
-
-
-- cgit v1.2.3 From cc8221c6ea686521261e2ac562d3d5a6d0b9913a Mon Sep 17 00:00:00 2001 From: siikamiika Date: Wed, 13 Nov 2019 20:24:11 +0200 Subject: add reading modes --- ext/bg/js/api.js | 14 ++++++++------ ext/bg/js/options.js | 3 ++- ext/bg/js/settings.js | 2 ++ ext/bg/settings.html | 9 +++++++++ ext/mixed/js/japanese.js | 37 +++++++++++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 7 deletions(-) (limited to 'ext/bg/settings.html') diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index bc9dfba1..228447c3 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -92,12 +92,13 @@ async function apiTextParse(text, optionsContext) { const {expression, reading} = definitions[0]; const source = text.slice(0, sourceLength); for (const {text, furigana} of jpDistributeFuriganaInflected(expression, reading, source)) { - // can't use 'furigana' in templates - term.push({text, reading: furigana}); + const reading = jpConvertReading(text, furigana, options.parsing.readingMode); + term.push({text, reading}); } text = text.slice(source.length); } else { - term.push({text: text[0]}); + const reading = jpConvertReading(text[0], null, options.parsing.readingMode); + term.push({text: text[0], reading}); text = text.slice(1); } results.push(term); @@ -122,11 +123,12 @@ async function apiTextParseMecab(text, optionsContext) { jpKatakanaToHiragana(reading), source )) { - // can't use 'furigana' in templates - term.push({text, reading: furigana}); + const reading = jpConvertReading(text, furigana, options.parsing.readingMode); + term.push({text, reading}); } } else { - term.push({text: source}); + const reading = jpConvertReading(source, null, options.parsing.readingMode); + term.push({text: source, reading}); } result.push(term); } diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 053fb13d..b9bf85f3 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -314,7 +314,8 @@ function profileOptionsCreateDefaults() { parsing: { enableScanningParser: true, enableMecabParser: false, - selectedParser: null + selectedParser: null, + readingMode: 'hiragana' }, anki: { diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index f4fe032a..ab267c32 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -66,6 +66,7 @@ async function formRead(options) { options.parsing.enableScanningParser = $('#parsing-scan-enable').prop('checked'); options.parsing.enableMecabParser = $('#parsing-mecab-enable').prop('checked'); + options.parsing.readingMode = $('#parsing-reading-mode').val(); const optionsAnkiEnableOld = options.anki.enable; options.anki.enable = $('#anki-enable').prop('checked'); @@ -131,6 +132,7 @@ async function formWrite(options) { $('#parsing-scan-enable').prop('checked', options.parsing.enableScanningParser); $('#parsing-mecab-enable').prop('checked', options.parsing.enableMecabParser); + $('#parsing-reading-mode').val(options.parsing.readingMode); $('#anki-enable').prop('checked', options.anki.enable); $('#card-tags').val(options.anki.tags.join(' ')); diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 08b9b6c1..0badb817 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -437,6 +437,15 @@
+ +
+ + +
diff --git a/ext/mixed/js/japanese.js b/ext/mixed/js/japanese.js index e2d7a090..a7cd0452 100644 --- a/ext/mixed/js/japanese.js +++ b/ext/mixed/js/japanese.js @@ -48,6 +48,43 @@ function jpKatakanaToHiragana(text) { return result; } +function jpHiraganaToKatakana(text) { + let result = ''; + for (const c of text) { + if (wanakana.isHiragana(c)) { + result += wanakana.toKatakana(c); + } else { + result += c; + } + } + + return result; +} + +function jpToRomaji(text) { + return wanakana.toRomaji(text); +} + +function jpConvertReading(expressionFragment, readingFragment, readingMode) { + switch (readingMode) { + case 'hiragana': + return jpKatakanaToHiragana(readingFragment || ''); + case 'katakana': + return jpHiraganaToKatakana(readingFragment || ''); + case 'romaji': + if (readingFragment) { + return jpToRomaji(readingFragment); + } else { + if (jpIsKana(expressionFragment)) { + return jpToRomaji(expressionFragment); + } + } + return readingFragment; + default: + return readingFragment; + } +} + function jpDistributeFurigana(expression, reading) { const fallback = [{furigana: reading, text: expression}]; if (!reading) { -- cgit v1.2.3