中文簡繁體互轉時,使用直譯很簡單,Delphi中使用LCMapStringW這個API。
但是如果要追求完美,考慮兩岸用詞習慣,就需要考慮“語義”,有兩種情況:
(1)字還是基本是那個字,只是寫法不同。如:
-
週期 - 週期
-
周杰倫 - 周傑倫
(2)字都變了。如:
- 文件 - 檔案
- 程序 - 程式
周和週:◎ “周”作姓、朝代時,繁體也爲“周”,與簡體同形。◎ 圈子、周圍、周到、星期的“周”繁體爲“週”。◎ 周chou(賙),接濟。
語義就要涉及上下文分析,有點AI在裏面,就不那麼容易了。
微軟Word和金山WPS的“中文簡繁體”轉換都不是簡單的直譯,有加AI。
百度的在線翻譯也有加AI。(不過“下週期末考試”和“這週期末考試”翻譯結果不同,可能訓練素材不全)
調用百度翻譯API,需要先去這裏申請開通:http://api.fanyi.baidu.com/
Delphi中調用百度翻譯API,如下:
function TranslateWordByHttp(AFromLanguage,AToLanguage:PacroCultureInfo;AText:WideString):WideString;
var
vParam:TStringList;
vMS:TMemoryStream;
S:Ansistring;
WS:WideString;
vHTTP:TIdHTTP;
MD5: TIdHashMessageDigest5;
vResult:TJSONObject;
vR:TJSONArray;
begin
Result:='';
vParam:=TStringList.Create;
vMS:=TMemoryStream.Create;
vHTTP:=TIdHttp.Create(nil);
try
vParam.Values['appid']:=C_appid;
vParam.Values['from']:=AFromLanguage^.BaiDu_Abbreviate;
vParam.Values['to']:=AToLanguage^.BaiDu_Abbreviate;
vParam.Values['q']:=AText;
vParam.Values['salt']:=InttoStr(Random(32768));
//vParam.Values['dict']:='1';
//拼接appid+q+salt+密鑰:
//S:=vParam.Values['appid']+S+vParam.Values['salt']+C_secret;
S:=C_appid;
vMS.Write(S[1],Length(S));
//按要求,必須UTF8,Delphi下S必須定義爲AnsiString
S:=UTF8Encode(AText);
vMS.Write(S[1],Length(S));
S:=vParam.Values['salt'];
vMS.Write(S[1],Length(S));
S:=C_secret;
vMS.Write(S[1],Length(S));
MD5 := TIdHashMessageDigest5.Create;
vMS.Position:=0;
//不能用HashStringAsHex,因爲它定義的string其實是widestring,utf8隱含會又被轉成unicode
vParam.Values['sign']:=MD5.HashStreamAsHex(vMS).ToLower();
MD5.Free;
vMS.Clear;
vHTTP.Request.ContentType:='application/x-www-form-urlencoded';
vHTTP.Post('http://api.fanyi.baidu.com/api/trans/vip/translate',vParam,vMS);
Setlength(S,vMS.Size);
Move(vMS.Memory^,S[1],vMS.Size);
//不需要轉碼
//WS:=UTF8Decode(S);
//得到內容字串:
//'{"from":"zh","to":"cht","trans_result":[{"src":"\u8d44\u672c\u4e3b\u4e49","dst":"\u8cc7\u672c\u4e3b\u7fa9"}]}'
vResult:=TJSONObject(TJSONObject.ParseJSONValue(S));
try
if vResult.Values['error_code']=nil then
begin
vR:=TJSONArray(vResult.Values['trans_result']);
if vR.Count>0 then
begin
//不需要轉碼
//WS:=UTF8Decode(S);
result:=TJSONString(TJSONObject(vR.Items[0]).Values['dst']).Value;
end;
end;
finally
vResult.Free;
end;
finally
vHttp.Free;
vMS.Free;
vParam.Free;
end;
end;
我測試了結果是OK的,有加入AI功能。
本來想網上找一個詞彙對照庫,沒有找到合適的。
ZhConversion.php內容大概是這樣的:
'䴬' => '䴬',
'麲' => '麲',
'麨' => '麨',
'䴴' => '䴴',
'麳' => '麳',
'𪘀' => '𪘀',
'𪘯' => '𪘯',
'' => '棡',
'0多隻' => '0多隻',
'0天后' => '0天後',
'0只' => '0隻',
'0餘' => '0餘',
'1天后' => '1天後',
'1只' => '1隻',
'2天后' => '2天後',
'2只' => '2隻',
'3天后' => '3天後',
'3只' => '3隻',
'4天后' => '4天後',
'4只' => '4隻',
'5天后' => '5天後',
'5只' => '5隻',
'6天后' => '6天後',
'6只' => '6隻',
'7天后' => '7天後',
'7只' => '7隻',
'8天后' => '8天後',
'8只' => '8隻',
'9天后' => '9天後',
'9只' => '9隻',
'〇只' => '〇隻',
'〇餘' => '〇餘',
'一乾二淨' => '一乾二淨',
查找了一下“週期”這個詞,沒有。放棄。
《簡體繁體字對照表全3.csv》內容大概是這樣的:
[轉換方向] [添加或修改] [轉換爲] [詞性]
簡繁雙向 計算機 電腦 名詞
簡繁雙向 0出現 0出現 名詞
簡繁雙向 0出現 0出現 名詞
簡繁雙向 0出線 0出線 名詞
簡繁雙向 0出線 0出線 名詞
簡繁雙向 0只支持 0只支持 名詞
簡繁雙向 0只支援 0只支援 名詞
簡繁雙向 0周後 0周後 名詞
簡繁雙向 0天后 0天後 名詞
簡繁雙向 0只 0隻 名詞
簡繁雙向 0餘 0餘 名詞
簡繁雙向 0出 0齣 名詞
簡繁雙向 1只支持 1只支持 名詞
簡繁雙向 1只支援 1只支援 名詞
簡繁雙向 1周後 1周後 名詞
簡繁雙向 1天后 1天後 名詞
簡繁雙向 1只 1隻 名詞
簡繁雙向 1餘 1餘 名詞
簡繁雙向 2只支持 2只支持 名詞
簡繁雙向 2只支援 2只支援 名詞
簡繁雙向 2周後 2周後 名詞
也用“週期”查了一下,沒有。“1周後”也沒翻譯成“1週後”,放棄。