Fix: split multibyte sentence with unicodeIndex

This commit is contained in:
2019-06-25 11:10:57 +09:00
parent cb9ac3a017
commit 5638266178
4 changed files with 144 additions and 37 deletions
+48
View File
@@ -177,6 +177,54 @@ StringUtil.isSameKoreanAlphabet = function(firstAlphabet, secondAlphabet) {
return false; return false;
}; };
StringUtil.isKoreanAlphabet = function(char) {
c = char.charCodeAt(0);
if(0x1100 <= c && c <= 0x11FF)
return true;
if(0x3130 <= c && c <= 0x318F)
return true;
if(0xAC00 <= c && c <= 0xD7A3)
return true;
return false;
};
StringUtil.getUnicodeAlphabetCount = function(text) {
var count = 0;
var textLen = text.length
for(var i = 0; i < textLen; i++) {
if(StringUtil.isKoreanAlphabet(text[i]))
count += 2;
else
count++
}
return count;
};
StringUtil.getMultibyteIndex = function(multibyteText, unicodeCharIndex) {
var multibyteTextIndex = 0;
var index = 0;
while(index < unicodeCharIndex) {
var unicodeChar = multibyteText[multibyteTextIndex];
if(typeof unicodeChar == "undefined")
break;
if(StringUtil.isKoreanAlphabet(unicodeChar)) {
if(index + 2 <= unicodeCharIndex)
index += 2;
else
break;
} else
index += 1;
multibyteTextIndex++;
}
return multibyteTextIndex;
};
StringUtil.getScoreUnit = function(playingAppID) { StringUtil.getScoreUnit = function(playingAppID) {
if(playingAppID == 104) if(playingAppID == 104)
return " 초"; return " 초";
+56 -25
View File
@@ -108,43 +108,73 @@ TypingTextManager.prototype.makeExaminationContents = function(arr) {
if(sentence.length == 0) if(sentence.length == 0)
continue; continue;
if(sentence.length > TypingTextManager.SENTENCE_MAX_CHARACTER) if(sentence.length > TypingTextManager.SENTENCE_MAX_CHARACTER)
this.add(this.makeShortSentences(sentence)); this.add(this.getSplitedSentences(sentence));
else else
this.add(sentence); this.add(sentence);
} }
} }
TypingTextManager.prototype.makeShortSentences = function(sentence) { TypingTextManager.prototype.isWordTooLong = function(word) {
var uniCharCount = StringUtil.getUnicodeAlphabetCount(word);
// console.log("word : " + word + "*");
// console.log("uniCharCount : " + uniCharCount);
if(uniCharCount > TypingTextManager.SENTENCE_MAX_CHARACTER)
return true;
return false;
}
TypingTextManager.prototype.isNotEmptySentence = function(sentence) {
if(sentence.legnth > 0)
return true;
return false;
}
/*
TypingTextManager.prototype.getSplitedWord = function(word, splitIndex) {
var startIndex = splitIndex * TypingTextManager.SENTENCE_MAX_CHARACTER;
var endIndex = (splitIndex + 1) * TypingTextManager.SENTENCE_MAX_CHARACTER;
return word.substring(startIndex, endIndex);
}
*/
TypingTextManager.prototype.getSplitedSentences = function(sentence) {
var words = sentence.split(" "); var words = sentence.split(" ");
var wordCount = words.length; var wordCount = words.length;
var shortSentences = []; var splitedSentences = [];
var sentenceIndex = 0; var sentenceIndex = 0;
shortSentences[sentenceIndex] = ""; splitedSentences[sentenceIndex] = "";
// console.log(sentence); // console.log(sentence);
// console.log(words); // console.log(words);
for(var i = 0; i < wordCount; i++) { for(var i = 0; i < wordCount; i++) {
// console.log(words[i]); // console.log(words[i]);
// console.log(shortSentences[sentenceIndex].length); // console.log(splitedSentences[sentenceIndex].length);
var wordUniCharCount = StringUtil.getUnicodeAlphabetCount(words[i]);
// if a word is longer than SENTENCE_MAX_CHARACTER
// then split the word with SENTENCE_MAX_CHARACTER length
if(this.isWordTooLong(words[i])) {
console.log(words[i]);
if(this.isNotEmptySentence(splitedSentences[sentenceIndex]))
sentenceIndex++; // start with new empty sentence
var wordLength = words[i].length; var tooLongWord = words[i];
if(wordLength > TypingTextManager.SENTENCE_MAX_CHARACTER) { while(typeof tooLongWord != "undefined" && tooLongWord.length > 0) {
// console.log(words[i]); // console.log("tooLongWord before : " + tooLongWord);
if(shortSentences[sentenceIndex].legnth > 0)
sentenceIndex++;
var loopCount = Math.ceil(wordLength / TypingTextManager.SENTENCE_MAX_CHARACTER); var multibyteIndex = StringUtil.getMultibyteIndex(tooLongWord, TypingTextManager.SENTENCE_MAX_CHARACTER);
// console.log("multibyteIndex : " + multibyteIndex);
var splitedSentence = tooLongWord.substring(0, multibyteIndex);
// console.log("splitedSentence : " + splitedSentence);
tooLongWord = tooLongWord.substring(multibyteIndex);
// console.log("tooLongWord : after " + tooLongWord);
for(var j = 0; j < loopCount; j++) { splitedSentences[sentenceIndex] = splitedSentence;
var startIndex = j * TypingTextManager.SENTENCE_MAX_CHARACTER;
var endIndex = (j + 1) * TypingTextManager.SENTENCE_MAX_CHARACTER;
var partialText = words[i].substring(startIndex, endIndex);
// console.log(partialText);
shortSentences[sentenceIndex] = partialText;
sentenceIndex++; sentenceIndex++;
} }
@@ -152,22 +182,23 @@ TypingTextManager.prototype.makeShortSentences = function(sentence) {
} }
// console.log("splitedSentences[" + sentenceIndex + "] : " + splitedSentences[sentenceIndex]);
if(shortSentences[sentenceIndex].length + words[i].length < TypingTextManager.SENTENCE_MAX_CHARACTER) var shortSentenceUniCharCount = StringUtil.getUnicodeAlphabetCount(splitedSentences[sentenceIndex]);
if(shortSentenceUniCharCount + wordUniCharCount < TypingTextManager.SENTENCE_MAX_CHARACTER)
if(i < wordCount - 1) if(i < wordCount - 1)
shortSentences[sentenceIndex] = shortSentences[sentenceIndex].concat(words[i], " "); splitedSentences[sentenceIndex] = splitedSentences[sentenceIndex].concat(words[i], " ");
else // last word else // last word
shortSentences[sentenceIndex] = shortSentences[sentenceIndex].concat(words[i]); splitedSentences[sentenceIndex] = splitedSentences[sentenceIndex].concat(words[i]);
else { else {
sentenceIndex++; sentenceIndex++;
if(i < wordCount - 1) if(i < wordCount - 1)
shortSentences[sentenceIndex] = words[i] + " "; splitedSentences[sentenceIndex] = words[i] + " ";
else // last word else // last word
shortSentences[sentenceIndex] = words[i]; splitedSentences[sentenceIndex] = words[i];
} }
} }
return shortSentences; return splitedSentences;
} }
@@ -205,4 +236,4 @@ TypingTextManager.ENGLISH_LETTER_RIGHT = [
]; ];
TypingTextManager.SENTENCE_MAX_CHARACTER = 28; TypingTextManager.SENTENCE_MAX_CHARACTER = 40;
+1 -1
View File
@@ -45,7 +45,7 @@
<script src="../../game/lib/util/number_util.js"></script> <script src="../../game/lib/util/number_util.js"></script>
<script src="../../game/lib/util/record_util.js"></script> <script src="../../game/lib/util/record_util.js"></script>
<script src="../../game/lib/text/input_type_text.js"></script> <script src="../../game/lib/text/input_type_text.js?update=20190622"></script>
<script src="../../game/lib/text/time_over_text.js"></script> <script src="../../game/lib/text/time_over_text.js"></script>
<script src="../../game/lib/text/mission_clear_text.js"></script> <script src="../../game/lib/text/mission_clear_text.js"></script>
<script src="../../game/lib/text/screen_top_ui.js"></script> <script src="../../game/lib/text/screen_top_ui.js"></script>
+39 -11
View File
@@ -1,3 +1,36 @@
//////////////////////////////////////////////////
// StringUtil
QUnit.test( "StringUtil", function( assert ) {
var englishText = "Is hangle character?";
var koreanText = "한글 단어 입니까?";
var englishKoreanText = "Is 한글 character 입니까?";
assert.equal(StringUtil.isKoreanAlphabet(englishText[0]), false, "StringUtil - isKoreanAlphabet");
assert.equal(StringUtil.isKoreanAlphabet(koreanText[0]), true, "StringUtil - isKoreanAlphabet");
assert.equal(StringUtil.getUnicodeAlphabetCount(englishText), 20, "StringUtil - getUnicodeAlphabetCount");
assert.equal(StringUtil.getUnicodeAlphabetCount(koreanText), 17, "StringUtil - getUnicodeAlphabetCount");
assert.equal(StringUtil.getMultibyteIndex(englishText, 0), 0, "StringUtil - getMultibyteIndex, eng");
assert.equal(StringUtil.getMultibyteIndex(englishText, 1), 1, "StringUtil - getMultibyteIndex, eng");
assert.equal(StringUtil.getMultibyteIndex(englishText, 3), 3, "StringUtil - getMultibyteIndex, eng");
assert.equal(StringUtil.getMultibyteIndex(koreanText, 0), 0, "StringUtil - getMultibyteIndex, kor1");
assert.equal(StringUtil.getMultibyteIndex(koreanText, 1), 0, "StringUtil - getMultibyteIndex, kor2");
assert.equal(StringUtil.getMultibyteIndex(koreanText, 2), 1, "StringUtil - getMultibyteIndex, kor3");
assert.equal(StringUtil.getMultibyteIndex(koreanText, 3), 1, "StringUtil - getMultibyteIndex, kor4");
assert.equal(StringUtil.getMultibyteIndex(koreanText, 4), 2, "StringUtil - getMultibyteIndex, kor5");
assert.equal(StringUtil.getMultibyteIndex(koreanText, 5), 3, "StringUtil - getMultibyteIndex, kor6");
assert.equal(StringUtil.getMultibyteIndex(koreanText, 6), 3, "StringUtil - getMultibyteIndex, kor7");
assert.equal(StringUtil.getMultibyteIndex(englishKoreanText, 0), 0, "StringUtil - getMultibyteIndex, engKor1");
assert.equal(StringUtil.getMultibyteIndex(englishKoreanText, 1), 1, "StringUtil - getMultibyteIndex, engKor1");
assert.equal(StringUtil.getMultibyteIndex(englishKoreanText, 2), 2, "StringUtil - getMultibyteIndex, engKor1");
assert.equal(StringUtil.getMultibyteIndex(englishKoreanText, 3), 3, "StringUtil - getMultibyteIndex, engKor2");
assert.equal(StringUtil.getMultibyteIndex(englishKoreanText, 4), 3, "StringUtil - getMultibyteIndex, engKor3");
assert.equal(StringUtil.getMultibyteIndex(englishKoreanText, 5), 4, "StringUtil - getMultibyteIndex, engKor4");
});
////////////////////////////////////////////////// //////////////////////////////////////////////////
// DateUtil // DateUtil
@@ -317,17 +350,12 @@ QUnit.test( "TypingTextManager - makeExaminationContents", function( assert ) {
typingTextMan.makeExaminationContents(sentences); typingTextMan.makeExaminationContents(sentences);
let contents = typingTextMan.getContents(); let contents = typingTextMan.getContents();
assert.equal(contents[0], "냉면 / 김남천", "addTextArray - line #1"); assert.equal(contents[0], "냉면 / 김남천", "addTextArray - line 1");
assert.equal(contents[1], "'냉면'이라는 말에 '평양'이 붙어서 ", "addTextArray - line #2"); assert.equal(contents[1], "'냉면'이라는 말에 '평양'이 붙어서 ", "addTextArray - line 2");
assert.equal(contents[2], "'평양냉면'이라야 비로소 어울리는 격에 맞는 말이 ", "addTextArray - line #3"); assert.equal(contents[2], "'평양냉면'이라야 비로소 어울리는 격에 ", "addTextArray - line 3");
assert.equal(contents[3], "되듯이 냉면은 평양에 있어 대표적인 음식이다. ", "addTextArray - line #4"); assert.equal(contents[3], "맞는 말이 되듯이 냉면은 평양에 있어 ", "addTextArray - line 4");
assert.equal(contents[4], "언제부터 이 냉면이 평양에 들어왔으며 언제부터 ", "addTextArray - line #5"); assert.equal(contents[10], "가나다라마바사아자차카타파하가나다라마바", "addTextArray - line 5");
assert.equal(contents[5], "냉면이 평안도 사람의 입에 가장 많이 기호에 맞는 ", "addTextArray - line #6"); assert.equal(contents[11], "사아자차카타파하가나다라마바사아자차카타", "addTextArray - line 6");
assert.equal(contents[6], "음식물이 되었는지는 나 같은 무식쟁이에게는 알 ", "addTextArray - line #7");
assert.equal(contents[7], "수도 없고 또 알려고도 아니한다.", "addTextArray - line #8");
assert.equal(contents[8], "가나다라마바사아자차카타파하가나다라마바사아자차카타파하", "addTextArray - line #9");
assert.equal(contents[9], "가나다라마바사아자차카타파하가나다라마바사아자차카타파하", "addTextArray - line #10");
assert.equal(contents[10], "가나다라마바사아자차카타파하가나다라마바사아자차카타파하", "addTextArray - line #11");
}); });
QUnit.test( "TypingTextManager - add", function( assert ) { QUnit.test( "TypingTextManager - add", function( assert ) {