fixing syntax issues
This commit is contained in:
parent
b38450b6ce
commit
a4c3ba434a
@ -71,8 +71,8 @@ class Translator {
|
|||||||
expression: definition.expression,
|
expression: definition.expression,
|
||||||
reading: definition.reading,
|
reading: definition.reading,
|
||||||
glossary: definition.glossary,
|
glossary: definition.glossary,
|
||||||
tags: dictTagsSort(tags),
|
tags: dictTagsSort(tags),
|
||||||
furigana: distributeFurigana(definition.expression, definition.reading)
|
furigana: distributeFurigana(definition.expression, definition.reading)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,80 +39,80 @@ function jpKatakanaToHiragana(text) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function distributeFurigana(word, reading) {
|
function distributeFurigana(word, reading) {
|
||||||
reading = reading || wanakana.toHiragana(word);
|
reading = reading || wanakana.toHiragana(word);
|
||||||
function span(str, pred) {
|
function span(str, pred) {
|
||||||
let i = 0;
|
let i = 0;
|
||||||
while (i < str.length && pred(str[i])) {
|
while (i < str.length && pred(str[i])) {
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return [str.substring(0, i), str.substring(i)];
|
return [str.substring(0, i), str.substring(i)];
|
||||||
}
|
}
|
||||||
const isKanji = c => jpIsKanji(c) ||
|
const isKanji = c => jpIsKanji(c) ||
|
||||||
c == "\u3005"; /* kurikaeshi */
|
c === '\u3005'; /* kurikaeshi */
|
||||||
const isKana = c => jpIsKana(c) ||
|
const isKana = c => jpIsKana(c) ||
|
||||||
c == "\u30fc"; /* chouonpu */
|
c === '\u30fc'; /* chouonpu */
|
||||||
function parse(word) {
|
function parse(word) {
|
||||||
const res = [];
|
const res = [];
|
||||||
while (word.length > 0) {
|
while (word.length > 0) {
|
||||||
const c = word.charAt(0);
|
const c = word.charAt(0);
|
||||||
if (isKana(c)) {
|
if (isKana(c)) {
|
||||||
const [text, rest] = span(word, isKana);
|
const [text, rest] = span(word, isKana);
|
||||||
res.push({ type: "kana", text });
|
res.push({ type: 'kana', text });
|
||||||
word = rest;
|
word = rest;
|
||||||
} else if (isKanji(c)) {
|
} else if (isKanji(c)) {
|
||||||
const [text, rest] = span(word, isKanji);
|
const [text, rest] = span(word, isKanji);
|
||||||
res.push({ type: "kanji", text });
|
res.push({ type: 'kanji', text });
|
||||||
word = rest;
|
word = rest;
|
||||||
} else return null;
|
} else return null;
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
let fallback = () => [{ text: word, furigana: reading }];
|
const fallback = () => [{ text: word, furigana: reading }];
|
||||||
let parts = parse(word);
|
const parts = parse(word);
|
||||||
if (parts == null) return fallback();
|
if (!parts) return fallback();
|
||||||
let parti = 0;
|
let parti = 0;
|
||||||
let readingi = 0;
|
let readingi = 0;
|
||||||
let res = [];
|
const res = [];
|
||||||
let current = null;
|
let current = null;
|
||||||
function backtrack() {
|
function backtrack() {
|
||||||
parti--;
|
parti--;
|
||||||
const prev = res.pop();
|
const prev = res.pop();
|
||||||
current = prev.furigana;
|
current = prev.furigana;
|
||||||
}
|
}
|
||||||
while (parti < parts.length) {
|
while (parti < parts.length) {
|
||||||
const part = parts[parti];
|
const part = parts[parti];
|
||||||
switch (part.type) {
|
switch (part.type) {
|
||||||
case 'kana':
|
case 'kana':
|
||||||
if (reading.startsWith(wanakana.toHiragana(part.text), readingi)) {
|
if (reading.startsWith(wanakana.toHiragana(part.text), readingi)) {
|
||||||
if (parti == parts.length - 1 && readingi != reading.length - part.text.length) {
|
if (parti === parts.length - 1 && readingi !== reading.length - part.text.length) {
|
||||||
backtrack();
|
backtrack();
|
||||||
} else {
|
} else {
|
||||||
readingi += part.text.length;
|
readingi += part.text.length;
|
||||||
res.push({ text: part.text });
|
res.push({ text: part.text });
|
||||||
parti++;
|
parti++;
|
||||||
}
|
}
|
||||||
} else backtrack();
|
} else backtrack();
|
||||||
break;
|
break;
|
||||||
case "kanji":
|
case 'kanji':
|
||||||
current = current || "";
|
current = current || '';
|
||||||
if (parti == parts.length - 1) {
|
if (parti === parts.length - 1) {
|
||||||
// last part, consume all
|
// last part, consume all
|
||||||
current += reading.substring(readingi);
|
current += reading.substring(readingi);
|
||||||
} else {
|
} else {
|
||||||
const nextText = parts[parti + 1].text;
|
const nextText = parts[parti + 1].text;
|
||||||
let end = reading.indexOf(nextText, readingi + 1); // consume at least one character
|
const end = reading.indexOf(nextText, readingi + 1); // consume at least one character
|
||||||
if (end == -1) {
|
if (end === -1) {
|
||||||
return fallback();
|
return fallback();
|
||||||
}
|
}
|
||||||
current += reading.substring(readingi, end);
|
current += reading.substring(readingi, end);
|
||||||
readingi = end;
|
readingi = end;
|
||||||
}
|
}
|
||||||
res.push({ text: part.text, furigana: current });
|
res.push({ text: part.text, furigana: current });
|
||||||
current = null;
|
current = null;
|
||||||
parti++;
|
parti++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user