Nginx层面使用OPENCC实现简繁体转换

本文共有3588个字,关键词:nginxopencc简繁转换openresty

前言

前段时间接了个小活,要求是在nginx层面实现简繁体转换。一般正常的简繁转换都会放在前端页面的JS来实现,为何会要求在nginx层面实现呢?问过客户后,发现是要反向代理其它网站。具体的用途我也没好意思深究,我还是只管好好实现功能就完了。

方案一

最先想到的自然是简单的查找替换,自然是在nginx层面使用lua,需要nginx安装ngx_http_substitutions_filter_module模块和lua-nginx-module,或者使用openresty也可以。我这里直接使用openresty安装ngx_http_substitutions_filter_module模块。
1、 下载

openresty(https://openresty.org/download/openresty-1.15.8.1.tar.gz)
cd /usr/local/src
wget https://openresty.org/download/openresty-1.15.8.1.tar.gz
tar zxvf openresty-1.15.8.1.tar.gz
cd openresty-1.15.8.1

2、 下载nginx第三方模块

ngx_http_substitutions_filter_module
mkdir git
cd git
wget https://github.com/yaoweibin/ngx_http_substitutions_filter_module/archive/master.zip
unzip master.zip
mv ngx_http_substitutions_filter_module-master/ ngx_http_substitutions_filter_module
cd ..

3、 编译openresty

./configure --prefix=/usr/local/openresty --with-http_v2_module --with-http_ssl_module --with-luajit --with-http_stub_status_module --with-http_sub_module --add-module=/usr/local/src/openresty-1.15.8.1/git/ngx_http_substitutions_filter_module/
gmake && gmake install

4、lua程序
这样环境就准备得差不多了,接下来就是lua程序了。具体的反向代理的配置就不给出了,自己根据需要配置nginx.conf就行了,主要是在nginx配置文件中增加一行以下引用

body_filter_by_lua_file /usr/local/openresty/nginx/lua/repl.lua;

然后编译repl.lua文件

ngx.arg[1] = string.gsub(ngx.arg[1],"https://www.******.com","http://***.net")
ngx.arg[1] = string.gsub(ngx.arg[1],"www.******.com","***.net")
local jt="啊阿埃挨......"
local ft="啊阿埃挨......"
for i=1,4792*3,3 do
    ngx.arg[1] = string.gsub(ngx.arg[1],string.sub(jt, i, i+2),string.sub(ft, i, i+2))
end

程序就是这么简单循环替换汉字就完事了。得益于lua的效率,这样做出来的效果在速度上还在能接受的范围内,具体速度数据没有做测试。(限于篇幅,这里的简繁对应表都做了省略,实在太长了,需要的话可以去网上查找对应表,修改jt和ft变量值即可。)

方案二

标题里写的opencc怎么一直没出现呢?接下就该opencc出场了。配置上还是和方案一的环境样,也是直接使用openresty+opencc。
这里只能讲讲原理了,毕竟有人花钱了,咱需要照顾一下金主的心情,所以不好意思,这里就不直接给出lua的程序来了。
原理是通openrestsy的luajit可以调用C接口特性,刚好opencc也提供C接口调用。具体调用接口可以参考opencc作者的网站说明:
http://byvoid.github.io/OpenCC/1.0.4/group__opencc__c__api.html#gaedebe843f05973efd295461be90d50f2

得益于opencc强大功能,在nginx层面的替换变得更加有效和准确了。
opencc特性:

嚴格區分「一簡對多繁」和「一簡對多異」。
完全兼容異體字,可以實現動態替換。
嚴格審校一簡對多繁詞條,原則爲「能分則不合」。
支持中國大陸、臺灣、香港異體字和地區習慣用詞轉換,如「裏」「裡」、「鼠標」「滑鼠」。
詞庫和函數庫完全分離,可以自由修改、導入、擴展。

opencc預設配置文件

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 Standard) 簡體到香港繁體(香港小學學習字詞表標準)
hk2s.json Traditional Chinese (Hong Kong Standard) 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 標準)到臺灣正體
t2hk.json Traditional Chinese (OpenCC Standard) to Hong Kong Standard 繁體(OpenCC 標準)到香港繁體(香港小學學習字詞表標準)

总结

方案一简单粗暴,也是最容易最简单直观的方法。
方案二可扩展性与准确性都要强太多了。

参考:
http://byvoid.github.io/OpenCC/1.0.4/group__opencc__c__api.html#gaedebe843f05973efd295461be90d50f2
https://github.com/BYVoid/opencc
https://github.com/BYVoid/OpenCC/wiki/%E7%B7%A3%E7%94%B1

「一键投喂 软糖/蛋糕/布丁/牛奶/冰阔乐!」

e2c

(๑>ڡ<)☆谢谢老板~

使用微信扫描二维码完成支付

版权声明:本文为作者原创,如需转载须联系作者本人同意,未经作者本人同意不得擅自转载。
添加新评论
已有 2 条评论
  1. jackydu:

    大佬,能分享一下lua+opencc的代码,感激不尽

    1. jackydu: 回复 @jackydu

      openresy+opencc(!)[zface_7.png]