Lazarus中文社区

 找回密码
 立即注册(注册审核可向QQ群索取)

QQ登录

只需一步,快速开始

Lazarus IDE and 组件 下载地址版权申明
查看: 4746|回复: 2

汉字转拼音/五笔编码的例子 亲测通过

[复制链接]

该用户从未签到

发表于 2014-1-13 11:59:10 | 显示全部楼层 |阅读模式
以下代码均在winxp+lazarus 1.0.14下测试通过
感谢欧文
测试程序如下:


wb单元:
  1. //*************************************
  2. //汉字转五笔编码单元
  3. //作者:孙晓刚
  4. // 2013-12-24
  5. //=====================================

  6. unit uWuBi;

  7. {$mode objfpc}{$H+}

  8. interface

  9. uses
  10.   Classes, SysUtils ,windows;

  11. //参数AType =0 86版本 ==1 98版  AStr 要转换的字符串 (GBK编码) ,返回ASCII 五笔编码
  12. function GetWuBi(AType:integer;AStr:string):string;

  13. implementation


  14. {$R WBSearch.res}

  15. Function SwapByte(AiChinese : String) : Integer;
  16. Var
  17.   ch, cl : Integer;
  18. Begin
  19.   ch := Ord(AiChinese[1]);
  20.   cl := Ord(AiChinese[2]);
  21.   Result := (ch shl 8) + cl;
  22. end;

  23. function IsChinese(Code: Integer): Boolean;
  24. Const
  25.   SPOS=$8140;
  26.   EPOS=$FEA0;
  27. begin
  28.    Result := ((Code>=SPOS) And (Code<=EPOS))
  29. end;

  30. //0--1
  31. function InnerFind(FWBType:integer;Code: Integer): String;
  32. Const
  33.   BaseCode=$8140;
  34.   RESNAME:Array[0..1] of String=('WB86','WB98');
  35. var
  36.   RS:TResourceStream;
  37.   nPos:DWord;
  38.   Keys:array[0..4] of Char;
  39. begin

  40.   RS := TResourceStream.Create(Hinstance,RESNAME[FWBType],RT_RCDATA);
  41.   IF RS.Size>0 then
  42.   begin
  43.     nPos := (Code-BaseCode)*5;

  44.     RS.Seek(nPos,soBeginning);
  45.     RS.Read(Keys,5);
  46.     IF Keys[4]='' then
  47.     Result := Format('%s%s%s%s',[Keys[0],Keys[1],Keys[2],Keys[3]])
  48.     else Result := Format('%s%s%s%s  %s',[Keys[0],Keys[1],Keys[2],Keys[3],Keys[4]]);
  49.     RS.Free;
  50.   end;
  51. end;

  52. function GetWuBi(AType:integer;AStr:string):string;
  53. var
  54.   aChar:WideChar;
  55.   wStr:WideString;
  56.   Code,nLen:Integer;
  57. begin
  58.   If Length(AStr)>0 then
  59.   begin
  60.     wStr := AStr;
  61.     nLen := Length(wStr);
  62.     aChar := wStr[nLen];
  63.     Code := SwapByte(aChar);
  64.     If IsChinese(Code) then
  65.     result := UpperCase(InnerFind(AType,Code))
  66.     else result  := '';
  67.   end
  68.   else result := '';
  69. end;

  70. end.
