Created
October 9, 2014 16:42
-
-
Save kiding/6d7a0e37a7bd7c4ac734 to your computer and use it in GitHub Desktop.
한글 조사 자동 선택 스크립트
This file contains hidden or 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
/** | |
* 한글조사 - 은/는 이/가 을/를 과/와 이여/여 | |
* @author Dongsung Don Kim <[email protected]> | |
* @license MIT | |
*/ | |
// TODO: 으로/로 | |
var hangulJosa = (function() { | |
var reT = /[0136-80136-8L-NRL-NRㄱ-ㅎ\uFFA1-\uFFBE\u3165-\u3186\u1100-\u115E\u11A8-\u11FF]/, | |
// 영일삼육칠팔 반각 전각, 대문자 알파벳 엘엠엔 반각 전각, 자음, 반각 자금, 옛자음, 조합 초성, 조합 종성 | |
reF = /[24592459A-KO-QS-ZA-KO-QS-Zㅏ-ㅣ\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC\u3187-\u318E\u1161-\u11A7]/; | |
// 이사오구 반각 전각, 대문자 알파벳 엘엠엔 제외 반각 전각, 모음, 반각 모음(4), 옛모음, 조합 중성 | |
// 종성이 있는가? | |
var jong = function(input) { | |
input += ''; // string으로 변환 | |
var last = input.substr(-1); // 마지막 글자 | |
if (last >= '가' && last <= '힇') { // 확장완성 가-힇 | |
return !!((last.charCodeAt() - 0xAC00) % 28); // 종성 코드를 boolean으로 변환 | |
} else if (reT.test(last)) { | |
return true; | |
} else if (reF.test(last)) { | |
return false; | |
} | |
// TODO: 그 외 언어/글자는 Hangulize? | |
} | |
return { | |
iGa: function(input) { | |
var j = jong(input); | |
return j ? '이' : j+1 ? '가' : '이(가)'; | |
}, | |
eulReul: function(input) { | |
var j = jong(input); | |
return j ? '을' : j+1 ? '를' : '을(를)'; | |
}, | |
eunNeun: function(input) { | |
var j = jong(input); | |
return j ? '은' : j+1 ? '는' : '은(는)'; | |
}, | |
gwaWa: function(input) { | |
var j = jong(input); | |
return j ? '과' : j+1 ? '와' : '과(와)'; | |
}, | |
iyeoYeo: function(input) { | |
var j = jong(input); | |
return j ? '이여' : j+1 ? '여' : '(이)여'; | |
}, | |
}; | |
})(); | |
/** | |
* TEST CASES | |
*/ | |
(function() { | |
var items = [], | |
cases = [ | |
'한글', | |
'\u1112\u1161\u11AB\u1100\u1173\u11AF', // '한글' | |
'백두산', | |
'\u1107\u1162\u11A8\u1103\u116E\u1109\u1161\u11AB', // '백두산' | |
'박사', | |
'\u1107\u1161\u11A8\u1109\u1161', // '박사' | |
'자음', | |
'\u110C\u1161\u110B\u1173\u11B7', // '자음' | |
'ㅋ', | |
'\uFFBB', // 반각 'ㅋ' | |
'\u110F', // 조합 초성 'ㅋ' | |
'\u11BF', // 조합 초성 'ㅋ' | |
'ㅏ', | |
'\uFFC2', // 반각 'ㅏ' | |
'\u1161', // 조합 중성 'ㅏ' | |
'\uFFCC', // 반각 'ㅗ' | |
'\uFFD3', // 반각 'ㅜ' | |
'\uFFDB', // 반각 'ㅢ' | |
'123', | |
'123', // 전각 | |
'789', | |
'789', // 전각 | |
'0', | |
'0', // 전각 | |
'태권V', | |
'태권V', // 전각 | |
'PQR', | |
'PQR', // 전각 | |
]; | |
console.log('==이/가=='); | |
items = []; | |
cases.forEach(function(t) { | |
items.push(t + hangulJosa.iGa(t)); | |
}); | |
console.log(items.join(' / ')); | |
console.log('==은/는=='); | |
items = []; | |
cases.forEach(function(t) { | |
items.push(t + hangulJosa.eunNeun(t)); | |
}); | |
console.log(items.join(' / ')); | |
console.log('==을/를=='); | |
items = []; | |
cases.forEach(function(t) { | |
items.push(t + hangulJosa.eulReul(t)); | |
}); | |
console.log(items.join(' / ')); | |
console.log('==과/와=='); | |
items = []; | |
cases.forEach(function(t) { | |
items.push(t + hangulJosa.gwaWa(t)); | |
}); | |
console.log(items.join(' / ')); | |
console.log('==이여/여=='); | |
items = []; | |
cases.forEach(function(t) { | |
items.push(t + hangulJosa.iyeoYeo(t)); | |
}); | |
console.log(items.join(' / ')); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment