Created
December 27, 2023 12:26
-
-
Save valuex/b707b5fdc154578bfe1b5fbd8056fd50 to your computer and use it in GitHub Desktop.
AHK 汉字转拼音
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; https://www.autohotkey.com/boards/viewtopic.php?t=1629 | |
zh2py(str) | |
{ | |
; 从 php 转换而来的 (http://www.sjyhome.com/php/201311170606.html) | |
; 根据汉字区位表,(http://www.mytju.com/classcode/tools/QuWeiMa_FullList.asp) | |
; 我们可以看到从16-55区之间是按拼音字母排序的,所以我们只需要判断某个汉字的区位码就可以得知它的拼音首字母. | |
; 区位表第一部份,按拼音字母排序的. | |
; 16区-55区 | |
/* | |
'A'=>0xB0A1, 'B'=>0xB0C5, 'C'=>0xB2C1, 'D'=>0xB4EE, 'E'=>0xB6EA, 'F'=>0xB7A2, 'G'=>0xB8C1,'H'=>0xB9FE, | |
'J'=>0xBBF7, 'K'=>0xBFA6, 'L'=>0xC0AC, 'M'=>0xC2E8, 'N'=>0xC4C3, 'O'=>0xC5B6, 'P'=>0xC5BE,'Q'=>0xC6DA, | |
'R'=>0xC8BB, 'S'=>0xC8F6, 'T'=>0xCBFA, 'W'=>0xCDDA, 'X'=>0xCEF4, 'Y'=>0xD1B9, 'Z'=>0xD4D1 | |
*/ | |
static FirstTable := [ 0xB0C5, 0xB2C1, 0xB4EE, 0xB6EA, 0xB7A2, 0xB8C1, 0xB9FE, 0xBBF7, 0xBFA6, 0xC0AC, 0xC2E8 | |
, 0xC4C3, 0xC5B6, 0xC5BE, 0xC6DA, 0xC8BB, 0xC8F6, 0xCBFA, 0xCDDA, 0xCEF4, 0xD1B9, 0xD4D1, 0xD7FA ] | |
static FirstLetter := StrSplit("ABCDEFGHJKLMNOPQRSTWXYZ") | |
; 区位表第二部份,不规则的,下面的字母是每个区里面对应字的拼音首字母.从网上查询整理出来的,可能会有部份错误. | |
; 56区-87区 | |
static SecondTable := [ StrSpl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JBTKQBXXJJLJCHCZDBZJDCZJDCPRNPQCJPFCZLCLZXZDMXMPHJSGZ") | |
, StrSplit("GSZZQLYLWTJPFSYASMCJBTZYYCWMYTZSJJLJCQLWZMALBXYFBPNLSFHTGJWEJJXXGLLJSTGSHJQLZFKCGNNNSZFDEQFHBS") | |
, StrSplit("AQTGYLBXMMYGSZLDYDQMJJRGBJTKGDHGKBLQKBDMBYLXWCXYTTYBKMRTJZXQJBHLMHMJJZMQASLDCYXYQDLQCAFYWYXQHZ") ] | |
static var := Buffer(2) | |
; 如果不包含中文字符,则直接返回原字符 | |
if !RegExMatch(str, "[^\x{00}-\x{ff}]") | |
Return str | |
Loop Parse str | |
{ | |
StrPut(A_LoopField, var, "CP936") | |
H := NumGet(var, 0, "UChar") | |
L := NumGet(var, 1, "UChar") | |
; 字符集非法 | |
if (H < 0xB0 || L < 0xA1 || H > 0xF7 || L = 0xFF) | |
{ | |
newStr .= A_LoopField | |
Continue | |
} | |
if (H < 0xD8) || (H >= 0xB0 && H <=0xD7) ; 查询文字在一级汉字区(16-55) | |
{ | |
W := (H << 8) | L | |
For key, value in FirstTable | |
{ | |
if (W < value) | |
{ | |
newStr .= FirstLetter[key] | |
Break | |
} | |
} | |
} | |
else ; if (H >= 0xD8 && H <= 0xF7) ; 查询中文在二级汉字区(56-87) | |
newStr .= SecondTable[ H - 0xD8 + 1 ][ L - 0xA1 + 1 ] | |
} | |
Return newStr | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment