Author: 水如烟

这个版本已有使用价值。如果要求不高,基本满足需要。一并贴出来,也对比一下。

这个版本的汉字库已纳入程序资源里头,大小为324K。

主要代码如下:

Namespace  Businness.PinYin
     Public   Class  SpellInformation
         Private  gTone  As   String
         Private  gSpellInput  As   String
         Private  gSpellExpress  As   String

' '' <summary>
         ' '' 声调
         ' '' </summary>
         Public   Property  Tone()  As   String
             Get
                 Return  gTone
             End   Get
             Set ( ByVal  value  As   String )
                gTone  =  value.Trim
             End   Set
         End Property

' '' <summary>
         ' '' 拼音输入码
         ' '' </summary>
         Public   Property  SpellInput()  As   String
             Get
                 Return  gSpellInput
             End   Get
             Set ( ByVal  value  As   String )
                gSpellInput  =  value.Trim
             End   Set
         End Property

' '' <summary>
         ' '' 拼音
         ' '' </summary>
         Public   Property  SpellExpress()  As   String
             Get
                 Return  gSpellExpress
             End   Get
             Set ( ByVal  value  As   String )
                gSpellExpress  =  value.Trim
             End   Set
         End Property

Sub   New ()
         End Sub

' '' <param name="tone">声调</param>
         ' '' <param name="spellinput">拼音输入码</param>
         ' '' <param name="spellexpress">拼音</param>
         Sub   New ( ByVal  tone  As   String ,  ByVal  spellinput  As   String ,  ByVal  spellexpress  As   String )
             Me .Tone  =  tone
             Me .SpellInput  =  spellinput
             Me .SpellExpress  =  spellexpress
         End Sub

' '' <param name="spellexpress">拼音</param>
         Public   Overrides   Function  Equals( ByVal  spellexpress  As   Object )  As   Boolean
             Return   Me .SpellExpress.Equals(spellexpress)
         End Function

Public   Overrides   Function  ToString()  As   String
             Return   String .Format( " 拼音码 {0,-6} 声调 {1} 拼音 {2} " ,  Me .SpellInput,  Me .Tone,  Me .SpellExpress)
         End Function
     End Class

End Namespace

Namespace  Businness.PinYin
     Public   Class  Word
         Private  gValue  As   String

' '' <param name="word">单个汉字</param>
         Sub   New ( ByVal  word  As   String )
             Me .gValue  =  word
         End Sub

' '' <summary>
         ' '' 汉字
         ' '' </summary>
         Public   ReadOnly   Property  Value()  As   String
             Get
                 Return  gValue
             End   Get
         End Property

Public   ReadOnly   Property  Code()  As   String
             Get
                 Return  Common.Code(gValue)
             End   Get
         End Property

Private  gSpellList  As   New  Dictionary( Of   String , SpellInformation)

' '' <summary>
         ' '' 拼音集
         ' '' </summary>
         Public   ReadOnly   Property  Spells()  As  SpellInformation()
             Get
                 Dim  tmp(gSpellList.Count  -   1 )  As  SpellInformation
                gSpellList.Values.CopyTo(tmp,  0 )
                 Return  tmp
             End   Get
         End Property

' '' <summary>
         ' '' 是否多音字
         ' '' </summary>
         Public   ReadOnly   Property  IsMutiSpell()  As   Boolean
             Get
                 Return  gSpellList.Count  >   1
             End   Get
         End Property

Public   Sub  AddSpell( ByVal  spell  As  SpellInformation)
             If   String .IsNullOrEmpty(spell.SpellExpress)  Then   Exit Sub

If   Me .gSpellList.ContainsKey(spell.SpellExpress)  Then   Exit Sub

Me .gSpellList.Add(spell.SpellExpress, spell)
         End Sub

Public   Sub  AddSpell( ByVal  tone  As   String ,  ByVal  spellinput  As   String ,  ByVal  spellexpress  As   String )
             Dim  tmp  As   New  SpellInformation(tone, spellinput, spellexpress)

