我將此方法移植到VB.NET上了,請看源碼(二)

來源:互聯網
上載者:User
#Region "由拼音到拼音"
    Public Shared Function GetPyToPy(ByVal PYStr As String, _
                                     Optional ByVal IsComp As Boolean = False) As String
        If PYStr.Length = 0 Then Exit Function

        mIsCompelled = IsComp

        Dim toneSite As Integer
        Dim toneValue As Integer
        Dim toneChar As Char

        toneSite = GetToneSite(PYStr)
        If toneSite = -1 Then
            Return PYStr
        ElseIf toneSite < -1 Then
            Exit Function
        End If

        toneValue = GetToneValue(PYStr, toneSite)
        If toneValue = -1 Then Exit Function

        If mIsCompelled Then
            toneChar = GetToneChar(PYStr, toneSite - 1)
        Else
            toneChar = GetToneChar(PYStr)
        End If
        If Not isPyChar(toneChar) Then Exit Function
        Return Conversion(PYStr, toneSite, toneValue, toneChar)

    End Function
    '返回標明聲調數值的位置
    Private Shared Function GetToneSite(ByVal pyStr As String) As Integer
        If pyStr.Length = 0 Then Exit Function
        Dim i As Integer, j As Integer = 0
        '檢查字串中有幾個數字
        For i = 0 To pyStr.Length - 1
            If Char.IsNumber(pyStr.Chars(i)) Then
                j += 1
            End If
        Next
        If j = 0 Then       '沒有數字,即沒有標明聲調的數值
            Return -1
        ElseIf j = 1 Then   '有一個數字,合法
            For i = 0 To pyStr.Length - 1
                If Char.IsNumber(pyStr.Chars(i)) Then
                    Return i
                End If
            Next
        Else                '有多個數字,不合法,返回其相反數
            Return -j
        End If
    End Function
    '返回聲調是幾聲
    Private Shared Function GetToneValue(ByVal pyStr As String, _
                                         ByVal Site As Integer) As Integer
        If pyStr.Length = 0 Then Exit Function
        If Site >= 0 Then
            Dim value As Char = pyStr.Chars(Site)
            Dim num As Integer
            If Char.IsNumber(value) Then
                num = CType(value.ToString, Integer)
                If num >= 0 And num <= 4 Then
                    Return num
                Else
                    Return -1
                End If
            Else
                Return -1
            End If
        End If
    End Function
    '返回要標聲調的字母
    Private Shared Function GetToneChar(ByVal pyStr As String, _
                                     ByVal Site As Integer) As Char
        If pyStr.Length = 0 Then Exit Function
        If Site >= 0 Then
            Dim value As Char = pyStr.Chars(Site)
            Return value
        End If
    End Function
    Private Shared Function GetToneChar(ByVal pyStr As String) As Char
        If pyStr.Length = 0 Then Exit Function
        Dim Chr As Char

        If pyStr.IndexOf(a(0)) >= 0 Then
            Chr = pyStr.Chars(pyStr.IndexOf(a(0)))
        ElseIf pyStr.IndexOf(o(0)) >= 0 Then
            Chr = pyStr.Chars(pyStr.IndexOf(o(0)))
        ElseIf pyStr.IndexOf(e(0)) >= 0 Then
            Chr = pyStr.Chars(pyStr.IndexOf(e(0)))
        ElseIf pyStr.IndexOf(i(0)) >= 0 Then
            Chr = pyStr.Chars(pyStr.IndexOf(i(0)))
        ElseIf pyStr.IndexOf(u(0)) >= 0 Then
            Chr = pyStr.Chars(pyStr.IndexOf(u(0)))
        ElseIf pyStr.IndexOf(v(0)) >= 0 Then
            Chr = pyStr.Chars(pyStr.IndexOf(v(0)))
        End If

        Return Chr
    End Function
    '判斷是否為合法的拼音字元
    Private Shared Function isPyChar(ByVal chr As Char) As Boolean
        Select Case chr
            Case "a"c
                Return True
            Case "o"c
                Return True
            Case "e"c
                Return True
            Case "i"c
                Return True
            Case "u"c
                Return True
            Case "v"c
                Return True
            Case Else
                Return False
        End Select
    End Function
    '轉換
    Private Shared Function Conversion(ByVal pyStr As String, _
                                       ByVal toneSite As Integer, _
                                       ByVal toneValue As Integer, _
                                       ByVal toneChar As Char) As String
        If toneValue >= 1 And toneValue <= 4 Then
            Select Case toneChar
                Case "a"
                    pyStr = pyStr.Replace(toneChar, a(toneValue).Chars(0))
                Case "o"
                    pyStr = pyStr.Replace(toneChar, o(toneValue).Chars(0))
                Case "e"
                    pyStr = pyStr.Replace(toneChar, e(toneValue).Chars(0))
                Case "i"
                    pyStr = pyStr.Replace(toneChar, i(toneValue).Chars(0))
                Case "u"
                    pyStr = pyStr.Replace(toneChar, u(toneValue).Chars(0))
                Case "v"
                    pyStr = pyStr.Replace(toneChar, v(toneValue).Chars(0))
            End Select
            pyStr = pyStr.Replace(toneValue.ToString, "")
            Return pyStr
        End If
    End Function
#End Region

#Region "由漢字到拼音"
    Public Shared Function GetHzToPy(ByVal HzStr As String, _
                                 Optional ByVal ToneValue As Integer = 0, _
                                 Optional ByVal IsComp As Boolean = False) As String
        'ByVal toneValue As Integer, _
        If HzStr.Length = 0 Then Exit Function
        If PyList.Count = 0 Then LoadPyChars()
        mIsCompelled = IsComp
        If mIsCompelled Then
        Else
            Dim tmpStr As String = ""
            Dim i As Integer
            Dim num As Integer
            For i = 1 To HzStr.Length
                num = Asc(HzStr)
                Debug.WriteLine(num.ToString)
                tmpStr = GetPyChars(num)
                tmpStr = GetPyToPy(tmpStr & ToneValue)
                Return tmpStr
            Next
        End If
    End Function

    Private Shared Function GetPyChars(ByVal num As Integer) As String
        If num > 0 And num < 160 Then
            Return Chr(num)
        Else
            If num < -20319 Or num > -10247 Then
                Return ""
            Else
                Dim i, value As Integer
                For i = PyList.Count - 1 To 0 Step -1
                    If PyList.GetByIndex(i) <= num Then Exit For
                Next
                'value = CType(PyList.GetByIndex(i), Integer)
                Return PyList.GetKey(i).ToString
            End If
        End If
    End Function
#End Region

End Class

相關文章

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。