
昨天使用Mysql自定义函数批量简体繁体互转,郭同学看见后给我建议使用OpenCC简体繁体互转,今天去研究了下如何使用。
一、OpenCC是什么
Open Chinese Convet(OpenCC)是一个开源的中文简繁转换项目,致力于制作高质量的基于统计预料的简繁转换词库,还提供函数(libopencc)、命令行简繁转换工具、人工校对工具、词典生成程序、在线转换服务及图形用户界面。
二、Linux安装Opencc
准备工作
$ yum install -y cmake
$ yum install -y git
$ yum install -y doxygen
克隆开源项目
$ git clone https://github.com/BYVoid/OpenCC
安装
$ cd OpenCC
$ make && make install
验证安装是否成功
$ opencc --version
报错:
opencc: error while loading shared libraries: libopencc.so.1.1: cannot open shared object file: No such file or directory
查看运行时依赖文件
$ which opeopncc
/usr/bin/opencc
$ ldd /usr/bin/opencc
linux-vdso.so.1 => (0x00007ffd22bc1000)
libopencc.so.1.1 => not found #这里显示没有找到该文件
libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007feffd6f6000)
libm.so.6 => /lib64/libm.so.6 (0x00007feffd3f4000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007feffd1de000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007feffcfc2000)
libc.so.6 => /lib64/libc.so.6 (0x00007feffcbf4000)
/lib64/ld-linux-x86-64.so.2 (0x00007feffd9fd000)
建立库文件软链接
$ ln -s /usr/lib/libopencc.so.1.1 /usr/lib64/libopencc.so.1.1
再次查看版本
[root@localhost home]# opencc --version
Open Chinese Convert (OpenCC) Command Line Tool
Version: 1.1.1
[root@localhost home]#
三、测试验证
[root@localhost home]# echo "我爱祖国、祖国万岁" | opencc -c s2t
我愛祖國、祖國萬歲
[root@localhost home]# echo "我愛祖國、祖國萬歲" | opencc -c t2s
我爱祖国、祖国万岁
[root@localhost home]#
预设配置文件
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 variant) 簡體到香港繁體
hk2s.json Traditional Chinese (Hong Kong variant) 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 標準)到臺灣正體hk2t.json Traditional Chinese (Hong Kong variant) to Traditional Chinese 香港繁體到繁體(OpenCC 標準)
t2hk.json Traditional Chinese (OpenCC Standard) to Hong Kong variant 繁體(OpenCC 標準)到香港繁體
t2jp.json Traditional Chinese Characters (Kyūjitai) to New Japanese Kanji (Shinjitai) 繁體(OpenCC 標準,舊字體)到日文新字體
jp2t.json New Japanese Kanji (Shinjitai) to Traditional Chinese Characters (Kyūjitai) 日文新字體到繁體(OpenCC 標準,舊字體)
tw2t.json Traditional Chinese (Taiwan standard) to Traditional Chinese 臺灣正體到繁體(OpenCC 標準)
同时也支持对文件进行简繁转换
$ opencc -i product.sql -o product.sql -c s2t
开始的想法对数据库表product.ibd进行转换,但是不行,最终导出sql来转换;不知道是使用方式不正确,还是真的不可行,如果有小伙伴知道正确的方式,请留言告诉我,谢谢!
[root@localhost solo]# opencc -i product.ibd -o product.ibd -c s2t
Invalid UTF8:
[root@localhost solo]#
批量文件简繁转化脚本
#!/usr/bin/sh
#filename: t2s_batch.sh
if [ $# -ne 2 ];then
echo "USAGE: $0 path_name file_suffix"
exit
else
lst=`find $1 -type f -name "*.$2"`
lst_num=`find $1 -type f -name "*.$2"|wc -l`
if [ $lst_num -eq 0 ];then
echo "no such files."
else
for file in $lst
do
echo $file
opencc -i $file -o $file -c t2s
done
fi
fi