AddSpell(tmp)
         End Sub

Public   ReadOnly   Property  DefaultSpell()  As  SpellInformation
             Get
                 If   Me .gSpellList.Count  =   0   Then   Return   Nothing
                 Return   Me .Spells( 0 )
             End   Get
         End Property

Public   ReadOnly   Property  DefaultSpellExpress()  As   String
             Get
                 If  DefaultSpell  Is   Nothing   Then   Return   Nothing
                 Return   Me .DefaultSpell.SpellExpress
             End   Get
         End Property

Public   ReadOnly   Property  DefaultSpellInput()  As   String
             Get
                 If  DefaultSpell  Is   Nothing   Then   Return   Nothing
                 Return   Me .DefaultSpell.SpellInput
             End   Get
         End Property

Public   ReadOnly   Property  DefaultTone()  As   String
             Get
                 If  DefaultSpell  Is   Nothing   Then   Return   Nothing
                 Return   Me .DefaultSpell.Tone
             End   Get
         End Property

Public   ReadOnly   Property  AllSpellExpress()  As   String
             Get
                 If   Me .gSpellList.Count  =   0   Then   Return   Nothing

If   Not   Me .IsMutiSpell  Then   Return   Me .DefaultSpellExpress

Dim  tmp(gSpellList.Count  -   1 )  As   String
                 For  i  As   Integer   =   0   To  gSpellList.Count  -   1
                    tmp(i)  =   Me .Spells(i).SpellExpress
                 Next

Return   String .Join( "   " , tmp)
             End   Get
         End Property

Public   ReadOnly   Property  AllSpellInput()  As   String
             Get
                 If   Me .gSpellList.Count  =   0   Then   Return   Nothing

If   Not   Me .IsMutiSpell  Then   Return   Me .DefaultSpellInput

Dim  tmp(gSpellList.Count  -   1 )  As   String
                 For  i  As   Integer   =   0   To  gSpellList.Count  -   1
                    tmp(i)  =   Me .Spells(i).SpellInput
                 Next

Return   String .Join( "   " , tmp)
             End   Get
         End Property

Public   ReadOnly   Property  AllTone()  As   String
             Get
                 If   Me .gSpellList.Count  =   0   Then   Return   Nothing

If   Not   Me .IsMutiSpell  Then   Return   Me .DefaultTone

Dim  tmp(gSpellList.Count  -   1 )  As   String
                 For  i  As   Integer   =   0   To  gSpellList.Count  -   1
                    tmp(i)  =   Me .Spells(i).Tone
                 Next

Return   String .Join( "   " , tmp)
             End   Get
         End Property

Public   Overrides   Function  ToString()  As   String
             Dim  mBuilder  As   New  System.Text.StringBuilder
             For   Each  spell  As  SpellInformation  In   Me .Spells
                mBuilder.AppendLine( String .Concat( Me .Value,  "   " , spell.ToString))
             Next
             Return  mBuilder.ToString
         End Function
     End Class

End Namespace

Imports  System.IO
Imports  System.Text.RegularExpressions

Namespace  Businness.PinYin
     Public   Class  PYService
         Private  gDataSet  As  dsPinYin

' '' <summary>
         ' '' 汉字表
         ' '' </summary>
         Public   ReadOnly   Property  PinYinTable()  As  dsPinYin.PinYinDataTable
             Get
                 Return  gDataSet.PinYin
             End   Get
         End Property

' '' <summary>
         ' '' 单个汉字信息
         ' '' </summary>
         ' '' <param name="word">单个汉字</param>
         Public   Function  GetWord( ByVal  word  As   String )  As  Word
             Dim  mRow  As  dsPinYin.PinYinRow  =   Me .gDataSet.PinYin.FindBy代码(GetCode(word))

Return  RowConverter(mRow)
         End Function

Private   Function  RowConverter( ByVal  row  As  dsPinYin.PinYinRow)  As  Word
             If  row  Is   Nothing   Then   Return   Nothing

