使用cconv工具进行简繁转换

  |   0 评论   |   1,206 浏览

0 iconv用于简繁转换的问题

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

1 安装cconv工具

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

1sudo apt install cconv

即可安装cconv

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

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

2 cconv工具的使用

直接放帮助文档

 1cconv -h
 2
 3Chinese-Convert Tool. Version 0.6.2 (inside libcconv version 0.6.2).
 4Copyright (c) 2008-2009, China. xiaoyjy@gmail.com
 5
 6Usage: cconv [OPTION...] [FILE]
 7
 8Convert encoding of given files from one encoding to another.
 9
10 Input/Output format specification:
11  -f [NAME] encoding of original text! at this you can use GBK, BIG5 or UTF8
12  -t [NAME] encoding for output! at this you can use GBK, BIG5, UTF8-CN, UTF8-HK or UTF-TW
13
14 Information:
15  -l list coded character sets can used
16
17 Output control:
18  -o [FILE] output file
19
20  -?, -h, -v  Show this help page.
21Report bugs to <xiaoyjy@gmail.com>

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

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

1cconv -l
2
3The following encodings are supported:
4
5Chinese:
6   GB2312, GBK, GB-HANS, GB-HANT, GB18030, BIG5, UTF8, UTF8-CN, UTF8-TW, UTF8-HK
7Other:
8   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#!/bin/sh
2for file in *.ass
3do
4fileout=$(echo $file | cut -d. -f1).ass
5cconv -f utf8 -t utf8-cn "$file" -o "$fileout"
6done


标题:使用cconv工具进行简繁转换
作者:joyqat
地址:https://joyqat.top/articles/2022/04/28/1651140544896.html

评论

发表评论


取消