JapaneseUtil updates (#1813)

* Rename test file

* Use shorthand

* Add support checking functions

* Remove convertReading from JapaneseUtil
This commit is contained in:
toasted-nutbread 2021-07-09 17:31:16 -04:00 committed by GitHub
parent daa44a8389
commit 0d16709547
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 66 deletions

View File

@ -1066,7 +1066,7 @@ class Backend {
if ( if (
dictionaryEntries.length > 0 && dictionaryEntries.length > 0 &&
originalTextLength > 0 && originalTextLength > 0 &&
(originalTextLength !== character.length || this._japaneseUtil.isCodePointJapanese(codePoint)) (originalTextLength !== character.length || jp.isCodePointJapanese(codePoint))
) { ) {
previousUngroupedSegment = null; previousUngroupedSegment = null;
const {headwords: [{term, reading}]} = dictionaryEntries[0]; const {headwords: [{term, reading}]} = dictionaryEntries[0];

View File

@ -208,8 +208,6 @@ class QueryParser extends EventDispatcher {
} }
_createParseResult(data) { _createParseResult(data) {
const jp = this._japaneseUtil;
const readingMode = this._readingMode;
const fragment = document.createDocumentFragment(); const fragment = document.createDocumentFragment();
for (const term of data) { for (const term of data) {
const termNode = document.createElement('span'); const termNode = document.createElement('span');
@ -218,7 +216,7 @@ class QueryParser extends EventDispatcher {
if (reading.length === 0) { if (reading.length === 0) {
termNode.appendChild(document.createTextNode(text)); termNode.appendChild(document.createTextNode(text));
} else { } else {
const reading2 = jp.convertReading(text, reading, readingMode); const reading2 = this._convertReading(text, reading);
termNode.appendChild(this._createSegment(text, reading2)); termNode.appendChild(this._createSegment(text, reading2));
} }
} }
@ -245,4 +243,26 @@ class QueryParser extends EventDispatcher {
return segmentNode; return segmentNode;
} }
_convertReading(term, reading) {
switch (this._readingMode) {
case 'hiragana':
return this._japaneseUtil.convertKatakanaToHiragana(reading);
case 'katakana':
return this._japaneseUtil.convertHiraganaToKatakana(reading);
case 'romaji':
if (this._japaneseUtil.convertToRomajiSupported()) {
if (reading.length > 0) {
return this._japaneseUtil.convertToRomaji(reading);
} else if (this._japaneseUtil.isStringEntirelyKana(term)) {
return this._japaneseUtil.convertToRomaji(term);
}
}
return reading;
case 'none':
return '';
default:
return reading;
}
}
} }

View File

@ -277,6 +277,10 @@ const JapaneseUtil = (() => {
return this._getWanakana().toKana(text); return this._getWanakana().toKana(text);
} }
convertToKanaSupported() {
return this._wanakana !== null;
}
convertKatakanaToHiragana(text) { convertKatakanaToHiragana(text) {
let result = ''; let result = '';
const offset = (HIRAGANA_CONVERSION_RANGE[0] - KATAKANA_CONVERSION_RANGE[0]); const offset = (HIRAGANA_CONVERSION_RANGE[0] - KATAKANA_CONVERSION_RANGE[0]);
@ -315,25 +319,8 @@ const JapaneseUtil = (() => {
return wanakana.toRomaji(text); return wanakana.toRomaji(text);
} }
convertReading(term, reading, readingMode) { convertToRomajiSupported() {
switch (readingMode) { return this._wanakana !== null;
case 'hiragana':
return this.convertKatakanaToHiragana(reading);
case 'katakana':
return this.convertHiraganaToKatakana(reading);
case 'romaji':
if (reading.length > 0) {
return this.convertToRomaji(reading);
} else if (this.isStringEntirelyKana(term)) {
return this.convertToRomaji(term);
} else {
return reading;
}
case 'none':
return '';
default:
return reading;
}
} }
convertNumericToFullWidth(text) { convertNumericToFullWidth(text) {
@ -426,6 +413,10 @@ const JapaneseUtil = (() => {
return result; return result;
} }
convertAlphabeticToKanaSupported() {
return this._wanakana !== null;
}
// Furigana distribution // Furigana distribution
distributeFurigana(term, reading) { distributeFurigana(term, reading) {

View File

@ -171,48 +171,6 @@ function testConvertToRomaji() {
} }
} }
function testConvertReading() {
const data = [
[['アリガトウ', 'アリガトウ', 'hiragana'], 'ありがとう'],
[['アリガトウ', 'アリガトウ', 'katakana'], 'アリガトウ'],
[['アリガトウ', 'アリガトウ', 'romaji'], 'arigatou'],
[['アリガトウ', 'アリガトウ', 'none'], ''],
[['アリガトウ', 'アリガトウ', 'default'], 'アリガトウ'],
[['ありがとう', 'ありがとう', 'hiragana'], 'ありがとう'],
[['ありがとう', 'ありがとう', 'katakana'], 'アリガトウ'],
[['ありがとう', 'ありがとう', 'romaji'], 'arigatou'],
[['ありがとう', 'ありがとう', 'none'], ''],
[['ありがとう', 'ありがとう', 'default'], 'ありがとう'],
[['有り難う', 'ありがとう', 'hiragana'], 'ありがとう'],
[['有り難う', 'ありがとう', 'katakana'], 'アリガトウ'],
[['有り難う', 'ありがとう', 'romaji'], 'arigatou'],
[['有り難う', 'ありがとう', 'none'], ''],
[['有り難う', 'ありがとう', 'default'], 'ありがとう'],
// Cases with falsy readings
[['ありがとう', '', 'hiragana'], ''],
[['ありがとう', '', 'katakana'], ''],
[['ありがとう', '', 'romaji'], 'arigatou'],
[['ありがとう', '', 'none'], ''],
[['ありがとう', '', 'default'], ''],
// Cases with falsy readings and kanji terms
[['有り難う', '', 'hiragana'], ''],
[['有り難う', '', 'katakana'], ''],
[['有り難う', '', 'romaji'], ''],
[['有り難う', '', 'none'], ''],
[['有り難う', '', 'default'], '']
];
for (const [[term, reading, readingMode], expected] of data) {
assert.strictEqual(jp.convertReading(term, reading, readingMode), expected);
}
}
function testConvertNumericToFullWidth() { function testConvertNumericToFullWidth() {
const data = [ const data = [
['0123456789', ''], ['0123456789', ''],
@ -888,7 +846,6 @@ function main() {
testConvertKatakanaToHiragana(); testConvertKatakanaToHiragana();
testConvertHiraganaToKatakana(); testConvertHiraganaToKatakana();
testConvertToRomaji(); testConvertToRomaji();
testConvertReading();
testConvertNumericToFullWidth(); testConvertNumericToFullWidth();
testConvertHalfWidthKanaToFullWidth(); testConvertHalfWidthKanaToFullWidth();
testConvertAlphabeticToKana(); testConvertAlphabeticToKana();