惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
J
Java Code Geeks
The GitHub Blog
The GitHub Blog
博客园_首页
U
Unit 42
人人都是产品经理
人人都是产品经理
Engineering at Meta
Engineering at Meta
IT之家
IT之家
G
Google Developers Blog
L
LangChain Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
爱范儿
爱范儿
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
Jina AI
Jina AI
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
小众软件
小众软件
H
Help Net Security

博客园 - 野猪&翔帅

安装Windows2003 sp1/sp2提示产品密钥无效的解决方法[转] [疑]关于oracle导出数据时遇到的4031问题 - 野猪&翔帅 - 博客园 [转] 解析oracle的ROWNUM -- 作者: chen_liang WEB网站无法打开某种格式资源的解决办法 [求助]关于服务器之间的文件拷贝问题,没有头绪,希望大家指点一二 【转】ORACLE to_char函数详解 未在本地计算机上注册“OraOLEDB.Oracle.1”提供程序 解决方法之二 SQL--JOIN之完全用法 错误类型:ADODB.Recordset (0x800A0BB9)参数类型不正确,或不在可以接受的范围之内,或与其他参数冲突。 关于利用模板生成静态页面的时候出现乱码的问题 - 野猪&翔帅 - 博客园 C#中使用二维数组时,报错:内索引数错误 【转】Aspx页面慎用 Form.Enctype 属性 [转]设置textBox的响应按钮事件的js的两种方法 在ORACLE中,用SQL语句搜索上周的记录 【转】javascript实现图片的连续滚动 身份证明检索失败,如何解决? 『转』Oracle 函数大全 “当前上下文中不存在名称”解决 动态创建GridView的列(第二部分)【转】
【转自CSDN】汉字拼音的一个解决方法(初具使用价值)
野猪&翔帅 · 2007-02-13 · via 博客园 - 野猪&翔帅

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 PropertySub New()
        
End Sub''' <param name="tone">声调</param>
        ''' <param name="spellinput">拼音输入码</param>
        ''' <param name="spellexpress">拼音</param>
        Sub New(ByVal tone As StringByVal spellinput As StringByVal 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 ObjectAs Boolean
            
Return Me.SpellExpress.Equals(spellexpress)
        
End FunctionPublic Overrides Function ToString() As String
            
Return String.Format("拼音码 {0,-6} 声调 {1} 拼音 {2}"Me.SpellInput, Me.Tone, Me.SpellExpress)
        
End Function
    
End ClassEnd 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 PropertyPublic ReadOnly Property Code() As String
            
Get
                
Return Common.Code(gValue)
            
End Get
        
End PropertyPrivate gSpellList As New Dictionary(Of String, SpellInformation)''' <summary>
        ''' 拼音集
        ''' </summary>
        Public ReadOnly Property Spells() As SpellInformation()
            
Get
                
