当前位置: 首页 > news >正文

汉字转拼音

不推荐

  1. libpinyin 编译复杂,依赖Glib2
  2. pinyin4cpp 依赖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("中国人")
http://www.dtcms.com/a/296536.html

相关文章:

  • Kiro AI是如何帮我实现发布 harpoon 项目
  • 如何使用 php-vulnerability-hunter
  • 中国网专访百胜软件董事长兼CEO黄飞:中台助力新时代下零售行业的探索
  • go下载包
  • go语言基础教程:【1】基础语法:变量
  • Verilog 提取信号的上升沿或者下降沿
  • Python中常用标准库(时间库、随机库、正则表达式)
  • shell 正则表达式
  • Golang 语言中的指针介绍
  • 高版本Android跨应用广播通信实例
  • 40、鸿蒙Harmony Next开发:UI场景化-组件截图(ComponentSnapshot)
  • C++第一节课入门
  • Qt 元对象系统(Meta-Object System)解析
  • 前端安全问题怎么解决
  • 企业资产管理智能化:IT运维如何借力数字化管理提效避坑?
  • 配置DNS服务的正反向解析
  • 详解FreeRTOS开发过程(六)-- 队列
  • ESP32- 项目应用1 智能手表之更新时间 #3
  • Linux系统常用命令
  • 浅析飞算JavaAI “撤回需求” 功能:让需求管理更灵活
  • 【C++】类和对象(中)
  • 【三桥君】Windows系统如何临时关闭“Windows安全中心实时保护”?解析Win10和Win11关闭方法
  • C++中std::string和std::string_view使用详解和示例
  • Lua(字符串)
  • 生成式人工智能展望报告-欧盟-03-经济影响
  • pyautogui 置信度问题
  • 拖拽同时支持Y轴滚动
  • 开立医疗2026年校园招聘
  • openbmc 日志系统继续分析
  • 行为型模式-协作与交互机制