- you could see my blog here.
- you could see my website here
- you can also download this application from App Store!
Have you ever experienced typing in korean using an English keyboard layout? What if you do not recognize this mistakes until you have finished typing?
This tool would be really helpful for you in such cases!
This algorithm easily divides into three parts:
- Matching English Characters to Korean Characters.
dkssud->'ㅇ ㅏ ㄴ ㄴ ㅕ ㅇ'- Making a syllable of Korean.
'ㅇ ㅏ ㄴ ㄴ ㅕ ㅇ'->['ㅇ ㅏ ㄴ','ㄴ ㅕ ㅇ']- Changing a syllable to Hangul.
['ㅇ ㅏ ㄴ','ㄴ ㅕ ㅇ']->'안녕'- Matching English Characters to Korean Characters.
Think of performance in JavaScript, Pick the Map data format for matching each character.
exportconstKOREAN_DICTIONARY=newMap([['q','ㅂ'],['Q','ㅃ'],['w','ㅈ'],['W','ㅉ'],['e','ㄷ'],['E','ㄸ'],['r','ㄱ'],['R','ㄲ'],['t','ㅅ'],['T','ㅆ'],['y','ㅛ'],['u','ㅕ'],['i','ㅑ'],['o','ㅐ'],['p','ㅔ'],['a','ㅁ'],['s','ㄴ'],['d','ㅇ'],['f','ㄹ'],['g','ㅎ'],['h','ㅗ'],['j','ㅓ'],['k','ㅏ'],['l','ㅣ'],['z','ㅋ'],['x','ㅌ'],['c','ㅊ'],['v','ㅍ'],['b','ㅠ'],['n','ㅜ'],['m','ㅡ'],['O','ㅒ'],['P','ㅖ'],['Y','ㅛ'],['U','ㅕ'],['I','ㅑ'],['H','ㅗ'],['J','ㅓ'],['K','ㅏ'],['L','ㅣ'],['B','ㅠ'],['N','ㅜ'],['M','ㅡ'],['A','ㅁ'],['S','ㄴ'],['D','ㅇ'],['F','ㄹ'],['G','ㅎ'],['Z','ㅋ'],['X','ㅌ'],['C','ㅊ'],['V','ㅍ']])/** get a korean character matched an english character*/exportconsthandleEnglishCharacterMatch=(englishText: string)=>{letkoreanTextArray: string[]=[];for(constcharofenglishText){constmatchedKoreanChar=KOREAN_DICTIONARY.get(char);if(matchedKoreanChar){koreanTextArray.push(matchedKoreanChar);}else{koreanTextArray.push(char)}}returnkoreanTextArray;}- Making a syllable of Korean.
It is a main part to make a perfect syllable in Korean.
constisJungseong=(char: string)=>JUNGSEONG.has(char);constisChoseong=(char: string)=>CHOSEONG.has(char);constisJongseong=(char: string)=>JONGSEONG.has(char);/** Check if the current character is the end of a word */constisEndOfWord=(currentChar: string,nextChar: string|undefined,prevChar: string|undefined): boolean=>{// 중성 + 초성if(isJungseong(currentChar)&&nextChar&&isChoseong(nextChar)){returntrue;}// 종성 + 초성if(isJongseong(currentChar)&&nextChar&&isChoseong(nextChar)){returntrue;}// 초성 + 초성 or 초성 + undefinedif(isChoseong(currentChar)&&(!nextChar||isChoseong(nextChar))){returntrue;}// 초성 + 중성 + 종성if(prevChar&&isChoseong(prevChar)&&isJungseong(currentChar)&&nextChar&&(isChoseong(nextChar)||isJongseong(nextChar))){returntrue;}returnfalse;};/** split korean characters into syllables */koreanCharacterArray.reduce((acc: string[],char,i,array)=>{constnextChar=array[i+1];constprevChar=array[i-1];// 현재 문자와 다음 문자를 기준으로 음절 경계를 판단하여 음절을 분리if(i===array.length-1||isEndOfWord(char,nextChar,prevChar)){acc.push(array.slice(start,i+1).join(''));start=i+1;}// 자음 하나로 묶기: 단일 자음이 있는 경우 이전 음절과 합침constlastWord=acc[acc.length-1];if(lastWord&&lastWord.length===1&&isChoseong(lastWord)&&acc.length>1){constmergedWord=acc[acc.length-2]+lastWord;acc.splice(acc.length-2,2,mergedWord);}returnacc;},words);especially in Korea, we have double consonants and double vowels. So it is necessary to handle them separately.
/** convert dubble letter */constprocessWord=(word: string): string=>{constwordArray=word.split('');constprocessDoubleLetter=(arr: string[],startIndex: number,isDoubleFunc: (char: string)=>boolean,makeListFunc: (subArr: string[])=>string): void=>{if(isDoubleFunc(arr[startIndex])&&isDoubleFunc(arr[startIndex+1])){constoriginal=arr[startIndex];arr[startIndex]=makeListFunc(arr.slice(startIndex,startIndex+2));if(original!==arr[startIndex]){arr.splice(startIndex+1,1);}}};if(wordArray.length>2&&isChoseong(wordArray[0])&&isJungseong(wordArray[1])){processDoubleLetter(wordArray,1,isJungseong,makeJungseongList);}if(wordArray.length>=4&&isJongseong(wordArray[2])&&isJongseong(wordArray[3])){processDoubleLetter(wordArray,2,isJongseong,makeJongseongList);}returnwordArray.join('');};- Changing a syllable to Hangul.
It is simple, just use the unicode logic.
/** change split character to korean */constcombineKoreanCharacter=(characterArray: string[]): string=>{const[choseong,jungseong,jongseong]=characterArray;constchoseongIndex=CHOSEONG.get(choseong)asnumber;constjungseongIndex=JUNGSEONG.get(jungseong)asnumber;constjongseongIndex=JONGSEONG.get(jongseong)||0;consthangulChar=String.fromCharCode(0xAC00+(choseongIndex*21*28)+(jungseongIndex*28)+jongseongIndex)returnhangulChar;}
