PG使用 nlpbamboo chinesecfg 中文分词

环境:
CentOS 6.5 64bit
PostgreSQL 9.4.4
nlpbamboo-1.1.2
cmake-3.3.1
CRF++-0.57
2.安装cmake
[root@prod /opt]#cd cmake-3.3.1
[root@prod /opt]#./bootstrap --prefix=/opt/cmake3.3.1
[root@prod /opt]#gmake
[root@prod /opt]#gmake install
 
vi ~/.bash_profile
export PATH=/opt/cmake3.3.1/bin:$PATH
. ~/.bash_profile
3.安装CRF    
[root@prod /opt]# tar -zxvf CRF++-0.54.tar.gz 
[root@prod /opt]# cd CRF++-0.54
[root@prod /opt/CRF++-0.54]# ./configure 
[root@prod /opt/CRF++-0.54]# gmake
[root@prod /opt/CRF++-0.54]# gmake install
4.安装nlpbamboo
[root@prod /opt]# wget http://nlpbamboo.googlecode.com/files/nlpbamboo-1.1.2.tar.bz2貌似要翻墙,附件是这个文件。
[root@prod /opt]# tar -jxvf nlpbamboo-1.1.2.tar.bz2 
[root@prod /opt/nlpbamboo-1.1.2]# cd nlpbamboo-1.1.2
[root@prod /opt/nlpbamboo-1.1.2/build]# cd build/
[root@prod /opt/nlpbamboo-1.1.2/build]# cmake .. -DCMAKE_BUILD_TYPE=release
[root@prod /opt/nlpbamboo-1.1.2/build]# gmake all
[root@prod /opt/nlpbamboo-1.1.2/build]# gmake install
5.加入lib库链接

[root@prod /opt/nlpbamboo-1.1.2/build]# echo "/usr/lib" >>/etc/ld.so.conf

[root@prod /opt/nlpbamboo-1.1.2/build]# echo "/usr/local/lib" >>/etc/ld.so.conf

[root@prod /opt/nlpbamboo-1.1.2/build]# ldconfig -f /etc/ld.so.conf
测试是否正常:

[root@prod /opt/nlpbamboo-1.1.2/build]# ldconfig -p|grep bambo

        libbamboo.so.2 (libc6,x86-64) => /usr/lib/libbamboo.so.2

        libbamboo.so (libc6,x86-64) => /usr/lib/libbamboo.so

[root@prod /opt/nlpbamboo-1.1.2/build]# ldconfig -p|grep crf

        libcrfpp.so.0 (libc6,x86-64) => /usr/local/lib/libcrfpp.so.0

        libcrfpp.so (libc6,x86-64) => /usr/local/lib/libcrfpp.so

6.加入索引:
[root@prod /opt/nlpbamboo-1.1.2/build]# cd /opt/bamboo
wget http : //nlpbamboo.googlecode.com/files/index.tar.bz2
完蛋这里又下载不了了,还是别人帮忙下载的,附件有。
[root@prod /opt]# cd bamboo/
[root@prod /opt/bamboo]# tar -jxvf index.tar.bz2 
[root@prod /opt/bamboo]# cd exts/postgres/chinese_parser/
[root@prod /opt/bamboo/exts/postgres/chinese_parser]# make
[root@prod /opt/bamboo/exts/postgres/chinese_parser]# make install
7.编辑PG支持模块
[postgres@prod /opt/pgsql/share/contrib]$ cd /opt/bamboo/exts/postgres/chinese_parser
[postgres@prod /opt/pgsql/share/contrib]$make
[postgres@prod /opt/pgsql/share/contrib]$make install

[root@prod /opt/bamboo/exts/postgres/chinese_parser]# touch $PGHOME/share/tsearch_data/chinese_utf8.stop

[root@prod /opt/bamboo/exts/postgres/chinese_parser]# cd /opt/bamboo/exts/postgres/pg_tokenize

[root@prod /opt/bamboo/exts/postgres/chinese_parser]# make
[root@prod /opt/bamboo/exts/postgres/chinese_parser]# make install
8.安装PG支持模块

[root@prod /opt/bamboo/exts/postgres/chinese_parser]# su postgres

[postgres@prod /opt/bamboo/exts/postgres/chinese_parser]$ cd /opt/pgsql/share/contrib/

[postgres@prod /opt/pgsql/share/contrib]$ psql -h 127.0.0.1 postgres postgres -f chinese_parser.sql

[postgres@prod /opt/pgsql/share/contrib]$ psql -h 127.0.0.1 postgres postgres -f pg_tokenize.sql
中间可能会遇到这种错误:

postgres=# select * from tokenize('你好我是中国人');

ERROR:  invalid byte sequence for encoding "UTF8": 0xc4 0xe3

ERROR:  invalid byte sequence for encoding "UTF8": 0xc4 0xe3

原因是没有正确设置客户端字符集。
解决方法:
方法一:设置postgresql的客户端编码为GBK,这时PostgreSQL就知道输入的内容是GBK编码的,这样PostgreSQL数据库会自动做字符集的转换,把其转换成UTF8编码。psql中输入“\encoding GBK”
方法二:直接设置终端的字符集编码为UTF8,让输入的编码直接为UTF8,而不是GBK。[postgres@dsc ~]$ export PGCLIENTENCODING=GBK

9.查看全文检索配置中加入了chinesecfg的配置

postgres=# select * from tpg_ts_config;

  cfgname   | cfgnamespace | cfgowner | cfgparser 

------------+--------------+----------+-----------

 simple     |           11 |       10 |      3722

 danish     |           11 |       10 |      3722

 dutch      |           11 |       10 |      3722

 english    |           11 |       10 |      3722

 finnish    |           11 |       10 |      3722

 french     |           11 |       10 |      3722

 german     |           11 |       10 |      3722

 hungarian  |           11 |       10 |      3722

 italian    |           11 |       10 |      3722

 norwegian  |           11 |       10 |      3722

 portuguese |           11 |       10 |      3722

 romanian   |           11 |       10 |      3722

 russian    |           11 |       10 |      3722

 spanish    |           11 |       10 |      3722

 swedish    |           11 |       10 |      3722

 turkish    |           11 |       10 |      3722

 chinesecfg |           11 |       10 |     16453

(17 rows)

测试:
postgres=# select * from to_tsvector('chinesecfg','你好,我是中国人. 前在杭州斯凯做数据库相关的ぷ');                                                                to_tsvector                                                             ------------------------------------------------------------------------------------------------------- ',':2 '.':7,17 '中国':5 '人':6 '你好':1 '做':12 '在':9 '工作':16 '我':3 '数据库':13 '斯凯':11 '是':4 '杭州':10 '的':15 '目前':8 '相关':14 (1 row)

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/25954236/viewspace-1809299/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/25954236/viewspace-1809299/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值