Postgres中文分詞

環(huán)境

  • CentOS Linux release 7.2.1511 (Core)

安裝Postgres

安裝postgres很簡單

  1. yum安裝
    sudo yum install postgresql-server postgresql-contrib postgresql-devel
  2. 初始化數(shù)據(jù)庫
    sudo postgresql-setup initdb

配置密碼和遠(yuǎn)程訪問

  1. 修改 /var/lib/pgsql/data/pg_hba.conf
    原本的
host    all             all             127.0.0.1/32            ident
host    all             all             ::1/128                 ident

修改之后

host    all             all             127.0.0.1/32            trust
host    all             all             0.0.0.0/0               md5
  1. 修改/var/lib/pgsql/data/postgresql.conf
    原本的
    #listen_addresses=’localhost'
    修改之后
    #listen_addresses=’*'

  2. 增加密碼

su - postgres
psql
alter user postgres with password '123456';
  1. 啟動服務(wù)
systemctl start postgresql
systemctl enable postgresql

安裝中文分詞插件zhparser

  1. 安裝SCWS
wget http://www.xunsearch.com/scws/down/scws-1.2.3.tar.bz2
tar xf scws-1.2.3.tar.bz2
cd scws-1.2.3
./configure
make install
  1. 安裝zhparser
git clone https://github.com/amutu/zhparser.git
make && make install

實驗

-- create the extension

CREATE EXTENSION zhparser;

-- make test configuration using parser

CREATE TEXT SEARCH CONFIGURATION testzhcfg (PARSER = zhparser);

-- add token mapping

ALTER TEXT SEARCH CONFIGURATION testzhcfg ADD MAPPING FOR n,v,a,i,e,l WITH simple;

-- ts_parse

SELECT * FROM ts_parse('zhparser', 'hello world! 2010年保障房建設(shè)在全國范圍內(nèi)獲全面啟動,從中央到地方紛紛加大 了保障房的建設(shè)和投入力度 。2011年,保障房進(jìn)入了更大規(guī)模的建設(shè)階段。住房城鄉(xiāng)建設(shè)部黨組書記、部長姜偉新去年底在全國住房城鄉(xiāng)建設(shè)工作會議上表示,要繼續(xù)推進(jìn)保障性安居工程建設(shè)。');

----------------------------------------------------------------------------
                                 結(jié)果
----------------------------------------------------------------------------
 tokid |  token   
-------+----------
   101 | hello
   101 | world
   117 | !
   101 | 2010
   113 | 年
   118 | 保障
   110 | 房建



-- test to_tsvector

SELECT to_tsvector('testzhcfg','“今年保障房新開工數(shù)量雖然有所下調(diào),但實際的年度在建規(guī)模以及竣工規(guī)模會超以往年份,相對應(yīng)的對資金的需求也會創(chuàng)歷>史紀(jì)錄?!标悋鴱?qiáng)說。在他看來,與2011年相比,2012年的保障房建設(shè)在資金配套上的壓力將更為嚴(yán)峻。') as result;

----------------------------------------------------------------------------
                                 結(jié)果
----------------------------------------------------------------------------
 '2011':27 '2012':29 '上':35 '下調(diào)':7 '嚴(yán)峻':37 '會':14 '會創(chuàng)':20 '保障':1,30 '壓力':36 '史':21 '國強(qiáng)'
:24 '在建':10 '實際':8 '對應(yīng)':17 '年份':16 '年度':9 '開工':4 '房':2 '房建':31 '數(shù)量':5 '新':3 '有所':6
 '相比':28 '看來':26 '竣工':12 '紀(jì)錄':22 '規(guī)模':11,13 '設(shè)在':32 '說':25 '資金':18,33 '超':15 '配套':34
 '陳':23 '需求':19
(1 row)

-- test to_tsquery

SELECT to_tsquery('testzhcfg', '保障房資金壓力');

----------------------------------------------------------------------------
                                 結(jié)果
----------------------------------------------------------------------------

 '保障' & '房' & '資金' & '壓力'
(1 row)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容