refactor odaka function

This commit is contained in:
Ren Tatsumoto 2024-02-07 23:31:26 +03:00
parent 977b5a28d5
commit 26b07153ac

View file

@ -31,7 +31,7 @@ Fri Jul 7 06:33:50 PM UTC 2023
</div> <!-- /wrap --> </div> <!-- /wrap -->
<div style="display:none;"> <div style="display:none;">
<div id="vocab_kanji">{{VocabKanji}}</div> <div id="vocab_kanji_hidden">{{VocabKanji}}</div>
<div id="pitchnum_hidden">{{VocabPitchNum}}</div> <div id="pitchnum_hidden">{{VocabPitchNum}}</div>
<div id="kanaword_hidden">{{kana:VocabFurigana}}</div> <div id="kanaword_hidden">{{kana:VocabFurigana}}</div>
</div> </div>
@ -82,30 +82,16 @@ Fri Jul 7 06:33:50 PM UTC 2023
function odaka(pitch_num) { function odaka(pitch_num) {
// word is odaka if number of moras is equal to pitch accent position // word is odaka if number of moras is equal to pitch accent position
const vocab_kana = document.getElementById("kanaword_hidden"); const moras = document.getElementById("kanaword_hidden").innerText
if (vocab_kana === null) {
return false;
}
let moras = vocab_kana.innerHTML
.replace(/[(].*[)]/, "") // remove (お), (する), <する>, <な>, etc .replace(/[(].*[)]/, "") // remove (お), (する), <する>, <な>, etc
.replace(/[ャュョゃゅょ]/g, "") // small っ is one mora; ゃゅょ are parts of single mora .replace(/[ャュョゃゅょ]/g, "") // small っ is one mora; ゃゅょ are parts of single mora
.trim(); .trim();
let n_moras;
// first determine if な is part of pronunciation or な-adjective (e.g. 女) // first determine if な is part of pronunciation or な-adjective (e.g. 女)
const vocab_kanji = document.getElementById("vocab_kanji").innerHTML.trim(); const vocab_kanji = document.getElementById("vocab_kanji_hidden")
if (/な$/.test(vocab_kanji)) { .innerText
// な-adjectives don't change pitch .trim();
n_moras = moras.replace(/な$/, "").length; const n_moras = (/な$/.test(vocab_kanji)) ? moras.replace(/な$/, "").length : moras.length;
} else { return (n_moras == pitch_num);
n_moras = moras.length;
}
if (n_moras == pitch_num) {
return true;
} else {
return false;
}
} }
/* Splits tags into separate divs */ /* Splits tags into separate divs */