|
本帖最后由 tanhong 于 2011-6-21 19:15 编辑
之前发过一个将拼音转换为无声调拼音函数,是通过数组方法实现(请参看之前所发实例),但在实际运行中发现调用要慢很多,将其做了一些改进,用循环比对方法,速度提高不少,发上来分享一下.
- '枚举无声调拼音输出常量
- Enum NotTonesPinyin
- NOTONESPINYIN = 1 '无声调拼音
- NUMTIONES= 2 '声调数字标识
- End Enum
- '---------------------------------------------------------------------------------------------------
- '功能:实现拼音的转换,如:bāo --> bao ,获得拼的声调:如:bāo 声调为:1
- '输入:strPy字符串变量,为有声调拼音,
- ' OutputType 可选参数,默认参数值: "1"无声调拼音, "2"声调数标识, "3"拼音和声调数字标识
- '输出:String字符串变量
- '调用:1、无 声 调 拼 音:=PinYinNumTones("bǎng") 结果为:bang
- ' 2、声 调 值:=PinYinNumTones("bǎng", NumTones) 结果为:3
- ' 3、无声调拼音及声调值:=PinYinNumTones("bǎng",NoTonesPinYin + NumTones) 结果为:bang3
- '编程:江羽 2011-6-16
- '----------------------------------------------------------------------------------------------------
- Public Function PinYinNumTones(ByVal strPy As String, _
- Optional OutputType As NotTonesPinyin = 1) As String
- Dim I, J As Integer '循环变量
- Dim strTones As String '声调数字标识
- Dim strPinyin As String '无声调拼音
- Const TONESCHR = "āáǎàēéěèīíǐìōóǒòūúǔùǖǘǚǜ"
- Const NOTTONES = "aeiouv"
- '初始变量值
- strTones = 0
- strPinyin = strPy
- '获得声调数字码及无声调拼音
- For I = 0 To 23: For J = 1 To Len(strPy)
- If Mid(TONESCHR, I + 1, 1) = Mid(strPy, J, 1) Then
- '将无音标拼音及声调数值分别赋值给变量
- strPinyin = Replace(strPy, Mid(strPy, J, 1), Mid(NOTTONES, I \ 4 + 1, 1))
- strTones = I Mod 4 + 1
- GoTo ChrOutput '替换完后跳出,以期提高速度
- End If
- Next: Next
- ChrOutput:
- '按要求输出结果
- Select Case OutputType
- Case 1 '替换不含声调拼音
- PinYinNumTones = strPinyin
- Case 2 '输出声调数字码
- PinYinNumTones = strTones
- Case 3 '以数字标识声调输出拼音
- PinYinNumTones = strPinyin & strTones
- End Select
- End Function
复制代码
|
|