汉字转拼音
不推荐
libpinyin
编译复杂,依赖Glib2pinyin4cpp
依赖QT
强烈推荐 go-pinyin
hanzi2pinyin.go
导出函数toPinyin
gfree
package mainimport "C"
import ("fmt""encoding/json""github.com/mozillazg/go-pinyin""unicode/utf8""unsafe"
)/*
#include <stdlib.h>
*/
import "C"//export toPinyin
func toPinyin(input *C.char) *C.char {// 转换C字符串为Go字符串goInput := C.GoString(input)if !utf8.ValidString(goInput) {return C.CString("err:input need UTF-8 data")}args := pinyin.NewArgs()result := pinyin.Pinyin(goInput, args) // 默认无音调jsonBytes, err := json.Marshal(result)if err != nil{return C.CString("err:to json error")}jsonStr := string(jsonBytes)pinyinResult := C.CString(jsonStr)return pinyinResult
}//export gfree
func gfree(addr unsafe.Pointer) {if addr != nil{C.free(addr)addr = nil}}func main() {hans := "汉字转拼音123中国"args := pinyin.NewArgs()args.Heteronym=true //支持多音字args.Fallback = func(r rune, a pinyin.Args) []string {if unicode.IsDigit(r) || unicode.IsLetter(r) {return []string{string(r)} // 数字/字母返回原字符}return nil // 其他字符(如标点)可忽略或自定义}result := pinyin.Pinyin(hans, args) // 默认无音调var results []stringfor _, syllables := range result {if len(syllables) > 0 {results = append(results, syllables[0])}}fmt.Println(result) // 输出: [[han] [zi] [zhuan] [pin] [yin] [1] [2] [3] [zhong] [guo]]fmt.Println(results) // 输出: [han zi zhuan pin yin 1 2 3 zhong guo]fmt.Println(strings.Join(results, "")) // 输出: hanzizhuanpinyin123zhongguo
}
编译Dll 并且压缩(upx) dll
#mt dll
#-s:移除符号表(减少10%~20%)
#-w:移除DWARF调试信息(再减10%~30%)
go build -ldflags "-s -w" -buildmode=c-shared -o hanzi2pinyin.dll .\hanzi2pinyi.go#压缩dll
#--best:最高压缩率(减少50%~70%)
#--lzma:LZMA算法(压缩率更高,但稍慢)
upx --best --lzma hanzi2pinyin.dll -o hanzi2pinyin_compress.dll
C++汉字转拼音
#pragma once
#include <windows.h>
#include <Shlwapi.h>
#include "xlog/xlog.h"
#include "hv/http_content.h"
#include "defer.h"
typedef char* (*PtoPinin)(char* input);
typedef void (*Pgfree)(void* input);class CHanzi2PinyinMgr {
public:static CHanzi2PinyinMgr& Instance(){static CHanzi2PinyinMgr sInstance;return sInstance;}void Init() {if (m_hM == nullptr){char szModule[2048] = "";GetModuleFileNameA(NULL, szModule, 2048);PathRemoveFileSpecA(szModule);PathAppendA(szModule, "hanzi2pinyin.dll");m_hM = ::LoadLibraryA(szModule);DWORD dwErrcode = GetLastError();XLOGI("CHanzi2PinyinMgr init path[%s] hM[%I64d] errcode:%d", szModule, m_hM, dwErrcode);if (m_hM){m_toPinyin = (PtoPinyin)GetProcAddress(m_hM, "toPinyin");m_gfree = (Pgfree)GetProcAddress(m_hM, "gfree");XLOGI("CHanzi2PinyinMgr init m_toPinyin[%I64d],m_gfree[%I64d]", m_toPinyin, m_gfree);}}}bool toPinyin(const std::string& sword, std::vector<std::string> &pys){pys.clear();if (m_toPinyin == nullptr || m_gfree == nullptr) return false;std::string u8words = A2UTF8STR(sword);//返回jsonchar *jpy = m_toPinyin((char*)u8words.c_str());defer(if (jpy) m_gfree(jpy););XLOGI("toPinyin word:%s,py:%s", sword.c_str(), jpy ? jpy : "");if (jpy){try{hv::Json jparse;hv::Json jroot = jparse.parse(jpy);for (int i = 0;i < jroot.size();++i){hv::Json jitem = jroot[i];if(jitem.is_array() && jitem.size() > 0 && jitem[0].is_string()){pys.push_back(UTF82ASTR(jitem[0]));}}return true;}catch (const nlohmann::detail::exception& e){XLOGW("toPinyin exception word[%s],%s", sword.c_str(), e.what());}return false;}return false;}
private:CHanzi2PinyinMgr(){m_hM = nullptr;m_toPinyin = nullptr;m_gfree = nullptr;}private:HMODULE m_hM;PtoPinyin m_toPinyin;Pgfree m_gfree;
};
demo
CHanzi2PinyinMgr::Instance().Init();
CHanzi2PinyinMgr::Instance().toPinyin("中国人")