bi-gram解析器でindex & query | Lucene PHP

英数字連続 + bi-gram解析器のアナライザーを作ってやっと動きました^-^。

☆以下サイトを参考にさせてもらいました。
Mecab解析器 : http://nonn-et-twk.net/twk/php-lucene-japanese-search
CJKIndexを利用して生成した索引の検索 : http://codezine.jp/article/detail/3947?p=3
UTF-8 互換のテキスト解析器 : http://framework.zend.com/manual/ja/zend.search.lucene.charset.html

=== sample ===
インデックスに追加! Lib_Bigramが自分のアナライザーです。

Zend_Search_Lucene_Analysis_Analyzer::setDefault( new  Lib_Bigram() ) ;
//Zend_Search_Lucene_Analysis_Analyzer::setDefault( new  Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8());

$indexFile = '/tmp/myIndex';
$index = null;
if( !is_dir($indexFile) ){
  $index = Zend_Search_Lucene::create($indexFile);
}else{
  $index = Zend_Search_Lucene::open($indexFile);
}

setIndex( '9xyz','888『賭博黙示録カイジ』今日は少女時代。999',$index);
setIndex('snsd','SNSD Live in TOKYO', $index );
setIndex( '123','Zend_Search_Lucene は、完全に PHP 545 で書かれている汎用的なテキスト検索エンジンです。', $index );
setIndex( 'ddd','『賭博黙示録カイジ』(とばくもくしろくカイジ)は、福本伸行による日本の漫画作品。', $index );

// たまに最適化
$index->optimize();

function setIndex( $myid, $title, $index ){
  $hits = $index->find('myid:'.$myid);
  foreach ($hits as $hit){
    echo $hit->myid."\n";
    $index->delete($hit->id);
  }
  // add to index
  $doc = new Zend_Search_Lucene_Document();
  // for datastore key
  $doc->addField(Zend_Search_Lucene_Field::Keyword('myid',$myid));
  // search index
  $doc->addField(Zend_Search_Lucene_Field::UnStored('title',$title,'utf-8'));
  // add index
  $index->addDocument($doc);
}

検索

Zend_Search_Lucene_Analysis_Analyzer::setDefault( new  Lib_Bigram() ) ;
//Zend_Search_Lucene_Analysis_Analyzer::setDefault( new  Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8());
$indexFile = '/tmp/myIndex';
$index = null;
if( !is_dir($indexFile) ){
  exit;
}else{
  $index = Zend_Search_Lucene::open($indexFile);
}

function search($word, $index, $name='title'){
  $hits  = $index->find($name.':'.$word);
  //var_dump($hits);
  echo "word  : " .$word."\n";
  if( count($hits)>0){
    foreach ($hits as $hit) {
      echo '  score : ' . $hit->score ."\n";
      echo '  myid  : ' . $hit->myid . "\n";
    }
  }else{
    echo "  not found \n";
  }
}

search('カイジ', $index);
search('少女時代', $index);
search('女時', $index);
search('Lucene', $index);
search('uce', $index);
search('123',$index,'myid');

bi-gramなので少女時代、女時にマッチしますが
英数字の Luceneにはまっちしても Lucen , uceなどにはマッチしません。
結果はこんな感じで

word  : カイジ
  score : 0.56655960891225
  myid  : ddd
  score : 0.53415752187766
  myid  : 9xyz
word  : 少女時代
  score : 0.82977822741985
  myid  : 9xyz
word  : 女時
  score : 0.47907268296854
  myid  : 9xyz
word  : Lucene
  score : 0.3593045122264
  myid  : 123
word  : uce
  not found
word  : 123
  score : 1
  myid  : 123

詰まった後に、できると楽しいですよね
complacency
自己満足に終わらないように、サービスにつなげます。

This entry was posted on 木曜日, 5月 12th, 2011 at 4:09 AM and is filed under Lucene, php. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Unityによるゲームを公開しております iOS/Android
https://itunes.apple.com/jp/app/lost-knight-3d-action/id900917032
https://play.google.com/store/apps/details?id=com.groundroad.runknight

各種開発支援・機能開発等小さいお仕事などもお請けしております。
unity開発支援, PHPシステム開発, javascript/html5 フロント開発, titanium mobileアプリ開発
お気軽にお問い合わせください
大崎・五反田近郊での対面でのお打ち合わせはいつでも可能です。