Simplified Chinese conversion (Golang)

Source: Internet
Author: User
Before the project needs a simplified to traditional function, the duration is too tight, you have to make one, the effect is OK. In the process of doing this, it is found that simple transfer is far more complicated than thought.
In the middle there are many usages of simplified characters, which are different in traditional Chinese. And some words such as (after, after, Taiwanese, Taiwan) in traditional Chinese have several ways and usages.
Simplified simplified to a word ...
Then the same word, in different phrases, some with the simplified one, some with the traditional one ...
Finally, Taiwan's traditional and Hong Kong traditional also have different differences.
Not listed, in short, is a troublesome matter. Mechanical conversion by some code is not possible. So it is usually better to write the words themselves by using the thesaurus to translate.
Of course, third-party service APIs (such as Youdao, wikis, etc.) can be tuned, but if business performance is required, do not make such third-party services a good point.
After the project was finished, I looked back at the problem and found an open CC project to do this. Then the Golang call

The package on GitHub has also been implemented, it has been tried, and really good results.

Example:

Package main
/* Use OPENCC for simple example

   OPENCC library: Https://github.com/BYVoid/OpenCC
   OPENCC wrapper for GOLANG:GITHUB.COM/STEVENYAO/GO-OPENCC

  author:xcl
*

/import (
	"FMT"
	OPENCC "github.com/ STEVENYAO/GO-OPENCC "
)

func main () {

	//Mac  local
	config_s2t: ="/users/xcl/langtest/input/ Opencc-master/data/config/s2t.json "
	//Linux
	//config_s2t: ="/usr/share/opencc/s2t.json "

	///simplified character string
	ch: = "in front of the minister, behind the Queen to visit the scenery, play games, go to the park night tour on hand-made yacht, see Victoria Harbour scenery." Get a good equipment to explore Taiwan, go swimming, surf, don't waste this opportunity. "/

	/applications need to check that the file is not present,
	//otherwise reported" fatal error:unexpected signal during runtime execution. "Cause service interruption
	c: = OPENCC. Newconverter (CONFIG_S2T)
	defer c.close ()
	//convert
	TW: = C.convert (ch)
	FMT. Println ("Traditional:", TW)
}/

*
Run Result:
➜  src  : Go run main.go
Traditional: There is a minister in front, the Queen in the back to explore the scenery, do games, Go to the Park Night tour and board a handmade boat to see the Victoria Harbour view. Get ready to explore Taiwan, go swimming, surf, and don't waste this opportunity.
*/

To run this program, you need to have S2t.json the thesaurus.

Configuration S2t.json:

Local, unzip the source code downloaded from GitHub, there is this file thesaurus,

Linux can be used under the Yum or RPM commands such as the installation of OPENCC, its thesaurus under the/USR/SHARE/OPENCC.

This project conversion is very powerful, you can note the following examples of the words or words, but some words or phrases used to customize the use of this really do not know how to add in or customize some conversions.

Limited time has not been studied, anyway, the line before the run well, stability-based, not to toss this.


Blog:http://blog.csdn.net







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.