질/문/과/답/변
● Delphi에서 한글관련 작업의 사용방법
시작:=ConvEngToHan(tlwkr);
const
ChoSungTbl : String = 'ㄱㄲㄴㄷㄸㄹㅁㅂㅃㅅㅆㅇㅈㅉㅊㅋㅌㅍㅎ';
JungSungTbl : String = 'ㅏㅐㅑㅒㅓㅔㅕㅖㅗㅘㅙㅚㅛㅜㅝㅞㅟㅠㅡㅢㅣ';
JongSungTbl : String = ' ㄱㄲㄳㄴㄵㄶㄷㄹㄺㄻㄼㄽㄾㄿㅀㅁㅂㅄㅅㅆㅇㅈㅊㅋㅌㅍㅎ';
EngChosung : String = 'r R s e E f a q Q t T d w W c z x v g ';
EngJungsung : String = 'k o i O j p u P h hkhohly n njnpnlb m mll ';
EngJongsung : String = ' r R rts swsge f frfafqftfxfvfga q qtt T d w c z x v g ';
UniCodeHangeulBase = $AC00;
UniCodeHangeulLast = $D79F;
//한글관련
function ConvEngToHan(src:String):String;
function CutFirstHangul(src:String):String;
function DecodingEngToHan(src:String):String;
function DivChoJungJong(src:String):String;
function CharKeyCheck(c : char):boolean;
function MoumCheck(c : char):boolean;
function HanDiv(const Han: PChar; Han3: PChar): Boolean;
function HanDivPas(const Src: String;Check:integer): String;
Function GetHanType(const Src: string): Boolean;
////////////////////////////////////////////////////////////////////////////////
function MoumCheck(c : char):boolean;
Begin
Result := c in ['k','o','i','O','j','p','u','P','h','y','n','b','m','l'];
end;
function MarkCheck(c : char):boolean;
Begin
Result := c in ['@','#','$','%','^','&','*','(',')','_','-',' ','+','=','{','}','[',']','|','\','<','>','.',',','/','?',':',';','"',,'''];
end;
function CharKeyCheck(c : char):boolean;
Begin
Result := c in ['A'..'Z','a'..'z'];
end;
function DivChoJungJong(src:String):String;
var i, j, l : integer;
Tmp, Res : String;
Begin
l := Length(src);
If (l < 2) or (not CharKeyCheck(src[1])) Then Begin
Result := Src[1];
Exit;
end;
Tmp := src + ' ';
{초성}
i := Pos(Copy(Tmp,1,1),EngChosung);
If i > 0 Then Begin
res := Copy(tmp,1,1) + ' ';
j := 2;
End Else Begin
Result := Tmp[1];
Exit;
End;
{중성}
If (not CharKeyCheck(tmp[j])) or (not MoumCheck(tmp[j]))Then Begin
Result := Res;
Exit;
End Else Begin
i := pos(Copy(tmp,j,2),EngJungsung);
If i > 0 Then Begin
res := res + Copy(tmp,j,2);
j := j + 2;
End Else Begin
res := res + tmp[j] + ' ';
j := j + 1;
End;
End;
{종성}
If (not (tmp[j] in [' ','A'..'Z','a'..'z'])) or (MoumCheck(tmp[j]))Then Begin
Result := res + ' ';
exit;
End Else Begin
i := pos(Copy(tmp,j,2),EngJongsung);
If i > 0 Then Begin res := res + Copy(tmp,j,2);
End Else Begin res := res + tmp[j] + ' ' ;
End;
End;
Result := res;
end;
function DecodingEngToHan(src:String):String;
var UniCode : Integer;
ChoSung , JungSung , JongSung : Integer;
ChoSungPos, JungSungPos, JongSungPos: Integer;
Tmp : String;
Tmp2 : Array[0..2] of Char;
Begin
Result := '';
Tmp := DivChoJungJong(Src);
If Length(tmp) = 1 Then Begin
ChoSungPos := Pos(Tmp+' ', EngChoSung );
JungSungPos := Pos(Tmp+' ', EngJungSung);
JongSungPos := Pos(Tmp+' ', EngJongSung);
If ChoSungPos > 0 Then Result := Copy(ChoSungTbl ,ChoSungPos ,2)
Else If JungSungPos > 0 Then Result := Copy(JungSungTbl,JungSungPos,2)
Else If JongSungPos > 0 Then Result := Copy(JongSungTbl,JongSungPos,2)
Else Result := Tmp;
Exit;
End Else If Length(Tmp) <> 6 Then Begin
Result := Tmp;
Exit;
End;
ChoSungPos := Pos(Copy(tmp, 1, 2), EngChoSung );
JungSungPos := Pos(Copy(tmp, 3, 2), EngJungSung);
JongSungPos := Pos(Copy(tmp, 5, 2), EngJongSung);
//
If (ChoSungPos And JungSungPos And JongSungPos) = 0 Then Exit;
ChoSung := (ChoSungPos - 1) Div 2;
JungSung := (JungSungPos - 1) Div 2;
JongSung := (JongSungPos - 1) Div 2;
UniCode := UniCodeHangeulBase +(ChoSung * 21 + JungSung) * 28 + JongSung;
WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, @UniCode, 1, @tmp2, 2, nil, nil);
Result := tmp2[0]+tmp2[1];
end;
function CutFirstHangul(src:String):String;
var l : integer;
Begin
l := Length(src);
If l < 2 Then Result := src
Else If (Length(DivChoJungJong(src)) <> 6 ) Then
Result := src[1]
Else If (l < 3) Then Result := src
Else If (Length(DivChoJungJong(Copy(src,1,2))) = 6 ) And (not CharKeyCheck(src[3])) Then
Result := Copy(src,1,2)
Else If (l < 4) Then Result := src
Else If (Length(DivChoJungJong(Copy(src,1,2))) = 6 ) And ((not CharKeyCheck(src[3])) or (Length(DivChoJungJong(Copy(src,3,2))) = 6)) Then
Result := Copy(src,1,2)
Else If (l < 5) Then Result := src
Else If (Length(DivChoJungJong(Copy(src,1,3))) = 6 ) And ((not CharKeyCheck(src[4])) or (Length(DivChoJungJong(Copy(src,4,2))) = 6)) Then
Result := Copy(src,1,3)
Else If (l < 6) Then Result := src
Else If (Length(DivChoJungJong(Copy(src,1,4))) = 6 ) And ((not CharKeyCheck(src[5])) Or (Length(DivChoJungJong(Copy(src,5,2))) = 6)) Then
Result := Copy(src,1,4)
Else Result := Copy(src,1,5);
end;
function ConvEngToHan(src:String):String;
var s,r,t : String;
Begin
s := Src;
r := '';
If Trim(Src)<>'' Then Begin
Repeat
t := CutFirstHangul(s) ;
r := r + DecodingEngToHan(t);
Delete(s,1,Length(t));
Until s = '';
End;
Result := r;
end;
function HanDiv(const Han: PChar; Han3: PChar): Boolean;
var UniCode: Word;
ChoSung, JungSung, JongSung: Integer;
begin
Result := False;
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, Han, 2, @UniCode, 1);
if (UniCode < UniCodeHangeulBase) or
(UniCode > UniCodeHangeulLast) then Exit;
UniCode := UniCode - UniCodeHangeulBase;
ChoSung := UniCode div (21 * 28);
UniCode := UniCode mod (21 * 28);
JungSung := UniCode div 28;
UniCode := UniCode mod 28;
JongSung := UniCode;
StrLCopy(Han3, Pchar(ChoSungTbl ) + ChoSung * 2, 2);
StrLCopy(Han3 + 2, Pchar(JungSungTbl) + JungSung * 2, 2);
StrLCopy(Han3 + 4, Pchar(JongSungTbl) + JongSung * 2, 2);
Result := True;
end;
function HanDivPas(const Src: String;Check:integer): String;
var Buff: array[0..6] of Char;
B,C:String ;
i :Integer;
begin
B:='';
C:='';
For i:=1 To Length(Src) Do Begin
B:=Copy(Src,((i-1)*2)+1,2);
if Length(Trim(B))=2 then begin
if HanDiv(PChar(B), Buff) then begin
Buff[6]:=#0;
If Check=1 Then C :=C + Copy(String(Buff),1,2)
Else If Check=2 Then C :=C + Copy(String(Buff),3,2)
Else If Check=3 Then C :=C + Copy(String(Buff),5,2);
end;
end;
End;
HanDivPas:=C;
end;
Function GetHanType(const Src: string): Boolean;
var Len, Hi, Lo: Integer;
begin
Result := False;
if Length(Src) < 2 then Exit;
Len := Length(Src);
Hi := Ord(Src[Len - 1]);
Lo := Ord(Src[Len]);
//if ($A1 > Lo) or ($FE < Lo) then Exit;
if ($B0 <= Hi) and ($C8 >= Hi) then Result := True //한글
Else if ($CA <= Hi) and ($FD >= Hi) then Result := False //한자
Else Result := False; //기타
end;
김채연 wrote:
> 나비스 wrote:
> > 한글은
> > 자음 + 모음
> > 자음 + 모음 + 자음
> > 자음 + 모음 + 자음 + 자음 + 모음
> > ...
> >
> > 등의 단어를 이루는 패턴이 있습니다.
> > 영문자의 키보드 자판에서의 자모로 대입해서
> > 패턴이 한글에서 나올만한 패턴이 된다면
> > 자모분해된 한글로 판단하고 영문자를 각 키보드 자판의 한글자모로 변환한후
> > 다시 분해된 자모를 합치면 한글단어가 될듯 합니다.
> >
> > 이런 방식으로 구현을 해 보시면 어떨지..
> >
> > 김채연 wrote:
> > >
> > >
> > > 간단할것 같은데..여러가지 생각도 해보고
> > > 이곳에서 검색을 해도 답이 없는 듯합니다.
> > > 오타로 잘못친 영문자를 한글로 바꾸는 방법좀 알려주시면 감사하겠습니다.
> >
>
--------------------------------------------------------------------------------