













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 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 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 - 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 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 String, ByVal spellinput As String, ByVal spellexpress As String)
Dim tmp As New SpellInformation(tone, spellinput, spellexpress)
AddSpell(tmp)
End SubPublic ReadOnly Property DefaultSpell() As SpellInformationImports 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 String) As 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()RowUpdate(mWordInformation)
End Sub''' <summary>调用的代码
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
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。