复制代码
py单元 方式1:
  1. //=============================================================================
  2. //汉语拼音单元
  3. //应用场景: 助记符,人名拼音等
  4. //开发环境: delphi 7
  5. //          windows xp sp3
  6. //字符编码: GBK / CP936
  7. //功能:     将给出的中、英文字符串,查表得到其汉语拼音 以及 英文的 混合字符串
  8. //          中文可以得到其汉语拼音首字母串,或者全拼音字符串
  9. //限制:     没有实现音调,只限于 GBK 编码,没有支持 unicode, hz码等,
  10. //          修改代码,使其可以在任意版本delphi执行
  11. //          没有实现多音字自动使用正确的(如果要那样,就需要很大的词语库)
  12. //原始版本:某人写的 V4.1
  13. //修改:去类化,修改命名
  14. //编写:孙晓刚
  15. //日期: 2013-11-20
  16. //=============================================================================

  17. unit HanyuPinyin;

  18. {$IFDEF FPC}
  19.   {$MODE Delphi}
  20. {$ENDIF}

  21. interface

  22. uses
  23. {$IFNDEF FPC}
  24.   Windows,
  25. {$ELSE}
  26. {$ENDIF}
  27.   Messages, SysUtils, Classes;

  28. //函数1:GetHanyuPinyinByChar
  29. //功能:获得一个非中文字符或者1个中文字符的汉语拼音全拼串
  30. //参数:AValue  -- 1字节 英文/其他单字节字符
  31. //             -- 2字节 中文,GBK/Cp936编码
  32. //返回:1个字符(1字节/2字节)的全拼字符串
  33. function GetHanyuPinyinByChar(AValue: pAnsiChar; Len: Integer): AnsiString;

  34. //函数2:getHanyuPinyinFull
  35. //功能:获得一个中英文混合字符串的各个字符的拼音码+英文混合字符串,是全拼音
  36. //参数:avalue -- 中英文混合字符串
  37. //返回:1个字符串的全拼字符串
  38. function getHanyuPinyinFull(AValue: AnsiString): AnsiString;

  39. //函数2:getHanyuPinyinShort
  40. //功能:获得一个中英文混合字符串的各个字符的拼音码+英文混合字符串,是全拼音
  41. //参数:avalue -- 中英文混合字符串
  42. //返回:1个字符串的拼音首字母串(英文取本身,希腊字母,罗马数字都转换成响应的英文表示)
  43. function GetHanyuPinyinShort(AValue: AnsiString): AnsiString;

  44. //这个实际上只用在内部,得到首个字节的字符
  45. //function GetFirstLetter(AValue: pAnsiChar; Len: Integer): AnsiString;

  46. implementation

  47. {$I HanyuPinyin.set}
  48. {$I HanyuPinyin.inc}


  49. function GetHanyuPinyinByChar(AValue: pAnsiChar; Len: Integer): AnsiString;
  50. var
  51.   C: AnsiChar;
  52.   Index: Integer;
  53. begin
  54.   Result := '';
  55.   if (Len > 1) and (AValue[0] >= #129) and (AValue[1] >= #64) then
  56.   begin
  57.     //是否为 GBK 字符
  58.     case AValue[0] of
  59.       #163:  // 全角 ASCII
  60.       begin
  61.         C := AnsiChar(Ord(AValue[1]) - 128);
  62.         if C in ['a'..'z', 'A'..'Z', '0'..'9', '(', ')', '[', ']'] then
  63.           Result := C
  64.         else
  65.           Result := '';
  66.       end;
  67.       #162: // 罗马数字
  68.       begin
  69.         if AValue[1] > #160 then
  70.           Result := CharIndex[Ord(AValue[1]) - 160]
  71.         else
  72.           Result := '';
  73.       end;
  74.       #166: // 希腊字母
  75.       begin
  76.         if AValue[1] in [#$A1..#$B8] then
  77.           Result := CharIndex2[Ord(AValue[1]) - $A0]
  78.         else if AValue[1] in [#$C1..#$D8] then
  79.           Result := CharIndex2[Ord(AValue[1]) - $C0]
  80.         else
  81.           Result := '';
  82.       end;
  83.       else
  84.       begin  // 获得拼音索引
  85.         Index := hanyu_pinyin_gbk_index[Ord(AValue[0]) - 128, Ord(AValue[1]) - 63];
  86.         if Index = 0 then
  87.           Result := ''
  88.         else
  89.           Result := hanyu_pinyin_index[Index];
  90.       end;
  91.     end;
  92.   end
  93.   else if Len > 0 then
  94.   begin
  95.     //在 GBK 字符集外, 即半角字符
  96.     if AValue[0] in ['a'..'z', 'A'..'Z', '0'..'9'{$ifdef NEEDSymbols}, '(', ')', '[', ']',
  97.       '.', '!', '@', '#', 'py单元2:
  98. [code]unit py;

  99. {$mode objfpc}{$H+}

  100. interface

  101. uses
  102.   Classes, SysUtils;

  103. function GetHzPy(HzChar: PAnsiChar; Len: Integer): string;
  104. function GetHzPyFull(HzChar: string): string;
  105. function GetPyChars(HzChar: string): string;
  106. function GetHzPyHead(HzChar: PAnsiChar; Len: Integer): String;
  107. function CheckIsGB2312(AChar: WideChar): Boolean;
  108. function StrIsCanShow(const WS: WideString): Boolean;

  109. function HZ2PY(const HZ: string; ISPYHead: Boolean = True): string;

  110. implementation

  111. {$i HzSpDat2.inc}

  112. function GetHzPy(HzChar: PAnsiChar; Len: Integer): string;
  113. var
  114.   C: Char;
  115.   Index: Integer;
  116. begin
  117.   Result := '';
  118.   if (Len > 1) and (HzChar[0] >= #129) and (HzChar[1] >= #64) then
  119.   begin
  120.     //是否为 GBK 字符
  121.     case HzChar[0] of
  122.       #163:  // 全角 ASCII
  123.       begin
  124.         C := Chr(Ord(HzChar[1]) - 128);
  125.         if C in ['a'..'z', 'A'..'Z', '0'..'9', '(', ')', '[', ']'] then
  126.           Result := C
  127.         else
  128.           Result := '';
  129.       end;
  130.       #162: // 罗马数字
  131.       begin
  132.         if HzChar[1] > #160 then
  133.           Result := CharIndex[Ord(HzChar[1]) - 160]
  134.         else
  135.           Result := '';
  136.       end;
  137.       #166: // 希腊字母
  138.       begin
  139.         if HzChar[1] in [#$A1..#$B8] then
  140.           Result := CharIndex2[Ord(HzChar[1]) - $A0]
  141.         else if HzChar[1] in [#$C1..#$D8] then
  142.           Result := CharIndex2[Ord(HzChar[1]) - $C0]
  143.         else
  144.           Result := '';
  145.       end;
  146.       else
  147.       begin  // 获得拼音索引
  148.         Index := PyCodeIndex[Ord(HzChar[0]) - 128, Ord(HzChar[1]) - 63];
  149.         if Index = 0 then
  150.           Result := ''
  151.         else
  152.           Result := PyMusicCode[Index];
  153.       end;
  154.     end;
  155.   end
  156.   else if Len > 0 then
  157.   begin
  158.     //在 GBK 字符集外, 即半角字符
  159.     if HzChar[0] in ['a'..'z', 'A'..'Z', '0'..'9', '(', ')', '[', ']',
  160.       '.', '!', '@', '#', '
  161. 以上方式均在winxp+lazarus 1.0.14下测试通过
  162. 感谢欧文
  163. 测试程序如下:

  164. , '%', '^', '&', '*', '-', '+',
  165.       '<', '>', '?', ':', '"'{$endif}] then
  166.       Result := AValue[0]
  167.     else
  168.       Result := '';
  169.   end;
  170. end;

  171. function getHanyuPinyinFull(AValue: AnsiString): AnsiString;
  172. var
  173.   i, len: Integer;
  174.   Py: AnsiString;
  175.   function IsLeadCharOfGBK(C: AnsiChar): Boolean;
  176.   begin
  177.     Result := C >= #129;
  178.   end;
  179. begin
  180.   Result := '';
  181.   i := 1;
  182.   while i <= Length(AValue) do
  183.   begin
  184.     if IsLeadCharOfGBK(AValue[i]) and (Length(AValue) - i > 0) then
  185.       len := 2
  186.     else
  187.       len := 1;
  188.     Py := GetHanyuPinyinByChar(@AValue[i], len);
  189.     Inc(i, len);
  190.     if (Result <> '') and (Py <> '') then
  191.       Result := Result + ' ' + Py
  192.     else
  193.       Result := Result + Py;
  194.   end;
  195. end;

  196. function GetFirstLetter(AValue: pAnsiChar; Len: Integer): AnsiString;
  197. begin
  198.   Result := Copy(GetHanyuPinyinByChar(AValue, Len), 1, 1);
  199. end;

  200. function GetHanyuPinyinShort(AValue: AnsiString): AnsiString;
  201. var
  202.   i, len: Integer;
  203.   Py: AnsiString;
  204.   function IsLeadCharOfGBK(C: AnsiChar): Boolean;
  205.   begin
  206.     Result := C >= #129;
  207.   end;
  208. begin
  209.   Result := '';
  210.   i := 1;
  211.   while i <= Length(AValue) do
  212.   begin
  213.     if IsLeadCharOfGBK(AValue[i]) and (Length(AValue) - i > 0) then
  214.       len := 2
  215.     else
  216.       len := 1;
  217.     Py := GetFirstLetter(@AValue[i], len);
  218.     Inc(i, len);
  219.     Result := Result + Py;
  220.   end;
  221. end;

  222. end.
复制代码
py单元 方式2:
  1. unit py;

  2. {$mode objfpc}{$H+}

  3. interface

  4. uses
  5.   Classes, SysUtils;

  6. function GetHzPy(HzChar: PAnsiChar; Len: Integer): string;
  7. function GetHzPyFull(HzChar: string): string;
  8. function GetPyChars(HzChar: string): string;
  9. function GetHzPyHead(HzChar: PAnsiChar; Len: Integer): String;
  10. function CheckIsGB2312(AChar: WideChar): Boolean;
  11. function StrIsCanShow(const WS: WideString): Boolean;

  12. function HZ2PY(const HZ: string; ISPYHead: Boolean = True): string;

  13. implementation

  14. {$i HzSpDat2.inc}

  15. function GetHzPy(HzChar: PAnsiChar; Len: Integer): string;
  16. var
  17.   C: Char;
  18.   Index: Integer;
  19. begin
  20.   Result := '';
  21.   if (Len &gt; 1) and (HzChar[0] &gt;= #129) and (HzChar[1] &gt;= #64) then
  22.   begin
  23.     //是否为 GBK 字符
  24.     case HzChar[0] of
  25.       #163:  // 全角 ASCII
  26.       begin
  27.         C := Chr(Ord(HzChar[1]) - 128);
  28.         if C in ['a'..'z', 'A'..'Z', '0'..'9', '(', ')', '[', ']'] then
  29.           Result := C
  30.         else
  31.           Result := '';
  32.       end;
  33.       #162: // 罗马数字
  34.       begin
  35.         if HzChar[1] &gt; #160 then
  36.           Result := CharIndex[Ord(HzChar[1]) - 160]
  37.         else
  38.           Result := '';
  39.       end;
  40.       #166: // 希腊字母
  41.       begin
  42.         if HzChar[1] in [#$A1..#$B8] then
  43.           Result := CharIndex2[Ord(HzChar[1]) - $A0]
  44.         else if HzChar[1] in [#$C1..#$D8] then
  45.           Result := CharIndex2[Ord(HzChar[1]) - $C0]
  46.         else
  47.           Result := '';
  48.       end;
  49.       else
  50.       begin  // 获得拼音索引
  51.         Index := PyCodeIndex[Ord(HzChar[0]) - 128, Ord(HzChar[1]) - 63];
  52.         if Index = 0 then
  53.           Result := ''
  54.         else
  55.           Result := PyMusicCode[Index];
  56.       end;
  57.     end;
  58.   end
  59.   else if Len &gt; 0 then
  60.   begin
  61.     //在 GBK 字符集外, 即半角字符
  62.     if HzChar[0] in ['a'..'z', 'A'..'Z', '0'..'9', '(', ')', '[', ']',
  63.       '.', '!', '@', '#', '
  64. 以上方式均在winxp+lazarus 1.0.14下测试通过
  65. 感谢欧文
  66. 测试程序如下:

  67. , '%', '^', '&', '*', '-', '+',
  68.       '<', '>', '?', ':', '"'] then
  69.       Result := HzChar[0]
  70.     else
  71.       Result := '';
  72.   end;
  73. end;

  74. function GetHzPyFull(HzChar: string): string;
  75. var
  76.   i, len: Integer;
  77.   Py: String;
  78.   function IsDouByte(C: Char): Boolean;
  79.   begin
  80.     Result := C >= #129;
  81.   end;
  82. begin
  83.   Result := '';
  84.   i := 1;
  85.   while i <= Length(HzChar) do
  86.   begin
  87.     if IsDouByte(HzChar[i]) and (Length(HzChar) - i > 0) then
  88.       len := 2
  89.     else
  90.       len := 1;
  91.     Py := GetHzPy(@HzChar[i], len);
  92.     Inc(i, len);
  93.     if (Result <> '') and (Py <> '') then
  94.       Result := Result + ' ' + Py
  95.     else
  96.       Result := Result + Py;
  97.   end;
  98. end;

  99. function GetPyChars(HzChar: string): string;
  100. var
  101.   i, len: Integer;
  102.   Py: String;
  103.   function IsDouByte(C: Char): Boolean;
  104.   begin
  105.     Result := C >= #129;
  106.   end;
  107. begin
  108.   Result := '';
  109.   i := 1;
  110.   while i <= Length(HzChar) do
  111.   begin
  112.     if IsDouByte(HzChar[i]) and (Length(HzChar) - i > 0) then
  113.       len := 2
  114.     else
  115.       len := 1;
  116.     Py := GetHzPyHead(@HzChar[i], len);
  117.     Inc(i, len);
  118.     Result := Result + Py;
  119.   end;
  120. end;

  121. function GetHzPyHead(HzChar: PAnsiChar; Len: Integer): String;
  122. begin
  123.   Result := Copy(GetHzPy(HzChar, Len), 1, 1);
  124. end;

  125. function CheckIsGB2312(AChar: WideChar): Boolean;
  126. var
  127.   S: AnsiString;
  128. begin
  129.   S := AChar;
  130.   Result := (PByte(integer(S)+1)^>=$A1) and (PByte(integer(S)+1)^<=$FE) and
  131.     (PByte(S)^>=$B0) and (PByte(S)^<=$F7);
  132. end;

  133. function StrIsCanShow(const WS: WideString): Boolean;
  134. var
  135.   i: integer;
  136.   P: PWideChar;
  137. begin
  138.   Result := True;
  139.   P := Pointer(WS);
  140.   for i:=1 to Length(WS) do begin
  141.     if not (
  142.              ((PWord(P)^>=$20) and (PWord(P)^<=$7E)) //Ansi 可见字符
  143.              or CheckIsGB2312(P^)                    //GB2312汉字及符号
  144.            ) then  begin
  145.       Result := False;
  146.       Break;
  147.     end;
  148.     Inc(P);
  149.   end;
  150. end;

  151. function HZ2PY(const HZ: string; ISPYHead: Boolean): string;
  152. begin
  153.   if ISPYHead then Result := Trim(GetPyChars(Trim(HZ)))
  154.   else Result := Trim(GetHzPyFull(Trim(HZ)));
  155.   if not StrIsCanShow(Result) then Result := '';
  156. end;

  157. end.
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册(注册审核可向QQ群索取)

x
回复

使用道具 举报

  • TA的每日心情
    开心
    2020-9-18 14:51
  • 签到天数: 47 天

    [LV.5]常住居民I

    发表于 2014-1-13 12:32:42 | 显示全部楼层
    谢谢分享。
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    发表于 2014-10-29 13:03:22 | 显示全部楼层
    谢谢分享.请问是什么许可的,可以使用吗?
    回复 支持 反对

    使用道具 举报

    *滑块验证:

    本版积分规则

    QQ|手机版|小黑屋|Lazarus中国|Lazarus中文社区 ( 鄂ICP备16006501号-1 )

    GMT+8, 2025-5-2 21:41 , Processed in 0.034638 second(s), 13 queries , Redis On.

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表