具体比较复杂,vba
public function hztopy(hz as string, _
optional sep as string = "", _
optional notationtype as integer = -1, _
optional showinitialonly as boolean = false, _
optional showonlyonechar as boolean = false) as string
dim hp as hz2py
set hp = new hz2py '创建类
hp.seperator = sep
hp.initialonly = showinitialonly
hp.onlyonechar = showonlyonechar
hztopy = hp.getpinyin(hz)
hztopy = hp.adjustphoneticnotation(hztopy, notationtype)
set hp = nothing '释放类
end function
'类模块hz2py'***************************************************************************
'*
'* module: hztopy
'* update: 2011-09-23
'* author: tt.t
'*
'* description: 将中文字符串转换为拼音,就这些。原先这里写了太多废话,删了。
'*
'* theory: 原理依然是通过ifelanguage接口实现。
'* 唯一需要解释的是如何解决多音字正确注音的问题。
'* ifelanguage接口是能够正确返回很多多音字拼音的,但多音字的读音只有特定词汇中
'* 才能确认,因此在解析拼音时候不能把词拆成单字,否则多音字返回的拼音就很可能不对。
'* 之前版本中就是因为把词拆开获取拼音导致多音字拼音错误。
'* 这次的更新利用接口返回数据中标识每个拼音长度的数组实现了对返回拼音
'* 的按字拆分,无需再把词拆成字获取单个字的拼音,从而解决了多音字问题。
'* 需要说明的是,vb_morrslt结构就是ms文档中的morrslt结构,但是vba自定义结构
'* 无法实现不按4字节对齐,使得不得不修改morrslt的定义方式,能这样修改只能说运气不错,
'* 因为被修改的部分刚好获取拼音用不到。
'*
'*
'* histroy:
'* 2011-09-23
'* ● 重写主要代码,支持多音字,提高了运行效率。
'* ● 取拼音首字时,ao, ai, ei, ou, er作为首字而不是原来的第一个字母。
'* ● 为函数增加了注音方式选择,hàn可以显示为han或han4。
'* ● 函数的使用与之前版本兼容,将模块中函数代码和hz2py类代码覆盖之前版本即可实现升级,无需修改文档中的公式。
'* 2011-04-07
'* ● 更正cotaskmemfree传递参数错误,消除了win7等环境下崩溃。
'* 2007-04-03
'* ● 更正redim时vba数组默认起始值错误。
'* 2007-04-02
'* ● 最初版本,实现了由汉字获取拼音。
'*
'***************************************************************************
option explicit
private type guid
data1 as long
data2 as integer
data3 as integer
data4(0 to 7) as byte
end type
private type vb_morrslt
dwsize as long '4
pwchoutput as long '4
cchoutput as integer '2+(2),vba内存对齐闹得,折腾了好一阵才确认问题所在,唉
block1 as long '4
pchinputpos as long '4
pchoutputidxwdd as long '4
pchreadidxwdd as long '4
pamonorubypos as long '4
pwdd as long '4
cwdd as integer '2
pprivate as long '4
blkbuff as long '4
end type
private declare sub movememory lib "kernel32" alias "rtlmovememory" _
(destination as any, source as any, byval length as long)
private declare function clsidfromstring lib "ole32.dll" _
(byval lpszprogid as long, pclsid as guid) as long
private declare function cocreateinstance lib "ole32" ( _
rclsid as guid, byval punkouter as long, _
byval dwclscontext as long, riid as guid, _
byref ppv as long) as long
private declare function dispcallfunc lib "oleaut32" _
(byval pvinstance as long, byval ovft as long, _
byval cc as long, byval vtreturn as integer, _
byval cactuals as long, prgvt as integer, _
prgpvarg as long, pvargresult as variant) as long
private declare sub cotaskmemfree lib "ole32" (pv as long)
dim msime_guid as guid 'msime's guid
dim ifelanguage_guid as guid 'ifelanguage's guid
dim ifelanguage as long 'pointer to ifelanguage interface
dim pinyinarray() as string
dim hzlen as integer
dim pvseperator as string
dim pvuseseperator as boolean
dim pvinitialonly as boolean
dim pvonlyonechar as boolean
dim pvnonchnusesep as boolean
public function getpinyin(hzstr as string) as string
dim i as integer
dim py as string
dim ispy as boolean
getpinyin = ""
if ifelanguage = 0 then
getpinyin = "未发现运行环境,请安装微软拼音2.0以上版本!"
exit function
end if
if hzstr = "" then exit function
hzlen = len(hzstr)
call ifelanguage_getmorphresult(hzstr)
for i = 1 to hzlen
py = pinyinarray(i)
ispy = py <> ""
if not ispy then py = mid(hzstr, i, 1)
if pvinitialonly then py = getinitial(py)
if pvonlyonechar then py = vba.left(py, 1)
getpinyin = getpinyin & py & iif(ispy, pvseperator, "")
next i
if ispy and pvseperator <> "" then getpinyin = left(getpinyin, len(getpinyin) - 1)
end function
property get seperator() as string
seperator = pvseperator
end property
property let seperator(value as string)
pvseperator = value
end property
property get initialonly() as boolean
useseperator = pvinitialonly
end property
property let initialonly(value as boolean)
pvinitialonly = value
end property
property get onlyonechar() as boolean
useseperator = pvonlyonechar
end property
property let onlyonechar(value as boolean)
pvonlyonechar = value
end property
public function adjustphoneticnotation(py as string, byval pn as integer) as string
dim i as integer
dim c as string
if pn = -1 then
adjustphoneticnotation = py
exit function
else
for i = 1 to len(py)
c = vba.mid(py, i, 1)
select case asc(c)
case vba.asc("ā") to vba.asc("à")
c = "a" & iif(pn = 0, "", (vba.asc(c) - vba.asc("ā") + 1))
case vba.asc("ē") to vba.asc("è")
c = "e" & iif(pn = 0, "", (vba.asc(c) - vba.asc("ē") + 1))
case vba.asc("ī") to vba.asc("ì")
c = "i" & iif(pn = 0, "", (vba.asc(c) - vba.asc("ī") + 1))
case vba.asc("ō") to vba.asc("ò")
c = "o" & iif(pn = 0, "", (vba.asc(c) - vba.asc("ō") + 1))
case vba.asc("ū") to vba.asc("ù")
c = "u" & iif(pn = 0, "", (vba.asc(c) - vba.asc("ū") + 1))
case vba.asc("ǖ") to vba.asc("ǜ")
c = "u" & iif(pn = 0, "", (vba.asc(c) - vba.asc("ǖ") + 1))
case vba.asc("ü")
c = "u"
case vba.asc("ɡ")
c = "g"
end select
adjustphoneticnotation = adjustphoneticnotation & c
next i
end if
end function
private function getinitial(py as string) as string
getinitial = vba.mid(py, 1, 2)
select case adjustphoneticnotation(getinitial, 0)
case "ch", "sh", "zh", "ao", "ai", "ei", "ou", "er"
case else
getinitial = vba.left(getinitial, 1)
end select
end function
private function ifelanguage_getmorphresult(hzstr as string) as string
dim ret as variant
dim pargs(0 to 5) as long
dim vt(0 to 5) as integer
dim args(0 to 5) as long
dim resultptr as long
dim tinym as vb_morrslt
dim py() as byte
dim i as integer
dim j as integer
dim pinyinindexarray() as integer
ifelanguage_getmorphresult = ""
if ifelanguage = 0 then exit function
args(0) = &h30000
args(1) = &h40000100
args(2) = len(hzstr)
args(3) = strptr(hzstr)
args(4) = 0
args(5) = varptr(resultptr)
for i = 0 to 5
vt(i) = vblong
pargs(i) = varptr(args(i)) - 8
next
call dispcallfunc(ifelanguage, 20, 4, vblong, 6, vt(0), pargs(0), ret)
call movememory(tinym, byval resultptr, len(tinym))
redim pinyinindexarray(0 to hzlen - 1)
redim pinyinarray(1 to hzlen)
if tinym.cchoutput > 0 then
redim py(0 to tinym.cchoutput * 2 - 1)
call movememory(py(0), byval tinym.pwchoutput, tinym.cchoutput * 2)
ifelanguage_getmorphresult = py
call movememory(pinyinindexarray(0), byval tinym.pamonorubypos + 2, hzlen * 2)
j = 0
for i = 0 to hzlen - 1
pinyinarray(i + 1) = vba.mid(ifelanguage_getmorphresult, j + 1, pinyinindexarray(i) - j)
j = pinyinindexarray(i)
next i
end if
call cotaskmemfree(byval resultptr)
end function
private sub ifelanguage_open()
dim ret as variant
call dispcallfunc(ifelanguage, 4, 4, vblong, 0, 0, 0, ret)
call dispcallfunc(ifelanguage, 12, 4, vblong, 0, 0, 0, ret)
end sub
private sub ifelanguage_close()
dim ret as variant
if ifelanguage = 0 then exit sub
call dispcallfunc(ifelanguage, 8, 4, vblong, 0, 0, 0, ret)
call dispcallfunc(ifelanguage, 16, 4, vblong, 0, 0, 0, ret)
end sub
private function generateguid()
dim rlt as long
'msime.china guid = "{e4288337-873b-11d1-baa0-00aa00bbb8c0}"
rlt = clsidfromstring(strptr("msime.china"), msime_guid)
'ifelanguage guid = "{019f7152-e6db-11d0-83c3-00c04fddb82e}"
with ifelanguage_guid
.data1 = &h19f7152
.data2 = &he6db
.data3 = &h11d0
.data4(0) = &h83
.data4(1) = &hc3
.data4(2) = &h0
.data4(3) = &hc0
.data4(4) = &h4f
.data4(5) = &hdd
.data4(6) = &hb8
.data4(7) = &h2e
end with
generateguid = rlt = 0
end function
private sub class_initialize()
ifelanguage = 0
pvseperator = ""
generateguid
if cocreateinstance(msime_guid, 0, 1, ifelanguage_guid, ifelanguage) = 0 then call ifelanguage_open
end sub
private sub class_terminate()
if ifelanguage <> 0 then call ifelanguage_close
end sub