↓

使用cconv工具进行简繁转换

··

0 iconv用于简繁转换的问题
#

经典的iconv工具可以进行简繁转换,但是仅支持逐字从一个字符集转换到另一个字符集。若两个字符集之间没有对应关系,则转换会失败。另一方面,简繁之间部分词语存在对应关系,部分字也存在一对多的情况,在这种情况下iconv工具并不能很好的得到期望的结果。

1 安装cconv工具
#

在Ubuntu的官方源中已经有cconv工具了,只需要

1
sudo apt install cconv

即可安装cconv。

当然,如果使用的发行版软件仓库没有这个工具,可以从源代码安装。cconv的源代码现在托管于Github。

1
2
3
4
5
6
7
# 如果没有相应的工具或环境,应先安装
# 对于ubuntu,使用sudo apt install build-essential安装必要的环境
git clone https://github.com/xiaoyjy/cconv.git cconv
cd cconv
./autogen.sh
./configure --prefix=/usr
make && make install

2 cconv工具的使用
#

直接放帮助文档

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
cconv -h

Chinese-Convert Tool. Version 0.6.2 (inside libcconv version 0.6.2).
Copyright (c) 2008-2009, China. xiaoyjy@gmail.com

Usage: cconv [OPTION...] [FILE]

Convert encoding of given files from one encoding to another.

 Input/Output format specification:
  -f [NAME] encoding of original text! at this you can use GBK, BIG5 or UTF8
  -t [NAME] encoding for output! at this you can use GBK, BIG5, UTF8-CN, UTF8-HK or UTF-TW

 Information:
  -l list coded character sets can used

 Output control:
  -o [FILE] output file

  -?, -h, -v  Show this help page.
Report bugs to <xiaoyjy@gmail.com>

使用时注意-f指定的源文件和-t指定的输出文件不能相同,否则命令会无法结束且文件内容会丢失,这一点和iconv一样。不同于iconv,cconv没有-i命令开关,因此无法实现直接在原文件中转换。

查看其支持的编码格式如下

1
2
3
4
5
6
7
8
cconv -l

The following encodings are supported:

Chinese:
   GB2312, GBK, GB-HANS, GB-HANT, GB18030, BIG5, UTF8, UTF8-CN, UTF8-TW, UTF8-HK
Other:
   All encoding supported by iconv.

这里给一个实际使用到的例子,将当前目录下的所有ass字幕文件从繁体(utf8)转换为简体(utf8),转换前的文件名为[VCB-Studio] Ansatsu Kyoushitsu II [01][Ma10p_1080p][x265_flac].YY-tc.ass,希望转换后的文件名为[VCB-Studio] Ansatsu Kyoushitsu II [01][Ma10p_1080p][x265_flac].ass,可以使用这个脚本:

1
2
3
4
5
6
#!/bin/sh
for file in *.ass
do
fileout=$(echo $file | cut -d. -f1).ass
cconv -f utf8 -t utf8-cn "$file" -o "$fileout"
done