Dim  mWord  As   New  Word(row.汉字)

Dim  mSpellExpressArray()  As   String   =  row.拼音.Split( "   " c)
             Dim  mToneArray()  As   String   =  row.声调.Split( "   " c)
             Dim  mSpellInputArray()  As   String   =  row.拼音码.Split( "   " c)

For  i  As   Integer   =   0   To  mSpellExpressArray.Length  -   1
                mWord.AddSpell(mToneArray(i), mSpellInputArray(i), mSpellExpressArray(i))
             Next

Return  mWord
         End Function

Private   Sub  RowUpdate( ByVal  word  As  Word)
             Dim  mWord  As  Word  =  GetWord(word.Value)
             If  mWord  Is   Nothing   Then
                 Dim  tmpRow  As  dsPinYin.PinYinRow  =   Me .gDataSet.PinYin.AddPinYinRow(word.Value, word.Code,  "" ,  "" ,  "" ,  True )
                mWord  =  RowConverter(tmpRow)
             End   If

For   Each  spell  As  SpellInformation  In  word.Spells
                mWord.AddSpell(spell)
             Next

Dim  mRow  As  dsPinYin.PinYinRow  =   Me .gDataSet.PinYin.FindBy代码(mWord.Code)
             With  mRow
                .拼音码  =  mWord.AllSpellInput
                .拼音  =  mWord.AllSpellExpress
                .声调  =  mWord.AllTone
                .单音  =   Not  mWord.IsMutiSpell
             End   With

End Sub

Public   Sub  Load()
            UpdateFromTxt()
         End Sub

' 文件存放的格式是:汉字,拼音,音调,拼音码
         Private   Sub  UpdateFromTxt()
             Me .gDataSet  =   New  dsPinYin

LzmTW.uSystem.uCollections.CommonServices.MoveNext( Of   String )(My.Resources.pinyin.Split( CChar (vbCrLf)),  AddressOf  Action)

Me .gDataSet.AcceptChanges()
         End Sub

' 文件存放的格式是:汉字,拼音,音调,拼音码
         Private   Sub  Action( ByVal  line  As   String )

Dim  mArray  As   String ()
            mArray  =  line.Split( " , " c)

If  mArray.Length  <>   4   Then   Exit Sub

Dim  mWord  As   String   =  mArray( 0 ).Trim
             Dim  mSpellExpress  As   String   =  mArray( 1 ).Trim
             Dim  mTone  As   String   =  mArray( 2 ).Trim
             Dim  mSpellInput  As   String   =  mArray( 3 ).Trim

Dim  mWordInformation  As   New  Word(mWord)
            mWordInformation.AddSpell(mTone, mSpellInput, mSpellExpress)

RowUpdate(mWordInformation)

End Sub

' '' <summary>
         ' '' 将字符串转为拼音
         ' '' </summary>
         ' '' <param name="line">字符串</param>
         ' '' <param name="isgetfirst">如是多音字,取第一个拼音</param>
         ' '' <param name="forInput">是则查拼音码,否则查拼音</param>
         Public   Function  ToPinyin( ByVal  line  As   String ,  ByVal  isgetfirst  As   Boolean ,  ByVal  forInput  As   Boolean )  As   String
             Dim  mBuilder  As   New  Text.StringBuilder

For   Each  s  As   Char   In  line.ToCharArray
                 If  Common.IsSingleWord(s)  Then
                    mBuilder.Append(GetPinyin(s, isgetfirst, forInput))
                 Else
                    mBuilder.Append(s)
                 End   If
             Next

Return  mBuilder.ToString
         End Function

Private   Function  GetPinyin( ByVal  word  As   String ,  ByVal  isgetfirst  As   Boolean ,  ByVal  forInput  As   Boolean )  As   String
             Dim  mResult  As   String

Dim  mWord  As  Word  =  GetWord(word)

If  isgetfirst  Or   Not  mWord.IsMutiSpell  Then
                 If  forInput  Then
                    mResult  =  mWord.DefaultSpellInput
                 Else
                    mResult  =  mWord.DefaultSpellExpress
                 End   If

