Golang结合OpenCC实现高效的中文简体繁体互转

之前在页面中使用简体转繁体的功能一般是使用JS对页面进行字典替换,这样可以解决大部分问题,但要进行精确的转换,才发现简繁体的转换是个复杂的过程。幸好有个非常棒的繁体转简体开源项目OpenCC可以很好的工作。

OpenCC

OpenCC 特性

  • 嚴格區分「一簡對多繁」和「一簡對多異」。
  • 完全兼容異體字,可以實現動態替換。
  • 嚴格審校一簡對多繁詞條,原則爲「能分則不合」。
  • 支持中國大陸、臺灣、香港異體字和地區習慣用詞轉換,如「裏」「裡」、「鼠標」「滑鼠」。
  • 詞庫和函數庫完全分離,可以自由修改、導入、擴展。
  • 支持C、C++、Python、PHP、Java、Ruby、Node.js and Android。
  • 兼容Windows、Linux、Mac平臺。

OpenCC 的 Golang 库

目前有较好的几个 golang 库推荐

处于性能考虑,个人使用第一个项目,下面是其使用的例子。

我在Mac 里安装OpenCC

1
2
brew install opencc
go get github.com/stevenyao/go-opencc

OpenCC 预设置

  • s2t.json Simplified Chinese to Traditional Chinese 簡體到繁體
  • t2s.json Traditional Chinese to Simplified Chinese 繁體到簡體
  • s2tw.json Simplified Chinese to Traditional Chinese (Taiwan Standard) 簡體到臺灣正體
  • tw2s.json Traditional Chinese (Taiwan Standard) to Simplified Chinese 臺灣正體到簡體
  • s2hk.json Simplified Chinese to Traditional Chinese (Hong Kong Standard) 簡體到香港繁體(香港小學學習字詞表標準)
  • hk2s.json Traditional Chinese (Hong Kong Standard) to Simplified Chinese 香港繁體(香港小學學習字詞表標準)到簡體
  • s2twp.json Simplified Chinese to Traditional Chinese (Taiwan Standard) with Taiwanese idiom 簡體到繁體(臺灣正體標準)並轉換爲臺灣常用詞彙
  • tw2sp.json Traditional Chinese (Taiwan Standard) to Simplified Chinese with Mainland Chinese idiom 繁體(臺灣正體標準)到簡體並轉換爲中國大陸常用詞彙
  • t2tw.json Traditional Chinese (OpenCC Standard) to Taiwan Standard 繁體(OpenCC 標準)到臺灣正體
  • t2hk.json Traditional Chinese (OpenCC Standard) to Hong Kong Standard 繁體(OpenCC 標準)到香港繁體(香港小學學習字詞表標準)

具体实例

go-opencc 实现简体转繁体示例:

Go:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package main

import (
	"fmt"
	"github.com/stevenyao/go-opencc"
)

const (
	input = "中国鼠标软件打印机后台湾"
	config_s2t = "/usr/local/Cellar/opencc/1.0.5/share/opencc/s2t.json"
)

func main() {
	fmt.Println("Test Converter class:")
	c := opencc.NewConverter(config_s2t)
	defer c.Close()
	output := c.Convert(input)
	fmt.Println(output)
}

运行后输出:

1
2
Test Converter class:
中國鼠標軟件打印機後臺灣

opencc 源码安装

在 ubuntu/debian 使用 apt-get install opencc 安装,当运行 go get github.com/stevenyao/go-opencc 时可能会遇到下面的错误:

1
2
3
4
5
# github.com/stevenyao/go-opencc
opencc.c:5:10: fatal error: opencc/opencc.h: No such file or directory
 #include "opencc/opencc.h"
          ^~~~~~~~~~~~~~~~~
compilation terminated.

可以先卸载 apt-get remove opencc 再用源码安装

1
2
3
4
5
6
7
git clone https://github.com/BYVoid/OpenCC.git
apt-get install cmake
apt-get install doxygen

cd OpenCC
make
make install

成功安装 opencc

1
2
3
4
$ opencc --version

Open Chinese Convert (OpenCC) Command Line Tool
Version: 1.0.5

本文网址: https://pylist.com/topic/191.html 转摘请注明来源

Suggested Topics

golang 计算大文件md5

以前介绍过用python 计算大文件的md5 值,这里将介绍使用 golang 计算大文件md5...

一个简单高效的LRU 缓存,golang 实现

LRU(Least recently used,最近最少使用)是根据数据的历史访问记录来进行淘汰数据,其核心思想是“如果数据最近被访问过,那么将来被访问的几率也更高”。...

Golang 版 supervisord 使用记录

python 版经常出现一些错误,比如 supervisor.sock 文件找不到的错误。懒得去整,试试二进制的 supervisord ,用 Go 语言编写。...

在 Nginx 和 Golang web 上抢先体验 QUIC

QUIC(Quick UDP Internet Connection)是谷歌推出的一套基于 UDP 的传输协议,它实现了 TCP + HTTPS + HTTP/2 的功能,目的是保证可靠性的同时降低网络延迟。QUIC 是使用 UDP 协议,可以与原来的 TCP 服务不冲突。...

Golang 实现新闻网页提取正文内容

前段时间接触到新闻页面的提取问题,发现了python 实现的 gne ,测试一段时间,效果很好,但还不适合个人的需求,于是就用 go 来实现类似的功能。...

golang 缓存模版的方法

这是官方使用的方法,实例初始化时把所有模版渲染后缓存到 templates,后续使用ExecuteTemplate 方法来使用特定的模版...

使用Golang selenium WebDriver 自动登录微博

有时候在Golang 程序里需要读取微博的某个页面内容,提示需要登录。最简单的方法是在浏览器里登录后,直接复制Cookie 的内容,然后 http 请求带上Cookie。这样会有人工介入,不是很方便,这里借用 selenium WebDriver 来打开微博登录页面,然后自动填入用户名与密码。...

Leave a Comment