2007年6月15日金曜日

mecab Tsearch2 postgresql install log

◎参考にしたもの
・http://mecab.sourceforge.net/
・http://sourceforge.net/project/showfiles.php?group_id=177856
・http://mecab.sourceforge.net/bindings.html
・https://www.oss.ecl.ntt.co.jp/tsearch2j/index.html

◎展開
#tar zxvf mecab-0.96.tar.gz
#tar zxvf mecab-ipadic-2.7.0-20070610.tar.gz
#tar zxvf mecab-ruby-0.96.tar.gz

◎mecab
# cd mecab-0.96
# ./configure --with-charset=utf8;make ;make check; make install

◎mecab-ipadic
# cd mecab-ipadic-2.7.0-20070610
# ./configure --with-charset=utf8;make ; make install

◎mecab-ruby
# cd mecab-ruby-0.96
# ruby extconf.rb
# make;make install

◎Tsearch2
# cdpostgresql-8.2.3/contrib/tsearch2/
# make; make install ;make installcheck

◎サンプルDBの作成
$ createdb -E UTF-8 sampledb

◎pgmecab
# wget http://www.emaki.minidns.net/Programming/postgres/pgmecab-1.1.tar.bz2
# tar jxvf pgmecab-1.1.tar.bz2
# cd pgmecab-1.1
Makefileを修正
top_builddir = /usr/local/src/postgresql-8.2.3
  postgresqlのソースへのpathを設定
MECAB_CONFIG_PATH = /usr/local/bin/mecab-config
  mecab-configへのpath
# make;make install

◎pgmecab.cに
fmgr.hヘッダファイルをincludeさせた後で、
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
のマクロを追加。8.2から必要になったらしい。

◎サンプルテーブルの作成
create table sample(i int4,t text,fti tsvector);

◎トリガーの作成
DROP TRIGGER tsvectorupdate ON tblSample;
CREATE TRIGGER tsvectorupdate BEFORE UPDATE OR INSERT ON sample
FOR EACH ROW EXECUTE PROCEDURE tsearch2(fti, pgmecab, t);

◎indexの作成
CREATE INDEX idxFTI_idx ON sample USING gist(fti);

◎サンプルクエリー
SELECT i, t
FROM sample
WHERE fti @@ to_tsquery('simple', replace(pgmecab(' する'), ' ', ''))
AND t LIKE '%オープン%';

◎ユーザー定義データ型の作成
create type finddoctype as (findindex integer, findtext text);

◎ユーザー定義関数の作成
drop function finddoc(text);
create function finddoc(text) returns setof finddoctype
as ' select findtbl.i as findindex, findtbl.t as findtext from
( select fromtbl.i, fromtbl.t from sample as fromtbl, to_tsquery(''simple'', replace(btrim(pgmecab($1),'' \n''), '' '', ''&'')) as q
where fromtbl.fti @@ q offset 0 ) as findtbl
where findtbl.t like ''%'' || $1 || ''%''; ' language sql;


sampledb=# select * from finddoc('ソース');
findindex | findtext
-----------+-------------------------------------
1 | プログラムはソースが決め手となる。
(1 row)

sampledb=# select * from finddoc('決め手');
findindex | findtext
-----------+-------------------------------------
1 | プログラムはソースが決め手となる。
(1 row)

sampledb=#

sampledb=# insert into Sample values (1, '登録テストwo
してみるのだけど、うまくいくのか心配です。');

update sample set t = '更新テストをしてみよう。日本は広いなぁ。' where i = 0;

sampledb=# delete from Sample where i = 1;

0 件のコメント: