一个利用拼音与汉字混合过滤字符串的函数

来源:互联网 发布:人工智能885728 编辑:程序博客网 时间:2024/04/30 01:11

type
  TChn = array[1..27] of string;

const
  ChnA = '啊阿呵吖锕嗄腌爱矮挨哎碍癌艾唉哀蔼隘埃皑呆捱嗳嫒瑷暧砹锿霭嗌袄凹傲奥'+
         '熬懊敖翱澳嚣坳嗷岙廒遨媪骜獒聱螯鏊鳌鏖拗按安暗岸俺案鞍氨胺谙埯揞犴庵'+
         '桉铵鹌黯'#163#193#163#225+'Aa
';

  ChnB = '捌扒叭吧笆八疤巴拔跋靶把耙坝霸罢爸白柏百摆佰败拜稗斑班搬扳般颁板版扮'+
         '拌伴瓣半办绊邦帮梆榜膀绑棒磅蚌镑傍谤苞胞包褒剥薄雹保堡饱宝抱报暴豹鲍'+
         '爆杯碑悲卑北辈背贝钡倍狈备惫焙被奔苯本笨崩绷甭泵蹦迸逼鼻比鄙笔彼碧蓖'+
         '蔽毕毙毖币庇痹闭敝弊必辟壁臂避陛鞭边编贬扁便变卞辨辩辫遍标彪膘表鳖憋'+
         '别瘪彬斌濒滨宾摈兵冰柄丙秉饼炳病并玻菠播拨钵波博勃搏铂箔伯帛舶脖膊渤'+
         '泊驳捕卜哺补埠不布步簿部怖'#163#194#163#226
+'Bb';


...

  Chn: TChn =
    (ChnA, ChnB, ChnC, ChnD, ChnE, ChnF, ChnG, ChnH, ChnI,
     ChnJ, ChnK, ChnL, ChnM, ChnN, ChnO, ChnP, ChnQ, ChnR,
     ChnS, ChnT, ChnU, ChnV, ChnW, ChnX, ChnY, ChnZ, Chn27);

function IsMBCSChar(const ch: Char): Boolean;
begin
Result := ord(ch) >127;//ByteType(ch, 1) <> mbSingleByte;
end;


function PYinChinese(ChnStr, InputStr: string): Boolean;

var
  P, Q: PChar;
  ID, IP: Integer;
  Dt: string;
  asterisk: Boolean;

  function GetChinesePYID: Integer;
  begin
    case P^ of
      'a'..'z': Result := ord(P^) - ord('a') + 1;
      'A'..'Z': Result := ord(P^) - ord('A') + 1;
      '0'..'9': Result := 27;
      '*': Result := 28;
      '?': Result := 29;           

      else
        if IsMBCSChar(P^) then
          Result := 0
        else
          Result := -1;
    end;

  end;


begin
  InputStr := UpperCase(InputStr) + #0;
  ChnStr := UpperCase(ChnStr) + #0;
  P := Pointer(InputStr);
  Q := Pointer(ChnStr);
  Result := False;
  MH := False;


  while (P^ <> #0) and (Q^ <> #0) do
  begin

    ID := GetChinesePYID;
    case ID of
      1..26:
        if not asterisk  then
        begin    //待查的串
          Dt := Q^;
          Result := Pos(Dt, CHN[ID]) > 0;
          Inc(P);
          Inc(Q);
        end
        else  //匹配星号
        begin
          Result := False;
          while (Result = False) and (Q^ <> #0) do
          begin
            Dt := Q^;
            Result := Pos(Dt, CHN[ID]) > 0;
            Inc(Q);
          end;

          asterisk := False;
          Inc(P);
        end;
      28: begin
            Result := True;
            asterisk := True;
            Inc(P);
          end;
      29: begin
            Result := True;
            Inc(P);
            Inc(Q);
          end;
      0: begin
           if asterisk then
           begin
             Dt := Q;
             IP := Pos(P^, Dt);
             Result := IP > 0;
             Inc(P);
             Inc(Q, IP);
             asterisk := False;
           end
           else
           begin
             Dt := Q;
             Result := Pos(P^, Dt) = 1;
             Inc(P);
             Inc(Q);

           end;
         end;
      -1: Result := False;
    end;

    if Result = False then Exit;
  end;
  if (P^ <> #0) and Result then Result := False;

end;



使用方法


var

   AChstr: TStringList;

  

  AChStr.Add('国际银行家');

  AChStr.Add('过滤字符串');

...

找出包含  ‘*gj’   或   (*国际) 或  (*g际)  的串 


for I := 0  to AChStr.Count - 1 do

   if  PYinChinese(AChStr[I], '*GJ') then 

       dosomething;


下面给出本人一个软件中的示例