summaryrefslogtreecommitdiff
path: root/ext/bg/js/api.js
diff options
context:
space:
mode:
authorsiikamiika <siikamiika@users.noreply.github.com>2019-10-30 03:58:24 +0200
committersiikamiika <siikamiika@users.noreply.github.com>2019-11-23 17:40:52 +0200
commitc35a05cd62d43ff435c022a353de55510b020277 (patch)
treeeed5ca7f70aed856ec2be69b39013202dfcba596 /ext/bg/js/api.js
parentf63e8e4be0e7bdb1a2e45e349bf667ea7ca4adab (diff)
add kana to text
Diffstat (limited to 'ext/bg/js/api.js')
-rw-r--r--ext/bg/js/api.js37
1 files changed, 37 insertions, 0 deletions
diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js
index df73aa2a..064903ca 100644
--- a/ext/bg/js/api.js
+++ b/ext/bg/js/api.js
@@ -79,6 +79,43 @@ async function apiTermsFind(text, details, optionsContext) {
return {length, definitions};
}
+async function apiTextParse(text, optionsContext) {
+ const options = await apiOptionsGet(optionsContext);
+ const translator = utilBackend().translator;
+
+ const results = [];
+ while (text) {
+ let [definitions, length] = await translator.findTerms(text, {}, options);
+ if (definitions.length > 0) {
+ definitions = dictTermsSort(definitions);
+ const {expression, source, reading} = definitions[0];
+
+ let stemLength = source.length;
+ while (source[stemLength - 1] !== expression[stemLength - 1]) {
+ --stemLength;
+ }
+ const offset = source.length - stemLength;
+
+ for (const result of jpDistributeFurigana(
+ source.slice(0, offset === 0 ? source.length : source.length - offset),
+ reading.slice(0, offset === 0 ? reading.length : source.length + (reading.length - expression.length) - offset)
+ )) {
+ results.push(result);
+ }
+
+ if (stemLength !== source.length) {
+ results.push({text: source.slice(stemLength)});
+ }
+
+ text = text.slice(source.length);
+ } else {
+ results.push({text: text[0]});
+ text = text.slice(1);
+ }
+ }
+ return results;
+}
+
async function apiKanjiFind(text, optionsContext) {
const options = await apiOptionsGet(optionsContext);
const definitions = await utilBackend().translator.findKanji(text, options);