// Pascal -> PChar // 直接使用 PChar 转化有时会转化出错 function StrPch(const stPas: string): PChar; begin Result := ''; GetMem(Result, Length(stPas) + 1); StrPCopy(Result, stPas); end;function MakeSpellCode(stText: string; iMode, iCount: Integer): string; var i, Index: integer; APy, ls_code: string; fFlag1, fFlag2, fFlag3: Boolean; begin fFlag1 := (iMode and $0001) = 1; fFlag2 := (iMode and $0002) = 2; fFlag3 := (iMode and $0004) = 4; Result := ''; if iMode < 0 then Exit; i := 1; while (i <= Length(stText)) do begin if (Ord(stText[i]) >= 129) and (Ord(stText[i + 1]) >= 64) then begin // 是否为 GBK 字符 case Ord(stText[i]) of 163: // 全角 ASCII begin APy := Chr(Ord(stText[i + 1]) - 128); // 控制不能输出非数字, 字母的字符 if not fFlag3 and not (APy[1] in ['a'..'z', 'A'..'Z', '0'..'9']) then APy := ''; end; 162: // 罗马数字 if Ord(stText[i + 1]) > 160 then APy := CharIndex[Ord(stText[i + 1]) - 160] else // 在罗马数字区, 不能翻译的字符非罗马数字 if fFlag2 then APy := '?' else APy := ''; 166: // 希腊字母 if Ord(stText[i + 1]) in [$A1..$B8] then APy := UpperCase(CharIndex2[Ord(stText[i + 1]) - $A0]) else if Ord(stText[i + 1]) in [$C1..$D8] then APy := UpperCase(CharIndex2[Ord(stText[i + 1]) - $C0]); else // 一般汉字 if gi_loaded = 1 then begin // 使用外挂 // 获得拼音索引 ls_code := IMCodes[Ord(stText[i]) - 128, Ord(stText[i + 1]) - 63].PYCode; if not fFlag1 then // iFlag1 = False, 是单拼音 APy := Copy(Uppercase(ls_code), 1, 1) else APy := Copy(Uppercase(ls_code), 1, 6); end else begin // 使用内置 // 获得拼音索引 Index := PyCodeIndex[Ord(stText[i]) - 128, Ord(stText[i + 1]) - 63]; if Index = 0 then // 无此汉字, 不能翻译的字符, GBK 保留 if fFlag2 then APy := '?' else APy := '' else if not fFlag1 then // iFlag1 = False, 是单拼音 APy := Copy(Uppercase(PyMusicCode[Index]), 1, 1) else APy := Copy(Uppercase(PyMusicCode[Index]), 1, 6); end; end; Result := Result + APy; Inc(i, 2); end else begin // 在 GBK 字符集外, 即半角字符 if fFlag3 or (stText[i] in ['a'..'z', 'A'..'Z', '0'..'9']) then Result := Result + UpperCase(stText[i]); Inc(i); end; end; Result := Copy(Result, 1, iCount); end;
function GetSpellCode(szText: PChar; iMode, iCount: Integer): PChar; // Call MakeSpellCode begin Result := StrPch(MakeSpellCode(String(szText), iMode, iCount)); end;function LoadIMCode(as_dict_file: PChar): integer; // 切换字典 var SpacePos, L, I, J, index: Integer; PYCode: string[6]; ls_dict_file, LineStr: string; SrcFp: TextFile; { 汉字输入法代码字典文件 } begin gi_loaded := 1; ls_dict_file := string(as_dict_file); result := 0; for J := 1 to 126 do { 初始化处理 } for I := 1 to 191 do ImCodes[J, I].PYCode := ''; try try AssignFile(SrcFp, ls_dict_file); Reset(SrcFp); while not EOF(SrcFp) do begin ReadLn(SrcFp, LineStr); if (Ord(LineStr[1]) >= 129) and (Ord(LineStr[2]) >= 64) { GBK 内码区间: 首字节: $81 - $FE 尾字节: $40 - $7E, $80 - $FE } and (LineStr[3] in ['0'..'9', 'A'..'Z', 'a'..'z']) then begin SpacePos := Pos(' ', LineStr); if SpacePos = 0 then // 单音字 PYCode := Copy(LineStr, 3, 6) // 取拼音代码 else begin // 多音字 L := Length(LineStr); PYCode := Copy(LineStr, SpacePos + 1, L - SpacePos); // 取常用拼音代码 end; ImCodes[Ord(LineStr[1]) - 128, Ord(LineStr[2]) - 63].PYCode := PYCode; end; end; result := 1; finally CloseFile(SrcFp); end; except end; end;function UseInplace: integer; begin gi_loaded := 0; result := 0; end;function UseOutplace: integer; begin gi_loaded := 1; result := 1; end;function IsOutplace: integer; begin result := gi_loaded; end;end.
调用: procedure TForm1.Button1Click(Sender: TObject); begin LabeledEdit1.EditLabel.Caption := GetSpellCode(Pchar(LabeledEdit1.Text),1,50); end;太长了,拷不全,给我email吧,or自己上网找这个转换的
interfacefunction MakeSpellCode(stText: string; iMode, iCount: Integer): string;
{ iMode 二进制功能位说明
X X X X X X X X X X X X X X X X
3 2 1
1: 0 - 只取各个汉字声母的第一个字母; 1 - 全取
2: 0 - 遇到不能翻译的字符不翻译; 1 - 翻译成 '?' (本选项目针对全角字符)
3: 0 - 生成的串不包括非数字, 字母的其他字符; 1 - 包括
(控制全角的要输出非数字, 字母字符的; 半角的非数字, 字母字符)
请在调用之前调用 LoadIMCode 函数
}function GetSpellCode(szText: PChar; iMode, iCount: Integer): PChar; stdcall;function LoadIMCode(as_dict_file: PChar): integer; stdcall;
// 切换输入码数据源
// 调用示例: LoadIMCode('winpy.txt');
// 此函数只需在切换字典数据时使用function UseInplace: integer; stdcall;
// 使用内置拼音代码字典function UseOutplace: integer; stdcall;
// 使用外置拼音代码字典function IsOutplace: integer; stdcall;
// 是否外置implementationuses
SysUtils;type
{ 拼音代码表 }
TPYCode = record
PYCode: string[6];
end;
TFPYCodes = array [1..126, 1..191] of TPYCode;var
ImCodes: TFPYCodes;const
PYMUSICCOUNT = 405;
PyMusicCode: array [1..PYMUSICCOUNT] of string[6] = { 汉字基本发音表 } (
'a', 'ai', 'an', 'ang', 'ao', 'ba', 'bai', 'ban', 'bang', 'bao',
'bei', 'ben', 'beng', 'bi', 'bian', 'biao', 'bie', 'bin', 'bing', 'bo',
'bu', 'ca', 'cai', 'can', 'cang', 'cao', 'ce', 'ceng', 'cha', 'chai',
'chan', 'chang', 'chao', 'che', 'chen', 'cheng', 'chi', 'chong', 'chou', 'chu',
'chuai', 'chuan', 'chuang', 'chui', 'chun', 'chuo', 'ci', 'cong', 'cou', 'cu',
'cuan', 'cui', 'cun', 'cuo', 'da', 'dai', 'dan', 'dang', 'dao', 'de',
'deng', 'di', 'dian', 'diao', 'die', 'ding', 'diu', 'dong', 'dou', 'du',
'duan', 'dui', 'dun', 'duo', 'e', 'en', 'er', 'fa', 'fan', 'fang',
'fei', 'fen', 'feng', 'fu', 'fou', 'ga', 'gai', 'gan', 'gang', 'gao',
'ge', 'ji', 'gen', 'geng', 'gong', 'gou', 'gu', 'gua', 'guai', 'guan',
'guang', 'gui', 'gun', 'guo', 'ha', 'hai', 'han', 'hang', 'hao', 'he',
'hei', 'hen', 'heng', 'hong', 'hou', 'hu', 'hua', 'huai', 'huan', 'huang',
'hui', 'hun', 'huo', 'jia', 'jian', 'jiang', 'qiao', 'jiao', 'jie', 'jin',
'jing', 'jiong', 'jiu', 'ju', 'juan', 'jue', 'jun', 'ka', 'kai', 'kan',
'kang', 'kao', 'ke', 'ken', 'keng', 'kong', 'kou', 'ku', 'kua', 'kuai',
'kuan', 'kuang', 'kui', 'kun', 'kuo', 'la', 'lai', 'lan', 'lang', 'lao',
'le', 'lei', 'leng', 'li', 'lia', 'lian', 'liang', 'liao', 'lie', 'lin',
'ling', 'liu', 'long', 'lou', 'lu', 'luan', 'lue', 'lun', 'luo', 'ma',
'mai', 'man', 'mang', 'mao', 'me', 'mei', 'men', 'meng', 'mi', 'mian',
'miao', 'mie', 'min', 'ming', 'miu', 'mo', 'mou', 'mu', 'na', 'nai',
'nan', 'nang', 'nao', 'ne', 'nei', 'nen', 'neng', 'ni', 'nian', 'niang',
'niao', 'nie', 'nin', 'ning', 'niu', 'nong', 'nu', 'nuan', 'nue', 'yao',
'nuo', 'o', 'ou', 'pa', 'pai', 'pan', 'pang', 'pao', 'pei', 'pen',
'peng', 'pi', 'pian', 'piao', 'pie', 'pin', 'ping', 'po', 'pou', 'pu',
'qi', 'qia', 'qian', 'qiang', 'qie', 'qin', 'qing', 'qiong', 'qiu', 'qu',
'quan', 'que', 'qun', 'ran', 'rang', 'rao', 're', 'ren', 'reng', 'ri',
'rong', 'rou', 'ru', 'ruan', 'rui', 'run', 'ruo', 'sa', 'sai', 'san',
'sang', 'sao', 'se', 'sen', 'seng', 'sha', 'shai', 'shan', 'shang', 'shao',
'she', 'shen', 'sheng', 'shi', 'shou', 'shu', 'shua', 'shuai', 'shuan', 'shuang',
'shui', 'shun', 'shuo', 'si', 'song', 'sou', 'su', 'suan', 'sui', 'sun',
'suo', 'ta', 'tai', 'tan', 'tang', 'tao', 'te', 'teng', 'ti', 'tian',
'tiao', 'tie', 'ting', 'tong', 'tou', 'tu', 'tuan', 'tui', 'tun', 'tuo',
'wa', 'wai', 'wan', 'wang', 'wei', 'wen', 'weng', 'wo', 'wu', 'xi',
'xia', 'xian', 'xiang', 'xiao', 'xie', 'xin', 'xing', 'xiong', 'xiu', 'xu',
'xuan', 'xue', 'xun', 'ya', 'yan', 'yang', 'ye', 'yi', 'yin', 'ying',
'yo', 'yong', 'you', 'yu', 'yuan', 'yue', 'yun', 'za', 'zai', 'zan',
'zang', 'zao', 'ze', 'zei', 'zen', 'zeng', 'zha', 'zhai', 'zhan', 'zhang',
'zhao', 'zhe', 'zhen', 'zheng', 'zhi', 'zhong', 'zhou', 'zhu', 'zhua', 'zhuai',
'zhuan', 'zhuang', 'zhui', 'zhun', 'zhuo', 'zi', 'zong', 'zou', 'zu', 'zuan',
'zui', 'zun', 'zuo', '', 'ei', 'm', 'n', 'dia', 'cen', 'nou',
'jv', 'qv', 'xv', 'lv', 'nv'
);
'1','2','3','4','5','6','7','8','9','10','','','','','','',
'1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20',
'1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20',
'1','2','3','4','5','6','7','8','9','10','','',
'1','2','3','4','5','6','7','8','9','10','','',
'1','2','3','4','5','6','7','8','9','10','11','12','',''
); CharIndex2: array [1..24] of string[2] = ( { 希腊字母 }
'a','b','g','d','e','z','e','th','i','k','l','m','n','x','o','p','r',
's','t','u','ph','kh','ps','o'
);var gi_loaded: integer = 0;
// Pascal -> PChar
// 直接使用 PChar 转化有时会转化出错
function StrPch(const stPas: string): PChar;
begin
Result := '';
GetMem(Result, Length(stPas) + 1);
StrPCopy(Result, stPas);
end;function MakeSpellCode(stText: string; iMode, iCount: Integer): string;
var
i, Index: integer;
APy, ls_code: string;
fFlag1, fFlag2, fFlag3: Boolean;
begin
fFlag1 := (iMode and $0001) = 1;
fFlag2 := (iMode and $0002) = 2;
fFlag3 := (iMode and $0004) = 4;
Result := '';
if iMode < 0 then Exit;
i := 1; while (i <= Length(stText)) do begin
if (Ord(stText[i]) >= 129) and (Ord(stText[i + 1]) >= 64) then begin
// 是否为 GBK 字符
case Ord(stText[i]) of
163: // 全角 ASCII
begin
APy := Chr(Ord(stText[i + 1]) - 128);
// 控制不能输出非数字, 字母的字符
if not fFlag3 and not (APy[1] in ['a'..'z', 'A'..'Z', '0'..'9']) then
APy := '';
end;
162: // 罗马数字
if Ord(stText[i + 1]) > 160 then
APy := CharIndex[Ord(stText[i + 1]) - 160] else
// 在罗马数字区, 不能翻译的字符非罗马数字
if fFlag2 then APy := '?' else APy := '';
166: // 希腊字母
if Ord(stText[i + 1]) in [$A1..$B8] then
APy := UpperCase(CharIndex2[Ord(stText[i + 1]) - $A0])
else if Ord(stText[i + 1]) in [$C1..$D8] then
APy := UpperCase(CharIndex2[Ord(stText[i + 1]) - $C0]);
else // 一般汉字
if gi_loaded = 1 then begin // 使用外挂
// 获得拼音索引
ls_code := IMCodes[Ord(stText[i]) - 128, Ord(stText[i + 1]) - 63].PYCode;
if not fFlag1 then // iFlag1 = False, 是单拼音
APy := Copy(Uppercase(ls_code), 1, 1) else
APy := Copy(Uppercase(ls_code), 1, 6);
end else begin // 使用内置
// 获得拼音索引
Index := PyCodeIndex[Ord(stText[i]) - 128, Ord(stText[i + 1]) - 63];
if Index = 0 then // 无此汉字, 不能翻译的字符, GBK 保留
if fFlag2 then APy := '?' else APy := ''
else if not fFlag1 then // iFlag1 = False, 是单拼音
APy := Copy(Uppercase(PyMusicCode[Index]), 1, 1) else
APy := Copy(Uppercase(PyMusicCode[Index]), 1, 6);
end;
end;
Result := Result + APy;
Inc(i, 2);
end else begin // 在 GBK 字符集外, 即半角字符
if fFlag3 or (stText[i] in ['a'..'z', 'A'..'Z', '0'..'9']) then
Result := Result + UpperCase(stText[i]);
Inc(i);
end;
end;
Result := Copy(Result, 1, iCount);
end;
// Call MakeSpellCode
begin
Result := StrPch(MakeSpellCode(String(szText), iMode, iCount));
end;function LoadIMCode(as_dict_file: PChar): integer;
// 切换字典
var
SpacePos, L, I, J, index: Integer;
PYCode: string[6];
ls_dict_file, LineStr: string;
SrcFp: TextFile; { 汉字输入法代码字典文件 }
begin
gi_loaded := 1;
ls_dict_file := string(as_dict_file);
result := 0;
for J := 1 to 126 do { 初始化处理 }
for I := 1 to 191 do ImCodes[J, I].PYCode := '';
try
try
AssignFile(SrcFp, ls_dict_file);
Reset(SrcFp);
while not EOF(SrcFp) do begin
ReadLn(SrcFp, LineStr);
if (Ord(LineStr[1]) >= 129) and (Ord(LineStr[2]) >= 64)
{ GBK 内码区间: 首字节: $81 - $FE
尾字节: $40 - $7E, $80 - $FE }
and (LineStr[3] in ['0'..'9', 'A'..'Z', 'a'..'z']) then begin
SpacePos := Pos(' ', LineStr);
if SpacePos = 0 then // 单音字
PYCode := Copy(LineStr, 3, 6) // 取拼音代码
else begin // 多音字
L := Length(LineStr);
PYCode := Copy(LineStr, SpacePos + 1, L - SpacePos); // 取常用拼音代码
end;
ImCodes[Ord(LineStr[1]) - 128, Ord(LineStr[2]) - 63].PYCode := PYCode;
end;
end;
result := 1;
finally
CloseFile(SrcFp);
end;
except
end;
end;function UseInplace: integer;
begin
gi_loaded := 0;
result := 0;
end;function UseOutplace: integer;
begin
gi_loaded := 1;
result := 1;
end;function IsOutplace: integer;
begin
result := gi_loaded;
end;end.
procedure TForm1.Button1Click(Sender: TObject);
begin
LabeledEdit1.EditLabel.Caption := GetSpellCode(Pchar(LabeledEdit1.Text),1,50);
end;太长了,拷不全,给我email吧,or自己上网找这个转换的