第三種,將漢字轉換為帶聲調的拼音
如: 將 我愛精品夢露 轉換為 jīngpǐnmènglù
原理很簡單,先造表(佩服這個牛人),然後搜尋取代即可
帶聲調的轉換.vbs
a1 = "的de5,一yi1,國guo2,在z....
.....
a68 = "煺tui4,柝tuo4,膃wa4,...
b1 = "的dí/dì/de,一yī,....
....
b68 = "煺tuì,柝tuò,膃......
cb = b1 & b2 &..... b67 & b68
ca = a1 & a2 & .....a67 & a68
Function pinyin(str, ab)
If Asc(str) > 0 Then '非中文,不轉換
pinyin = str
Exit Function
End If
If ab = "a" Then
p = InStr(ca, str)
If p > 0 Then
p2 = InStr(p, ca, ",")
pinyin = Mid(ca, p+1, p2 - p - 1)
End If
ElseIf ab = "b" Then
p = InStr(cb, str)
If p > 0 Then
p2 = InStr(p, cb, ",")
pinyin = Mid(cb, p+1, p2 - p - 1)
End If
End If
End Function
'---------------------- 將字元串轉換成拼音 --------------------------
Function Str2PY(str)
Str2PY = ""
For i = 1 To len(str)
Str2PY = Str2PY & pinyin(Mid(str, i, 1), "b")
Next
End Function
'----------------------- 測試 -----------------------
temp = InputBox("請輸入需要轉換成拼音的字元串:", "InputBox", "我是測試字元串,哈哈。…… English char is ok too.")
If Len(temp) > 0 Then
msgbox Str2PY(CStr(temp)), ,"Result"
End If
上面的程式碼都是示例程式碼,請下載附件,直接執行vbs就可看到效果
優點: 只要看懂了上面的原理,可以很容易移植到其它程序上
缺點: 能轉換的漢字有限,最強的是最後一個,收錄的漢字最多。前兩個只能轉換GB2312中的大部分漢字(基本夠用了,哈哈)
|