/**
*
* 초성 19개: ㄱ ㄲ ㄴ ㄷ ㄸ ㄹ ㅁ ㅂ ㅃ ㅅ ㅆ ㅇ ㅈ ㅉ ㅊ ㅋ ㅌ ㅍ ㅎ
* 중성 21개: ㅏ ㅐ ㅑ ㅒ ㅓ ㅔ ㅕ ㅖ ㅗ ㅘ ㅙ ㅚ ㅛ ㅜ ㅝ ㅞ ㅟ ㅠ ㅡ ㅢ ㅣ
* 종성 28개: Fill ㄱ ㄲ ㄳ ㄴ ㄵ ㄶ ㄷ ㄹ ㄺ ㄻ ㄼ ㄽ ㄾ ㄿ ㅀ ㅁ ㅂ ㅄ ㅅ ㅆ ㅇ ㅈ ㅊ ㅋ ㅌ ㅍ ㅎ
*
* 유니코드 2.0 한글의 범위
* AC00(가) ~ D7A3(힣)
*
* 유니코드 2.0 한글을 영문자로 표시하기
* ㄱ <--> ㅋ
* ㄷ <--> ㅌ
* ㅂ <--> ㅃ
* ㅈ <--> ㅉ
*/
public class HangulToEng {
final static String[] cho = { "G", "Gg", "N", "D", "Dd", "R", "M", "B", "Bb", "S", "Ss", "O", "J", "Jj", "Ch", "K", "T", "P", "H" };
final static String[] jung = { "a", "ae", "ya", "yae", "eo", "e", "yeo", "ye", "o", "wa", "wae", "woi", "yo", "u", "weo", "we", "wi", "yu", "eu", "eui", "i" };
final static String[] chong = { "", "g", "gg", "gs", "n", "nz", "nh", "d", "l", "lg", "lm", "lb", "ls", "lt", "lp", "lh", "m", "b", "bs", "s", "ss", "ng", "z", "ch", "k", "t", "p", "h" };
///////////////////////////////////////////////////////////////
// Convert a hangul string to a pronounced english string.
//
public static String toEng(String s) {
if (s == null)
return null;
String t = "";
String tmp = "";
int n, n1, n2, n3;
char c;
for (int i = 0; i < s.length(); i++) {
c = s.charAt(i);
n = (int)(c & 0xFFFF);
if (n >= 0xAC00 && n <= 0xD7A3) {
n = n - 0xAC00;
n1 = n / (21 * 28);
n = n % (21 * 28);
n2 = n / 28;
n3 = n % 28;
tmp = cho[n1] + jung[n2] + chong[n3];
if (tmp.length() > 1 && tmp.charAt(0) == 'O')
tmp = ("" + tmp.charAt(1)).toUpperCase() + tmp.substring(2);
t += tmp;
}
else
t += c;
}
return t;
}
///////////////////////////////////////////////////////////////
// Convert a pronounced english string to a hangul string
// This is the inverse of toEng(String).
//
public static String toKor(String src) {
if (src == null)
return null;
final int ASCII_STATE = 0;
final int CHO_STATE = 1;
final int JUNG_STATE = 2;
final int CHONG_STATE = 3;
final int FINISH_STATE = 100;
int state = ASCII_STATE;
int index = 0;
int f1 = 0, f2 = 0, f3 = 0;
String t = "";
String tmp = "";
String eng = "";
char a, c;
for (int i = 0; i < src.length(); i++) {
c = src.charAt(i);
eng += c;
switch (state) {
case ASCII_STATE:
f1 = 0;
f2 = 0;
f3 = 0;
if (c == 'Y' || c == 'W' || c == 'A' || c == 'E' || c == 'O' || c == 'U' || c == 'I') {
f1 = 11; // the index of 'O' is 11 in Cho-Sung array.
eng = "" + (char) (c | 0x20); // same to (char) (c + 0x20)
for (int n = 0; n < jung.length; n++) {
if (jung[n].equals(eng)) {
index = n;
}
}
f2 = index;
state = JUNG_STATE;
}
else if (c == 'G' || c == 'N' || c == 'D' || c == 'R' || c == 'M' || c == 'B' || c == 'S' || c == 'O' || c == 'W' || c == 'J' || c == 'C' || c == 'K' || c == 'T' || c == 'P' || c == 'H') {
if (c == 'C') {
for (int n = 0; n < cho.length; n++) {
if (cho[n].equals("Ch")) {
index = n;
}
}
}
else {
for (int n = 0; n < cho.length; n++) {
if (cho[n].equals(eng)) {
index = n;
}
}
}
f1 = index;
state = CHO_STATE;
}
else {
eng = "";
t += c;
}
break;
case CHO_STATE:
f2 = 0;
f3 = 0;
a = eng.charAt(1);
if (a == 'a' || a == 'y' || a == 'e' || a == 'w' || a == 'o' || a == 'u' || a == 'i') {
eng = eng.substring(1);
for (int n = 0; n < jung.length; n++) {
if (jung[n].equals(eng)) {
index = n;
}
}
f2 = index;
state = JUNG_STATE;
}
else {
for (int n = 0; n < cho.length; n++) {
if (cho[n].equals(eng)) {
index = n;
}
}
f1 = index;
eng = "";
state = JUNG_STATE;
}
break;
case JUNG_STATE:
f3 = 0;
if (c == 'a' || c == 'e' || c == 'o' || c == 'o' || c == 'u' || c == 'i') {
for (int n = 0; n < jung.length; n++) {
if (jung[n].equals(eng)) {
index = n;
}
}
f2 = index;
}
else if (c == 'Y' || c == 'W' || c == 'A' || c == 'E' || c == 'O' || c == 'U' || c == 'I') {
t += (char) (0xAC00 + f1 * 21 * 28 + f2 * 28);
b1f = 11; // the index of 'O' is 11 in Cho-Sung array.
eng = "" + (char) (c | 0x20); // same to (char) (c + 0x20)
for (int n = 0; n < jung.length; n++) {
if (jung[n].equals(eng)) {
index = n;
}
}
f2 = index;
f3 = 0;
}
else if (c == 'g' || c == 'n' || c == 'd' || c == 'l' || c == 'm' || c == 'b' || c == 's' || c == 'z' || c == 'c' || c == 'k' || c == 't' || c == 'p' || c == 't' || c == 'h') {
eng = "" + c;
for (int n = 1; n < chong.length; n++) {
if (chong[n].equals(eng)) {
index = n;
}
}
f3 = index;
state = CHONG_STATE;
}
else if (c == 'G' || c == 'N' || c == 'D' || c == 'R' || c == 'M' || c == 'B' || c == 'S' || c == 'O' || c == 'J' || c == 'C' || c == 'K' || c == 'T' || c == 'P' || c == 'H') {
t += (char) (0xAC00 + f1 * 21 * 28 + f2 * 28);
eng = "" + c;
for (int n = 0; n < cho.length; n++) {
if (cho[n].equals(eng)) {
index = n;
}
}
f1 = index;
f2 = 0;
f3 = 0;
state = CHO_STATE;
}
else if (c == 'Y' || c == 'W' || c == 'A' || c == 'E' || c == 'O' || c == 'U' || c == 'I') {
t += (char) (0xAC00 + f1 * 21 * 28 + f2 * 28);
f1 = 11; // the index of 'O' is 11 in Cho-Sung array.
eng = "" + (char) (c & 0xDF); // same to (char) (c - 0x20)
for (int n = 0; n < jung.length; n++) {
if (jung[n].equals(eng)) {
index = n;
}
}
f2 = index;
f3 = 0;
state = JUNG_STATE;
}
else {
t += (char) (0xAC00 + f1 * 21 * 28 + f2 * 28);
t += c;
eng = "";
f1 = 0;
f2 = 0;
f3 = 0;
state = ASCII_STATE;
}
break;
case CHONG_STATE:
if (c == 'g' || c == 'n' || c == 'd' || c == 'l' || c == 'm' || c == 'b' || c == 's'
|| c == 'z' || c == 'c' || c == 'k' || c == 't' || c == 'p' || c == 't' || c == 'h') {
for (int n = 1; n < chong.length; n++) {
if (chong[n].equals(eng)) {
index = n;
}
}
f3 = index;
}
else if (c == 'G' || c == 'N' || c == 'D' || c == 'R' || c == 'M' || c == 'B' || c == 'S' || c == 'O'
|| c == 'J' || c == 'C' || c == 'K' || c == 'T' || c == 'P' || c == 'H') {
t += (char) (0xAC00 + f1 * 21 * 28 + f2 * 28 + f3);
eng = "" + c;
if (c == 'C') {
for (int n = 0; n < cho.length; n++) {
if (cho[n].equals("Ch")) {
index = n;
}
}
}
else {
for (int n = 0; n < cho.length; n++) {
if (cho[n].equals(eng)) {
index = n;
}
}
}
f1 = index;
f2 = 0;
f3 = 0;
state = CHO_STATE;
if (c == 'O') {
c = 'o';
eng = "" + c;
f2 = 8;
f3 = 0;
state = JUNG_STATE;
}
}
else if (c == 'Y' || c == 'W' || c == 'A' || c == 'E' || c == 'O' || c == 'U' || c == 'I') {
t += (char) (0xAC00 + f1 * 21 * 28 + f2 * 28 + f3);
f1 = 11; // the index of 'O' is 11 in Cho-Sung array.
eng = "" + (char) (c | 0x20); // same to (char) (c + 0x20)
for (int n = 0; n < jung.length; n++) {
if (jung[n].equals(eng)) {
index = n;
}
}
f2 = index;
f3 = 0;
state = JUNG_STATE;
}
else {
t += (char) (0xAC00 + f1 * 21 * 28 + f2 * 28 + f3);
t += c;
eng = "";
f1 = 0;
f2 = 0;
f3 = 0;
state = ASCII_STATE;
}
break;
}
}
if (state > ASCII_STATE) {
t += (char) (0xAC00 + f1 * 21 * 28 + f2 * 28 + f3);
}
return t;
}
public static void main(String[] args) {
String data = "대-한민국! 코리아팀 파이팅!";
String data2 = "2002년 6월 월드컵 4강";
System.out.println("toEng(String): " + data + " --> " + toEng(data));
System.out.println("toKor(String): " + toEng(data) + " --> " + toKor(toEng(data)));
System.out.println("toEng(String): " + data2 + " --> " + toEng(data2));
System.out.println("toKor(String): " + toEng(data2) + " --> " + toKor(toEng(data2)));
}
}
/*
실행 결과:
toEng(String): 대-한민국! 코리아팀 파이팅! --> Dae-HanMinGug! KoRiATim PaITing!
toKor(String): Dae-HanMinGug! KoRiATim PaITing! --> 대-한민국! 코리아팀 파이팅!
toEng(String): 2002년 6월 월드컵 4강 --> 2002Nyeon 6Weol WeolDeuKeob 4Gang
toKor(String): 2002Nyeon 6Weol WeolDeuKeob 4Gang --> 2002년 6월 월드컵 4강
*/
/**
*
* 초성 19개: ㄱ ㄲ ㄴ ㄷ ㄸ ㄹ ㅁ ㅂ ㅃ ㅅ ㅆ ㅇ ㅈ ㅉ ㅊ ㅋ ㅌ ㅍ ㅎ
* 중성 21개: ㅏ ㅐ ㅑ ㅒ ㅓ ㅔ ㅕ ㅖ ㅗ ㅘ ㅙ ㅚ ㅛ ㅜ ㅝ ㅞ ㅟ ㅠ ㅡ ㅢ ㅣ
* 종성 28개: Fill ㄱ ㄲ ㄳ ㄴ ㄵ ㄶ ㄷ ㄹ ㄺ ㄻ ㄼ ㄽ ㄾ ㄿ ㅀ ㅁ ㅂ ㅄ ㅅ ㅆ ㅇ ㅈ ㅊ ㅋ ㅌ ㅍ ㅎ
*
* 유니코드 2.0 한글의 범위
* AC00(가) ~ D7A3(힣)
*
* 유니코드 2.0 한글의 초성 바꾸기(swapping)
* ㄱ <--> ㅋ
* ㄷ <--> ㅌ
* ㅂ <--> ㅃ
* ㅈ <--> ㅉ
*/
public class HangulChosungSwap {
// { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
final static int[] toKK = { 15, 1, 2, 16, 4, 5, 6, 17, 8, 9, 10, 11, 14, 13, 12, 0, 3, 7, 18 };
public static String swapCho(String s) {
if (s == null)
return null;
String t = "";
int n, n1, n2, n3;
char c;
for (int i = 0; i < s.length(); i++) {
c = s.charAt(i);
n = (int)(c & 0xFFFF);
if (n >= 0xAC00 && n <= 0xD7A3) {
n = n - 0xAC00;
n1 = n / (21 * 28);
n = n % (21 * 28);
n2 = n / 28;
n3 = n % 28;
n = 0xAC00 + (toKK[n1] * 21 * 28) + (n2 * 28) + n3;
t += (char)n;
}
else
t += c;
}
return t;
}
public static void main(String[] args) {
String data = "한글, 봉이 김선달";
System.out.println(data + " --> " + swapCho(data));
String data2 = "부엌칼, 찰떡 궁합";
System.out.println(data2 + " --> " + swapCho(data2));
String data3 = "잔잔한 바다 물결~";
System.out.println(data3 + " --> " + swapCho(data3));
String data4 = "평화로운 고향 마을...";
System.out.println(data4 + " --> " + swapCho(data4));
}
}
No comments:
Post a Comment