首页 > 编程 > .NET > 正文

我将此方法移植到VB.NET上了,请看源码(二)

2024-07-10 13:12:58
字体:
来源:转载
供稿:网友
#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
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表