yuu_nkjm blog


2011-05-11(Wed) [長年日記] 編集

[Lucene][NLP][Java] LuceneでTF(Term Frequency: 単語の出現頻度)を取得する

IndexSearcher.explain(検索にヒットした文書)というメソッドを呼ぶと,検索にヒットした文書のExplanationを取得できる.Explanation.toStringの中にTF(Term Frequency: 単語の出現頻度,回数)やPF(Phrase Frequency: フレーズの出現頻度)が文字列で格納されている.計算コストはかかるが,この文字列からTFやPFを切り出すことができる.

最初は全文検索システム Hyper Estraierを使おうと思ったんだけど,TFより高いレイヤ(スコアの高い文書の取得とか)でやる分には良さそうだったけど,TFを調べるのに適したAPIがなさそうだったので,Luceneを使うことにした.

サンプルコード

id:sleepy_yoshiさんのサンプルコードをお借りすると,こんな感じ.

class HogeSearch {
  public static void main (String [] args) throws Exception {
    //最後らへん
    for (ScoreDoc scoreDoc : docs2.scoreDocs) {
      Explanation explanation = indexSearch.explain(
        query2, scoreDoc.doc);
      System.out.println(explanation.toString());
    }
  }
}

(出力)

━━━━━━━━━━━━━━━━━━━━━━━
0.8784157 = (MATCH) weight(body:"あひ ひゃ" in 1), product of:
 1.0 = queryWeight(body:"あひ ひゃ"), product of:
   2.8109303 = idf(body: あひ=1 ひゃ=1)
   0.3557541 = queryNorm
 0.8784157 = fieldWeight(body:"あひ ひゃ" in 1), product of:
   1.0 = tf(phraseFreq=1.0)   ←←←←←←← 【ココ】
   2.8109303 = idf(body: あひ=1 ひゃ=1)
   0.3125 = fieldNorm(field=body, doc=1)
━━━━━━━━━━━━━━━━━━━━━━━

TFやPFを計算している箇所

TFやPFを計算し格納するのは,以下の箇所の様だ.

public class TermQuery extends Query {
  private class TermWeight extends Weight {
    public Explanation explain(IndexReader reader, int doc)
      throws IOException {
      if (termDocs != null) {
        try {
          if (termDocs.skipTo(doc) && termDocs.doc() == doc) {
            tf = termDocs.freq();
          }
        } finally {
          termDocs.close();
        }
        tfExplanation.setValue(similarity.tf(tf));
        tfExplanation.setDescription("tf(termFreq("+term+")="+tf+")");
      }
    }
  }
}
public class PhraseQuery extends Query {
  private class PhraseWeight extends Weight {
    public Explanation explain(IndexReader reader, int doc)
      throws IOException {
      float phraseFreq;
      if (d == doc) {
        phraseFreq = scorer.freq();
      } else {
        phraseFreq = 0.0f;
      }
      tfExplanation.setValue(similarity.tf(phraseFreq));
      tfExplanation.setDescription("tf(phraseFreq=" + phraseFreq + ")");
    }
  }
}

2011-04-13(Wed) [長年日記] 編集

[Postgress] Postgressのメモ

su postgres
psql database_name

database_nameというデータベースに接続する.

langridResources=# \d
langridResources=#
langridResources=# \d
langridResources=# DROP TABLE test;
DROP TABLE
langridResources=# \d
langridResources=# DROP TABLE tes;
DROP TABLE
langridResources=# DROP TABLE testtest;
DROP TABLE
langridResources=# DROP TABLE testtest;
ERROR:  テーブル"testtest"は存在しません
langridResources=# \d
langridResources=# \t
タプルのみを表示しています。
langridResources=# \t test
unrecognized boolean value; assuming "on".
タプルのみを表示しています。
langridResources=# \t nkjm
unrecognized boolean value; assuming "on".
タプルのみを表示しています。
langridResources=# select * from resource;

2011-04-12(Tue) [長年日記] 編集

[MySQL] MySQLの小ネタ(バイナリログ,接続数,タイムアウト他)

バイナリログを見る

mysqlbinlog mysql-bin.000100|lv

接続数のチューニング

MySQLの接続数が多いときのチューニング | KennyQi PHP Blog

タイムアウトの確認と設定

mysql -u root -p -e 'show variables like "wait_timeout"'
Enter password:
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout  | 28800 |
+---------------+-------+
mysql -u root -p -e 'SET GLOBAL wait_timeout = 120'

MySQLでカラム名の確認

mysql> describe published_dictionaries;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| dictionary_id   | varchar(255) | NO   | PRI | NULL    |       |
| dictionary_name | varchar(255) | NO   |     | NULL    |       |
| file_name       | varchar(255) | NO   |     | NULL    |       |
| user_id         | varchar(255) | NO   |     | NULL    |       |
| deployed        | datetime     | NO   |     | NULL    |       |
+-----------------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

トップ 最新 追記 設定
2006|01|06|12|
2007|06|09|
2008|01|03|04|06|07|08|09|10|12|
2009|01|02|05|06|07|08|10|11|12|
2010|03|04|05|06|07|08|09|10|11|
2011|01|02|03|04|05|06|07|08|09|11|12|
2012|01|02|04|06|07|08|10|11|12|
2013|01|02|03|07|08|10|11|12|
2014|01|02|04|05|06|07|08|09|10|11|
2015|01|02|07|11|12|
2016|01|03|05|07|08|09|