zoukankan      html  css  js  c++  java
  • 在SQL中实现汉字转全拼

    这个一定要拼音库的.Top

    2 楼zjcxc(邹建)回复于 2006-06-01 19:54:20 得分 95
    /*--从全拼音中得到汉字拼音  
       
      首先,用输入法生成器(Imegen.exe)的逆转换功能  
      将全拼的码表文件   WINPY.MB   转换成文本文件   c:\winpy.txt  
      然后用下面的语句导入到数据库中  
        下面只是显示了读取并显示的过程,并没有存储到具体的表中  
       
      读取语句中的:  
      with(datafiletype='widechar')     的作用是处理unicode数据  
      我是用win2000测试的,转换的文本文件编码是unicode  
      如果是编码是ansi,则不要这句  
       
      查看文本文件编码,可以用记事本打开文本文件,另存为,就可以看到当前编码  
      --*/  
       
      --创建临时表  
      create   table   #t(a   varchar(500))  
       
      --导入数据  
      bulk   insert   #t   from   'c:\winpy.txt'  
      with(datafiletype='widechar')  
       
      --删除表头  
      set   rowcount   12  
      delete   from   #t  
      set   rowcount   0  
       
      --分拆处理  
      select   汉字=left(a,patindex('%[a-z]%',a)-1)  
      ,拼音=stuff(a,1,patindex('%[a-z]%',a)-1,'')  
      from   #t  
      --where   patindex('%[a-z]%',a)=2     --如果是获得单字的读音  
       
      --删除测试  
      drop   table   #tTop

    3 楼zjcxc(邹建)回复于 2006-06-01 19:56:16 得分 0
    --   使用拼音库处理拼音的示例  
       
       
      /*--获得汉字拼音的函数  
       
            需要创建一个拼音表,包含所有汉字的发音,这个可以通过
    转换全拼输入法的编码库得到,这里仅举了一个简单的例子.  
      --邹建   2003.10--*/  
       
      --创建汉字拼音库  
      create   table   YingShe(CHR     char(2),PY   varchar(10))  
      insert   YingShe    
      select   '长','chang'  
      union   all   select   '长','zhang'  
      union   all   select   '城','cheng'  
      union   all   select   '科','kel'  
      union   all   select   '技','ji'  
      union   all   select   '金','jin'  
      union   all   select   '立','li'  
      union   all   select   '章','zhang'  
      union   all   select   '公','gong'  
      union   all   select   '司','si'  
       
      /*--下面是两个函数,一个以表的形式返回某个字符串的全部拼音,一个返回某某个字符串的其中一个拼音  
      --*/  
       
      go  
      --获取汉字拼音的函数--返回所有的拼音  
      create   function   f_getpy_tb(@str   varchar(100))  
      returns   @tb   table(re   varchar(8000))  
      as  
      begin  
      declare   @re   table(id   int,re   varchar(8000))     --数据处理中间表  
       
      declare   @i   int,@ilen   int,@splitchr   varchar(1)  
      select   @splitchr='   ' --两个拼音之间的分隔符(目的是为了通用性考虑)  
      ,@i=1,@ilen=len(@str)  
       
      insert   into   @re   select   @i,py   from   YingShe   where   chr=substring(@str,@i,1)  
      while   @i<@ilen  
      begin  
      set   @i=@i+1  
      insert   into   @re   select   @i,re+@splitchr+py   from   @re   a,YingShe   b  
      where   a.id=@i-1   and   b.chr=substring(@str,@i,1)  
      end  
       
      insert   into   @tb   select   re   from   @re   where   id=@i  
      return    
      end  
      go  
       
      --获取汉字拼音的函数--返回汉字的某一个拼音  
      create   function   f_getpy(@str   varchar(100))  
      returns   varchar(8000)  
      as  
      begin  
      declare   @re   varchar(8000)  
       
      declare   @i   int,@ilen   int,@splitchr   varchar(1)  
      select   @splitchr='   ' --两个拼音之间的分隔符(目的是为了通用性考虑)  
      ,@i=1,@ilen=len(@str)  
       
      select   @re=py   from   YingShe   where   chr=substring(@str,@i,1)  
      while   @i<@ilen  
      begin  
      set   @i=@i+1  
      select   top   1   @re=@re+@splitchr+py  
      from   YingShe   where   chr=substring(@str,@i,1)  
      end  
       
      return(@re)  
      end  
      go  
       
      --测试  
      --返回'长城'的所有可能拼音  
      select   *   from   dbo.f_getpy_tb('长城')  
       
        --应用案例:  
       
      create   table   MyTable(Name   varchar(10),ID   int)  
        insert   MyTable   select   '长城科技',1  
      union   all   select   '金长城科技',2  
        union   all   select   '立章城公司',3  
       
      go  
       
      --查询包含与'长城'拼音一样的记录:  
      select   *   from   mytable  
      where   exists(select   1   from   dbo.f_getpy_tb('长城')   where   dbo.f_getpy(name)   like   '%'+re+'%')  
       
      --删除表  
      drop   table   YingShe,mytable  
      --删除拼音函数  
      drop   function   f_getpy,f_getpy_tb  
     

    原文:http://topic.csdn.net/t/20060601/19/4794741.html

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhou__zhou/archive/2007/12/10/1927679.aspx

  • 相关阅读:
    使用KNN算法手写体识别
    os内置模块
    python步长为负时的情况
    qplot()函数的详细用法
    python文件I/O
    python中 @property
    python中定制类
    python中多重继承与获取对象
    python继承,判断类型,多态
    python中访问限制
  • 原文地址:https://www.cnblogs.com/0000/p/1516265.html
Copyright © 2011-2022 走看看