aboutsummaryrefslogtreecommitdiff
path: root/ext/mixed/js
diff options
context:
space:
mode:
authorsiikamiika <siikamiika@users.noreply.github.com>2019-11-27 01:20:04 +0200
committersiikamiika <siikamiika@users.noreply.github.com>2019-11-27 01:20:04 +0200
commitc2ff25b0ec2fdb8764a5e9994c1e37bfed7f05c6 (patch)
tree9f990112041512966ff77b006e77bef43f8ffeb4 /ext/mixed/js
parent50604b25e659661a10b9b1a2e268e25447746456 (diff)
use fallback for ambiguous furigana
fixes #281, fixes #94
Diffstat (limited to 'ext/mixed/js')
-rw-r--r--ext/mixed/js/japanese.js22
1 files changed, 19 insertions, 3 deletions
diff --git a/ext/mixed/js/japanese.js b/ext/mixed/js/japanese.js
index a7cd0452..8b841b2e 100644
--- a/ext/mixed/js/japanese.js
+++ b/ext/mixed/js/japanese.js
@@ -91,8 +91,9 @@ function jpDistributeFurigana(expression, reading) {
return fallback;
}
+ let isAmbiguous = false;
const segmentize = (reading, groups) => {
- if (groups.length === 0) {
+ if (groups.length === 0 || isAmbiguous) {
return [];
}
@@ -106,14 +107,25 @@ function jpDistributeFurigana(expression, reading) {
}
}
} else {
+ let foundSegments = null;
for (let i = reading.length; i >= group.text.length; --i) {
const readingUsed = reading.substring(0, i);
const readingLeft = reading.substring(i);
const segs = segmentize(readingLeft, groups.slice(1));
if (segs) {
- return [{text: group.text, furigana: readingUsed}].concat(segs);
+ if (foundSegments !== null) {
+ // more than one way to segmentize the tail, mark as ambiguous
+ isAmbiguous = true;
+ return null;
+ }
+ foundSegments = [{text: group.text, furigana: readingUsed}].concat(segs);
+ }
+ // there is only one way to segmentize the last non-kana group
+ if (groups.length === 1) {
+ break;
}
}
+ return foundSegments;
}
};
@@ -129,7 +141,11 @@ function jpDistributeFurigana(expression, reading) {
}
}
- return segmentize(reading, groups) || fallback;
+ const segments = segmentize(reading, groups);
+ if (segments && !isAmbiguous) {
+ return segments;
+ }
+ return fallback;
}
function jpDistributeFuriganaInflected(expression, reading, source) {