Else
                 If  forInput  Then

Dim  tmpList( - 1 )  As   String

For   Each  spell  As  SpellInformation  In  mWord.Spells
                         If  Array.IndexOf(tmpList, spell.SpellInput)  =   - 1   Then
                            LzmTW.uSystem.uCollections.CommonServices.Append(tmpList, spell.SpellInput)
                         End   If
                     Next

If  tmpList.Length  =   1   Then
                        mResult  =  mWord.DefaultSpellInput
                     Else
                        mResult  =   String .Format( " ({0}) " ,  String .Join( "   " , tmpList))
                     End   If
                 Else
                    mResult  =   String .Format( " ({0}) " , mWord.AllSpellExpress)
                 End   If

End   If

Return  mResult
         End Function

' '' <summary>
         ' '' 按拼音查字
         ' '' </summary>
         ' '' <param name="pinyin">拼音</param>
         Public   Function  WordArray( ByVal  pinyin  As   String )  As   String ()
             Dim  mRows  As  dsPinYin.PinYinRow()  =   CType ( Me .gDataSet.PinYin.Select( String .Format( " 拼音码 LIKE '%{0}%' " , pinyin)), dsPinYin.PinYinRow())
             Dim  mResult( - 1 )  As   String
             For  i  As   Integer   =   0   To  mRows.Length  -   1
                 If  Array.IndexOf(mRows(i).拼音码.Split( "   " c), pinyin)  <>   - 1   Then
                    LzmTW.uSystem.uCollections.CommonServices.Append(mResult, mRows(i).汉字)
                 End   If
             Next
             Return  mResult
         End Function

' '' <summary>
         ' '' 按拼音查字
         ' '' </summary>
         ' '' <param name="pinyin">拼音</param>
         Public   Function  Words( ByVal  pinyin  As   String )  As   String
             Return   String .Concat(WordArray(pinyin))
         End Function

Public   Function  GetCode( ByVal  word  As   String )  As   String
             Return  Common.Code(word)
         End Function

End Class
End Namespace

调用的代码

Public   Class  Form1
     Dim  gPinyinService  As   New  LzmTW.Businness.PinYin.PYService

