This is morsecode translator for English, Korean, Numbers.
It seems quite easy to translate English and Numbers but Korean. Korean letter is composed with initial sound(cho-sung, 초성), middle sound(joong-sung, 중성), last sound(jong-sung, 종성). So when it is being encoding, it should be seperated each sound and when it is being decoding, it should be combining back for complete letter.
-
each sound:
- initial sound
- "ㄱ", "ㄲ", "ㄴ", "ㄷ", "ㄸ", "ㄹ", "ㅁ", "ㅂ", "ㅃ", "ㅅ", "ㅆ", "ㅇ", "ㅈ", "ㅉ", "ㅊ", "ㅋ", "ㅌ", "ㅍ", "ㅎ"
- middle sound
- "ㅏ", "ㅐ", "ㅑ", "ㅒ", "ㅓ", "ㅔ", "ㅕ", "ㅖ", "ㅗ", "ㅘ", "ㅙ", "ㅚ", "ㅛ", "ㅜ", "ㅝ", "ㅞ", "ㅟ", "ㅠ", "ㅡ", "ㅢ", "ㅣ"
- last sound(it can be empty)
- "", "ㄱ", "ㄲ", "ㄳ", "ㄴ", "ㄵ", "ㄶ", "ㄷ", "ㄹ", "ㄺ", "ㄻ", "ㄼ", "ㄽ", "ㄾ", "ㄿ", "ㅀ", "ㅁ", "ㅂ", "ㅄ", "ㅅ", "ㅆ", "ㅇ", "ㅈ", "ㅊ", "ㅋ", "ㅌ", "ㅍ", "ㅎ"
- initial sound
-
letters:
- initial sound + middle sound
- "가", "까", ..., "개", "깨", ..., "희", "히"
- initial sound + middle sound + last sound
- "각", "깍", ..., "객", "깩", ..., "힢", "힣"
- combination table
ㄱ 가 각 갂 간 ... 갛 개 객 갞 갠 갷 ... 긔 긕 긖 긘 긯 기 긱 긲 긴 깋 x 19 (fo all initial sound)
- initial sound + middle sound
-
Getting unicode
- charCodeAt(0) returns Unicode of a letter
-
Calculating index of each sound from unicode
- Refer to the reference above, the number of possible Korean letter is 19(initial) * 21(middle) * 28(last) and all possible letters are sequantially placed from the initial Korean letter "가" which is composed by "ㄱ"(initial) + "ㅏ"(middle) + ""(last).
- Last sound: The last sound must be a remainder by 28 because the number of last sound is 28.
- Middle sound: To get next sound, minus the result of last sound from unicode of the letter. And then divide by 28 to consider about middle sound. After that getting remainder by 21.
- Initial sound: Same with middle sound until dividing by 28. Minus middle sound to calculate left initial sound. And then divide by 21.
- Refer to the reference above, the number of possible Korean letter is 19(initial) * 21(middle) * 28(last) and all possible letters are sequantially placed from the initial Korean letter "가" which is composed by "ㄱ"(initial) + "ㅏ"(middle) + ""(last).
-
Returning matching morsecode from morsecode map
- Find the letter using the indexes from each sound array
- Getting each sound
- Find each sound from morse code map and stick together to tell that it will be a letter
- Translating to a letter
- Find indexes from each character of sounds
- Calculate unicode using each index.
- '가'.charCodeAt(0) + index of initial sound * 21 * 28 + index of middle sound * 28 + index of last sound
- Translating to character from unicode
- String.fromCharCord()
- Each Korean letter should have two spaces
- If each letter has one space between, it's impossible to recognize combined consonants.
var morsecode = new morsecode();
// encode
// morsecode.encode(normal_string);
morsecode.encode('안녕 세상');
// -.- . ..-. ..-. ... -.- --. -.-- --. . -.-
// decode
// morsecode.decode(morsecode_string, type);
// type: 'kor' || 'eng'
morsecode.decode('-.- . ..-. ..-. ... -.- --. -.-- --. . -.-', 'kor');
// 안녕 세상