summaryrefslogtreecommitdiff
path: root/ext/bg/js/api.js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/bg/js/api.js')
-rw-r--r--ext/bg/js/api.js48
1 files changed, 30 insertions, 18 deletions
diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js
index 7c9a72a7..2ab01af3 100644
--- a/ext/bg/js/api.js
+++ b/ext/bg/js/api.js
@@ -91,25 +91,10 @@ async function apiTextParse(text, optionsContext) {
definitions = dictTermsSort(definitions);
const {expression, reading} = definitions[0];
const source = text.slice(0, sourceLength);
-
- let stemLength = 0;
- const shortest = Math.min(source.length, expression.length);
- while (stemLength < shortest && source[stemLength] === expression[stemLength]) {
- ++stemLength;
- }
- const offset = source.length - stemLength;
-
- for (const {text, furigana} of jpDistributeFurigana(
- source.slice(0, offset === 0 ? source.length : source.length - offset),
- reading.slice(0, offset === 0 ? reading.length : reading.length - expression.length + stemLength)
- )) {
- term.push({text, reading: furigana || ''});
- }
-
- if (stemLength !== source.length) {
- term.push({text: source.slice(stemLength)});
+ for (const {text, furigana} of jpDistributeFuriganaInflected(expression, reading, source)) {
+ // can't use 'furigana' in templates
+ term.push({text, reading: furigana});
}
-
text = text.slice(source.length);
} else {
term.push({text: text[0]});
@@ -120,6 +105,33 @@ async function apiTextParse(text, optionsContext) {
return results;
}
+async function apiTextParseMecab(text, optionsContext) {
+ const options = await apiOptionsGet(optionsContext);
+ const mecab = utilBackend().mecab;
+
+ const results = [];
+ for (const parsedLine of await mecab.parseText(text)) {
+ for (const {expression, reading, source} of parsedLine) {
+ const term = [];
+ if (expression && reading) {
+ for (const {text, furigana} of jpDistributeFuriganaInflected(
+ expression,
+ jpKatakanaToHiragana(reading),
+ source
+ )) {
+ // can't use 'furigana' in templates
+ term.push({text, reading: furigana});
+ }
+ } else {
+ term.push({text: source});
+ }
+ results.push(term);
+ }
+ results.push([{text: '\n'}]);
+ }
+ return results;
+}
+
async function apiKanjiFind(text, optionsContext) {
const options = await apiOptionsGet(optionsContext);
const definitions = await utilBackend().translator.findKanji(text, options);