Private   Sub  ButtonLoad_Click( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  ButtonLoad.Click
        gPinyinService.Load()
         Me .DataGridView1.DataSource  =  gPinyinService.PinYinTable
     End Sub

Private   Sub  ButtonTran_Click( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  ButtonTran.Click
         Me .RichTextBox2.Text  =  gPinyinService.ToPinyin( Me .RichTextBox1.Text,  Me .CheckBox1.Checked,  Me .CheckBox2.Checked)
     End Sub

Private   Sub  ButtonWord_Click( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  ButtonWord.Click
         Me .RichTextBox3.Text  =  gPinyinService.Words( Me .TextBoxPinyin.Text)
     End Sub

Private   Sub  Button1_Click( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  Button1.Click
         Me .RichTextBox3.Text  =  gPinyinService.GetWord( Me .TextBox1.Text).ToString
     End Sub
End Class

效果图:

下载方案:代码

汉字拼音的一个解决方法(初具使用价值)相关推荐

  1. 汉字拼音的一个解决方法

    Author: 水如烟 暂歇一下写那个区划方案. 平常中,经常用到汉字转拼音,比如批量生成姓名->拼音作为登录帐号. 这个方法只是简单的利用汉字拼音库.至于怎么找这个库,网上多有介绍.在最后提供 ...

  2. mysql数据库sql汉字数字排序_MYSQL数据库UTF8编码使用汉字拼音第一个字母排序的方法...

    MYSQL数据库UTF8编码使用汉字拼音第一个字母排序的方法 今天做网站时遇到了这样一个问题,有一个标签数据库,在管理标签时想按标签列的汉字拼音的第一个字母排序,这样人工查找的时候就很快可以看到: 于 ...

  3. iOS获取汉字拼音首字母的方法

    iOS获取汉字拼音首字母的方法 之前在项目中需要根据昵称的首字母进行排序,蹚了几个坑,今天记录一下.其实iOS系统封装的就有获取汉字首字母的方法,但不推荐,想看最优解的同学直接略过: var name ...

  4. Requirejs加载超时问题的一个解决方法:设置waitSeconds=0

    Requirejs加载超时问题的一个解决方法:设置waitSeconds=0 参考文章: (1)Requirejs加载超时问题的一个解决方法:设置waitSeconds=0 (2)https://ww ...

  5. mysql启动失败的一个解决方法

    mysql启动失败的一个解决方法 参考文章: (1)mysql启动失败的一个解决方法 (2)https://www.cnblogs.com/notfresh/p/mysqlStartError.htm ...

  6. python怎么识别拼音-python获取一组汉字拼音首字母的方法

    本文实例讲述了python获取一组汉字拼音首字母的方法.分享给大家供大家参考.具体实现方法如下: #!/usr/bin/env python # -*- coding: utf-8 -*- def m ...

  7. python汉字拼音查询_python获取一组汉字拼音首字母的方法

    本文实例讲述了python获取一组汉字拼音首字母的方法.分享给大家供大家参考.具体实现方法如下: #!/usr/bin/env python # -*- coding: utf-8 -*- def m ...

  8. mysql查询汉字拼音首字母的方法_MySQL查询汉字拼音首字母的方法

    下面为您介绍了MySQL查询汉字拼音首字母的方法,该方法极具实用价值,如果您之前遇到过类似方面的问题,不妨一看. MySQL查询汉字拼音首字母方法如下: 1.建立拼音首字母资料表 Sql代码: DRO ...

  9. python汉字转拼音首字母_python获取一组汉字拼音首字母的方法

    作者:不吃皮蛋 字体: 类型:转载 这篇文章主要介绍了python获取一组汉字拼音首字母的方法,涉及Python针对汉字操作的相关技巧,需要的朋友可以参考下 本文实例讲述了python获取一组汉字拼音 ...

最新文章

  1. ionic4中实现时间线
  2. Spring Cloud构建分布式电子商务平台:服务消费(基础)
  3. python基础(part15)--迭代
  4. 论文浅尝 - ACL2020 | IntKB: 一种交互式知识图谱补全框架
  5. 移动数字广告与互联网反欺诈蓝皮报告
  6. Redhat或者Centos手动安装Vim,
  7. python助教评分问题_【1414软工助教】单元测试 得分榜
  8. 连续子数组的最大和(基于动态规划)
  9. 在struts中实现验证码
  10. 【中国传媒大学】史上最全的《电视原理》笔记
  11. 详解 欧拉角与四元数
  12. 交叉熵和相对熵(KL散度)
  13. 2022备赛蓝桥杯给大家的建议与提醒和资料,值得认真看一下,多拿几十分
  14. 不同时区不同夏令时(夏时制)间转换
  15. m-TRP transmission for URLLC(draft)
  16. 关于电脑版/PC微信如何恢复聊天记录【实践可行】
  17. 跨越13开启14--猿猿感想
  18. 淘宝商品详情 API 返回值说明
  19. mysql中实现分类统计查询的步骤_在MySQL中如何进行分组统计查询
  20. Java白皮书关键词总结

热门文章

  1. 位置定位LocationManager
  2. AutoCAD .NET 二次开发实例:批量文本查找替换
  3. VxWorks6.7新建bootrom工程
  4. CSS中有哪几种方式能隐藏页面元素(8种)
  5. 四川汶川县今天又连发生地震!
  6. 人,羊,狼,菜过河问题的计算机编程实现的matlab程序,人狼羊菜渡河问题(含Matlab程序)...
  7. 关于自动化运维的那些事儿
  8. 微积分手机版与高清电子投影仪是“绝配”
  9. 哈佛结构冯·诺依曼结构
  10. CSDN博主排行榜上线!