Dim tmp(gSpellList.Count - 1As 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 PropertyPublic Sub AddSpell(ByVal spell As SpellInformation)
            
If String.IsNullOrEmpty(spell.SpellExpress) Then Exit SubIf Me.gSpellList.ContainsKey(spell.SpellExpress) Then Exit SubMe.gSpellList.Add(spell.SpellExpress, spell)
        
End SubPublic Sub AddSpell(ByVal tone As StringByVal spellinput As StringByVal spellexpress As String)
            
Dim tmp As New SpellInformation(tone, spellinput, spellexpress)

            AddSpell(tmp)

End SubPublic ReadOnly Property DefaultSpell() As SpellInformation
            
Get
                
If Me.gSpellList.Count = 0 Then Return Nothing
                
Return Me.Spells(0)
            
End Get
        
End PropertyPublic ReadOnly Property DefaultSpellExpress() As String
            
Get
                
If DefaultSpell Is Nothing Then Return Nothing
                
Return Me.DefaultSpell.SpellExpress
            
End Get
        
End PropertyPublic ReadOnly Property DefaultSpellInput() As String
            
Get
                
If DefaultSpell Is Nothing Then Return Nothing
                
Return Me.DefaultSpell.SpellInput
            
End Get
        
End PropertyPublic ReadOnly Property DefaultTone() As String
            
Get
                
If DefaultSpell Is Nothing Then Return Nothing
                
Return Me.DefaultSpell.Tone
            
End Get
        
End PropertyPublic ReadOnly Property AllSpellExpress() As String
            
Get
                
If Me.gSpellList.Count = 0 Then Return NothingIf Not Me.IsMutiSpell Then Return Me.DefaultSpellExpressDim tmp(gSpellList.Count - 1As String
                
For i As Integer = 0 To gSpellList.Count - 1
                    tmp(i) 
= Me.Spells(i).SpellExpress
                
NextReturn String.Join(" ", tmp)
            
End Get
        
End PropertyPublic ReadOnly Property AllSpellInput() As String
            
Get
                
If Me.gSpellList.Count = 0 Then Return NothingIf Not Me.IsMutiSpell Then Return Me.DefaultSpellInputDim tmp(gSpellList.Count - 1As String
                
For i As Integer = 0 To gSpellList.Count - 1
                    tmp(i) 
= Me.Spells(i).SpellInput
                
NextReturn String.Join(" ", tmp)
            
End Get
        
End PropertyPublic ReadOnly Property AllTone() As String
            
Get
                
If Me.gSpellList.Count = 0 Then Return NothingIf Not Me.IsMutiSpell Then Return Me.DefaultToneDim tmp(gSpellList.Count - 1As String
                
For i As Integer = 0 To gSpellList.Count - 1
                    tmp(i) 
= Me.Spells(i).Tone
                
NextReturn String.Join(" ", tmp)
            
End Get
        
End PropertyPublic 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 ClassEnd Namespace

Imports System.IO
Imports System.Text.RegularExpressionsNamespace 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 StringAs Word
            
Dim mRow As dsPinYin.PinYinRow = Me.gDataSet.PinYin.FindBy代码(GetCode(word))Return RowConverter(mRow)
        
End FunctionPrivate Function RowConverter(ByVal row As dsPinYin.PinYinRow) As Word
            
If row Is Nothing Then Return NothingDim 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))
            
NextReturn mWord
        
End FunctionPrivate 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 IfFor Each spell As SpellInformation In word.Spells
                mWord.AddSpell(spell)
            
NextDim mRow As dsPinYin.PinYinRow = Me.gDataSet.PinYin.FindBy代码(mWord.Code)
            
With mRow
                .拼音码 
= mWord.AllSpellInput
                .拼音 
= mWord.AllSpellExpress
                .声调 
= mWord.AllTone
                .单音 
= Not mWord.IsMutiSpell
            
End WithEnd SubPublic 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 SubDim 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).TrimDim 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 StringByVal isgetfirst As BooleanByVal forInput As BooleanAs String
            
Dim mBuilder As New Text.StringBuilderFor Each s As Char In line.ToCharArray
                
If Common.IsSingleWord(s) Then
                    mBuilder.Append(GetPinyin(s, isgetfirst, forInput))
                
Else
                    mBuilder.Append(s)
                
End If
            
NextReturn mBuilder.ToString
        
End FunctionPrivate Function GetPinyin(ByVal word As StringByVal isgetfirst As BooleanByVal forInput As BooleanAs String
            
Dim mResult As StringDim mWord As Word = GetWord(word)If isgetfirst Or Not mWord.IsMutiSpell Then
                
If forInput Then
                    mResult 
= mWord.DefaultSpellInput
                
Else
                    mResult 
= mWord.DefaultSpellExpress
                
End IfElse
                
If forInput ThenDim tmpList(-1As StringFor 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
                    
NextIf 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 IfEnd IfReturn mResult
        
End Function''' <summary>
        ''' 按拼音查字
        ''' </summary>
        ''' <param name="pinyin">拼音</param>
        Public Function WordArray(ByVal pinyin As StringAs String()
            
Dim mRows As dsPinYin.PinYinRow() = CType(Me.gDataSet.PinYin.Select(String.Format("拼音码 LIKE '%{0}%'", pinyin)), dsPinYin.PinYinRow())
            
Dim mResult(-1As 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 StringAs String
            
Return String.Concat(WordArray(pinyin))
        
End FunctionPublic Function GetCode(ByVal word As StringAs String
            
Return Common.Code(word)
        
End FunctionEnd Class
End Namespace

调用的代码

Public Class Form1
    
Dim gPinyinService As New LzmTW.Businness.PinYin.PYServicePrivate Sub ButtonLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonLoad.Click
        gPinyinService.Load()
        
Me.DataGridView1.DataSource = gPinyinService.PinYinTable
    
End SubPrivate 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 SubPrivate 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 SubPrivate 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

效果图:

下载方案:代码

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1330615