KazMuzik.net
Music / Technology / Healthcare / Immigration / アメリカ
Google
 
<< Kingwin eSATA / USB 2.0 enclosure - Black Friday Sale #8ディスク関連製品 - Black Friday Sale #9 >>

天国への階段 - 英辞郎 + Lucene #9 - KazMuzik Blog
2007-11-25 02:16

英辞郎 + Lucene Project で、"stairway to heaven" の検索結果が、いつも 「(パンストの)伝線」ばかりでは格好悪いので、Lucene index に、「天国への階段」を加えることにします。
import net.java.sampo.lucene.eijiro.index.EijiroLuceneIndex;
import static net.java.sampo.lucene.eijiro.index.EijiroLuceneIndex.EijiroSearchResult;
import static net.java.sampo.lucene.eijiro.index.EijiroLuceneIndex.parse;
import org.apache.lucene.document.Document;

public class StairwayToHeaven {
  private static final String stairwayToHeaven
    = "天国への階段";
  private static final String lineStairwayToHeaven
    = "■stairway to heaven : "
    + "〈楽曲〉天国への階段"
    + "◆イギリスのロックグループ、レッド・ツェッペリンの代表曲。"
    + "ジミー・ページとロバート・プラントによって作詞作曲された。"
    + "1971年に発表され、以後ロック史上最高の名曲の一つとして広く愛聴されている。"
    + "レコードでの演奏時間は約8分。";
  // http://ja.wikipedia.org/wiki/
  // %E5%A4%A9%E5%9B%BD%E3%81%B8%E3%81%AE%E9%9A%8E%E6%AE%B5_%28%E6%A5%BD%E6%9B%B2%29

  public static void main(String[] args) throws Exception {
    EijiroLuceneIndex index = new EijiroLuceneIndex("eijiro_simple_index3");
    index.openSearcher();
    EijiroSearchResult[] results = index.search("stairway AND heaven");
    boolean exists = false;
    for (EijiroSearchResult result : results) {
      String line = result.getLine();
      if (line.indexOf(stairwayToHeaven) >= 0) {
        exists = true;
      }
    }
    index.closeSearcher();
    if (exists) {
      System.err.println( "\"Stairway to Heaven\" already exists." );
    }
    else {
      System.err.println( "Adding \"Stairway to Heaven\"..." );
      index.openWriter();
      Document doc = parse(lineStairwayToHeaven);
      index.addDocument(doc);
      index.closeWriter(false);
    }
  }
}

実は、EijiroLuceneIndex クラスにも手を入れていますが、これについては次回(以降)に説明します。基本的には、今回のクラスから使うためのメソッドを、切り出して、public にしただけです。
$ java -Dsen.home=/usr/java/sen \
  -classpath .:lib/lucene-core-2.2.0.jar:lib/lucene-ja.jar:lib/sen.jar:lib/commons-logging.jar \
  StairwayToHeaven
Adding "Stairway to Heaven" ...
$ sh index.sh 5
> stairway AND heaven
query=(+en:stairway +en:heaven)
1.000000[id=1576357] ■stairway to heaven : 〈豪俗〉(パンストの)伝線◆【同】run ; ladder
1.000000[id=1858538] ■stairway to heaven : 〈楽曲〉天国への階段◆イギリスのロックグループ、レッド・ツェッペリンの代表曲。\
ジミー・ページとロバート・プラントによって作詞作曲された。1971年に発表され、以後ロック史上最高の名曲の一つとして広く愛聴されている。\
レコードでの演奏時間は約8分。
> 天国への階段
query=ja:"天国 へ の 階段"
1.000000[id=1858538] ■stairway to heaven : 〈楽曲〉天国への階段◆イギリスのロックグループ、レッド・ツェッペリンの代表曲。\
ジミー・ページとロバート・プラントによって作詞作曲された。1971年に発表され、以後ロック史上最高の名曲の一つとして広く愛聴されている。\
レコードでの演奏時間は約8分。
> 
$ 

いい感じになりました。

Tags: programming