Created
December 3, 2015 08:40
-
-
Save yoo2001818/5207dc8dcb15512384f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Hangul = { | |
startChar: 'ㄱㄲㄴㄷㄸㄹㅁㅂㅃㅅㅆㅇㅈㅉㅊㅋㅌㅍㅎ'.split(''), | |
middleChar: 'ㅏㅐㅑㅒㅓㅔㅕㅖㅗㅘㅙㅚㅛㅜㅝㅞㅟㅠㅡㅢㅣ'.split(''), | |
endChar: ' ㄱㄲㄳㄴㄵㄶㄷㄹㄺㄻㄼㄽㄾㄿㅀㅁㅂㅄㅅㅆㅇㅈㅊㅋㅌㅍㅎ'.split(''), | |
code: '가'.charCodeAt(0) | |
} | |
var HanConv = { | |
start: 'rRseEfaqQtTdwWczxvg'.split(''), | |
middle: 'k,o,i,O,j,p,u,P,h,hk,ho,hl,y,n,nj,np,nl,b,m,ml,l'.split(','), | |
end: ',r,R,rt,s,sw,sg,e,f,fr,fa,fq,ft,fx,fv,fg,a,q,qt,t,T,d,w,c,z,x,v,g'.split(','), | |
consonant: 'qwertasdfgzxcv'.split(''), | |
vowel: 'yuiophjklbnm'.split('') | |
} | |
function isVowel (char) { | |
return HanConv.vowel.indexOf(char.toLowerCase()) != -1; | |
} | |
function isConsonant (char) { | |
return HanConv.consonant.indexOf(char.toLowerCase()) != -1; | |
} | |
function getStartIndex (char) { | |
var orig = HanConv.start.indexOf(char); | |
if(orig != -1) return orig; | |
return HanConv.start.indexOf(char.toLowerCase()); | |
} | |
function getMiddleIndex (char) { | |
var orig = HanConv.middle.indexOf(char); | |
if(orig != -1) return orig; | |
return HanConv.middle.indexOf(char.toLowerCase()); | |
} | |
function getEndIndex (char) { | |
var orig = HanConv.end.indexOf(char); | |
if(orig != -1) return orig; | |
orig = HanConv.end.indexOf(char.toLowerCase()); | |
if(orig != -1) return orig; | |
return 0; | |
} | |
function createHangul (start, middle, end) { | |
if (typeof start === 'string' || start instanceof String) | |
start = Hangul.startChar.indexOf(start); | |
if (typeof middle === 'string' || middle instanceof String) | |
middle = Hangul.middleChar.indexOf(middle); | |
if (typeof end === 'string' || end instanceof String) | |
end = Hangul.endChar.indexOf(end); | |
var startCode = start * Hangul.middleChar.length * Hangul.endChar.length; | |
var middleCode = middle * Hangul.endChar.length; | |
var endCode = end; | |
return String.fromCharCode(Hangul.code + startCode + middleCode + endCode); | |
} | |
var HangulIME = function () { | |
this.startCode = -1; | |
this.middleCode = -1; | |
this.endCode = 0; | |
this.queue = ''; | |
this.errors = 0; | |
} | |
HangulIME.prototype.getChar = function () { | |
if (this.middleCode == -1) { | |
this.errors ++; | |
return Hangul.startChar[this.startCode]; | |
} else if (this.startCode == -1) { | |
this.errors ++; | |
return Hangul.middleChar[this.middleCode]; | |
} else { | |
return createHangul(this.startCode, this.middleCode, this.endCode); | |
} | |
} | |
HangulIME.prototype.pushQueue = function () { | |
var char = this.getChar(); | |
if(char != undefined) this.queue += char; | |
} | |
HangulIME.prototype.reset = function () { | |
this.startCode = -1; | |
this.middleCode = -1; | |
this.endCode = 0; | |
this.errors = 0; | |
} | |
HangulIME.prototype.next = function (char) { | |
if(isConsonant(char)) { | |
if(this.startCode == -1) { | |
// Start code is not set yet | |
this.startCode = getStartIndex(char); | |
} else if(this.middleCode == -1) { | |
// Middle code is not set yet | |
// Just reset | |
this.pushQueue(); | |
this.reset(); | |
this.startCode = getStartIndex(char); | |
} else { | |
// Set end code | |
var endTemp = getEndIndex(HanConv.end[this.endCode] + char); | |
if(endTemp == 0) { | |
// requested character does not exist | |
this.pushQueue(); | |
this.reset(); | |
this.startCode = getStartIndex(char); | |
} else { | |
this.endCode = endTemp; | |
} | |
} | |
} else if(isVowel(char)) { | |
if(this.endCode == 0) { | |
// Set middle code | |
var middleTemp; | |
if(this.middleCode == -1) { | |
middleTemp = getMiddleIndex(char); | |
} else { | |
middleTemp = getMiddleIndex(HanConv.middle[this.middleCode] + char); | |
} | |
if(middleTemp == -1) { | |
// Start new char | |
this.pushQueue(); | |
this.reset(); | |
this.middleCode = getMiddleIndex(char); | |
} else { | |
this.middleCode = middleTemp; | |
} | |
} else { | |
// Try to 'subtract' end code | |
var endChar = HanConv.end[this.endCode]; | |
var newStart; | |
if(endChar.length == 1) { | |
this.endCode = 0; | |
newStart = getStartIndex(endChar); | |
} else { | |
this.endCode = getEndIndex(endChar.slice(0, endChar.length-1)); | |
newStart = getStartIndex(endChar.slice(endChar.length-1)); | |
} | |
this.pushQueue(); | |
this.reset(); | |
this.startCode = newStart; | |
this.middleCode = getMiddleIndex(char); | |
} | |
} else { | |
// Finish char right now | |
this.pushQueue(); | |
this.reset(); | |
this.queue += char; | |
} | |
} | |
HangulIME.prototype.end = function() { | |
this.pushQueue(); | |
this.reset(); | |
} | |
var ime = new HangulIME(); | |
var sequence = ' xmrtnanswksms akrdlei!!!!!!!'; | |
for(var i = 0; i < sequence.length; ++i) { | |
ime.next(sequence.charAt(i)); | |
console.log(sequence.charAt(i)+' '+ime.queue + '_' + ime.getChar()); | |
} | |
ime.end(); | |
console.log('$ ' + ime.queue); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment