add reading modes

This commit is contained in:
siikamiika 2019-11-13 20:24:11 +02:00
parent f625321650
commit cc8221c6ea
5 changed files with 58 additions and 7 deletions

View File

@ -92,12 +92,13 @@ async function apiTextParse(text, optionsContext) {
const {expression, reading} = definitions[0];
const source = text.slice(0, sourceLength);
for (const {text, furigana} of jpDistributeFuriganaInflected(expression, reading, source)) {
// can't use 'furigana' in templates
term.push({text, reading: furigana});
const reading = jpConvertReading(text, furigana, options.parsing.readingMode);
term.push({text, reading});
}
text = text.slice(source.length);
} else {
term.push({text: text[0]});
const reading = jpConvertReading(text[0], null, options.parsing.readingMode);
term.push({text: text[0], reading});
text = text.slice(1);
}
results.push(term);
@ -122,11 +123,12 @@ async function apiTextParseMecab(text, optionsContext) {
jpKatakanaToHiragana(reading),
source
)) {
// can't use 'furigana' in templates
term.push({text, reading: furigana});
const reading = jpConvertReading(text, furigana, options.parsing.readingMode);
term.push({text, reading});
}
} else {
term.push({text: source});
const reading = jpConvertReading(source, null, options.parsing.readingMode);
term.push({text: source, reading});
}
result.push(term);
}

View File

@ -314,7 +314,8 @@ function profileOptionsCreateDefaults() {
parsing: {
enableScanningParser: true,
enableMecabParser: false,
selectedParser: null
selectedParser: null,
readingMode: 'hiragana'
},
anki: {

View File

@ -66,6 +66,7 @@ async function formRead(options) {
options.parsing.enableScanningParser = $('#parsing-scan-enable').prop('checked');
options.parsing.enableMecabParser = $('#parsing-mecab-enable').prop('checked');
options.parsing.readingMode = $('#parsing-reading-mode').val();
const optionsAnkiEnableOld = options.anki.enable;
options.anki.enable = $('#anki-enable').prop('checked');
@ -131,6 +132,7 @@ async function formWrite(options) {
$('#parsing-scan-enable').prop('checked', options.parsing.enableScanningParser);
$('#parsing-mecab-enable').prop('checked', options.parsing.enableMecabParser);
$('#parsing-reading-mode').val(options.parsing.readingMode);
$('#anki-enable').prop('checked', options.anki.enable);
$('#card-tags').val(options.anki.tags.join(' '));

View File

@ -437,6 +437,15 @@
<div class="checkbox">
<label><input type="checkbox" id="parsing-mecab-enable"> Enable text parsing using MeCab</label>
</div>
<div class="form-group">
<label for="parsing-reading-mode">Reading mode</label>
<select class="form-control" id="parsing-reading-mode">
<option value="hiragana">ひらがな</option>
<option value="katakana">カタカナ</option>
<option value="romaji">Romaji</option>
</select>
</div>
</div>
<div class="ignore-form-changes">

View File

@ -48,6 +48,43 @@ function jpKatakanaToHiragana(text) {
return result;
}
function jpHiraganaToKatakana(text) {
let result = '';
for (const c of text) {
if (wanakana.isHiragana(c)) {
result += wanakana.toKatakana(c);
} else {
result += c;
}
}
return result;
}
function jpToRomaji(text) {
return wanakana.toRomaji(text);
}
function jpConvertReading(expressionFragment, readingFragment, readingMode) {
switch (readingMode) {
case 'hiragana':
return jpKatakanaToHiragana(readingFragment || '');
case 'katakana':
return jpHiraganaToKatakana(readingFragment || '');
case 'romaji':
if (readingFragment) {
return jpToRomaji(readingFragment);
} else {
if (jpIsKana(expressionFragment)) {
return jpToRomaji(expressionFragment);
}
}
return readingFragment;
default:
return readingFragment;
}
}
function jpDistributeFurigana(expression, reading) {
const fallback = [{furigana: reading, text: expression}];
if (!reading) {