Created
September 9, 2017 18:14
-
-
Save 20chan/6137d0d9c47db9836afef2dd43b492b4 to your computer and use it in GitHub Desktop.
한글 처리 C# 클래스
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
public struct 글자 | |
{ | |
/// <summary> | |
/// 한글여부(H, NH) | |
/// </summary> | |
public string 한글인가; | |
/// <summary> | |
/// 분석 한글 | |
/// </summary> | |
public char 원래글자; | |
/// <summary> | |
/// 분리 된 한글(강 -> ㄱ,ㅏ,ㅇ) | |
/// </summary> | |
public char[] 음소; | |
} |
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
public sealed class 한국어 | |
{ | |
public static readonly string 초성 = "ㄱㄲㄴㄷㄸㄹㅁㅂㅃㅅㅆㅇㅈㅉㅊㅋㅌㅍㅎ"; | |
public static readonly string 중성 = "ㅏㅐㅑㅒㅓㅔㅕㅖㅗㅘㅙㅚㅛㅜㅝㅞㅟㅠㅡㅢㅣ"; | |
public static readonly string 종성 = " ㄱㄲㄳㄴㄵㄶㄷㄹㄺㄻㄼㄽㄾㄿㅀㅁㅂㅄㅅㅆㅇㅈㅊㅋㅌㅍㅎ"; | |
private static readonly ushort 유니코드첫한국어 = 0xAC00; | |
private static readonly ushort 유니코드마지막한국어 = 0xD79F; | |
public 한국어() { } | |
public static char 글자만들기(string 초, string 중, string 종) | |
{ | |
int 초성위치, 중성위치, 종성위치; | |
int 유니코드; | |
초성위치 = 초성.IndexOf(초); // 초성 위치 | |
중성위치 = 중성.IndexOf(중); // 중성 위치 | |
종성위치 = 종성.IndexOf(종); // 종성 위치 | |
// 앞서 만들어 낸 계산식 | |
유니코드 = 유니코드첫한국어 + (초성위치 * 21 + 중성위치) * 28 + 종성위치; | |
// 코드값을 문자로 변환 | |
char 임시 = Convert.ToChar(유니코드); | |
return 임시; | |
} | |
public static 글자 글자나누기(char 한글자) | |
{ | |
int 초, 중, 종; // 초성,중성,종성의 인덱스 | |
ushort 임시유니코드 = 0x0000; // 임시로 코드값을 담을 변수 | |
글자 결과 = new 글자(); | |
//Char을 16비트 부호없는 정수형 형태로 변환 - Unicode | |
임시유니코드 = Convert.ToUInt16(한글자); | |
// 캐릭터가 한글이 아닐 경우 처리 | |
if ((임시유니코드 < 유니코드첫한국어) || (임시유니코드 > 유니코드마지막한국어)) | |
{ | |
결과.한글인가 = "NH"; | |
결과.원래글자 = 한글자; | |
결과.음소 = null; | |
} | |
else | |
{ | |
// nUniCode에 한글코드에 대한 유니코드 위치를 담고 이를 이용해 인덱스 계산. | |
int 유니코드 = 임시유니코드 - 유니코드첫한국어; | |
초 = 유니코드 / (21 * 28); | |
유니코드 = 유니코드 % (21 * 28); | |
중 = 유니코드 / 28; | |
유니코드 = 유니코드 % 28; | |
종 = 유니코드; | |
결과.한글인가 = "H"; | |
결과.원래글자 = 한글자; | |
결과.음소 = new char[] { 초성[초], 중성[중], 종성[종] }; | |
} | |
return 결과; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment