在lucene中实施反馈

时间:2013-03-29 17:37:20

标签: lucene

有人可以给我提示在lucene中应用伪反馈。我在谷歌上找不到太多帮助。我正在使用相似性课程。 lucene中是否有任何类可以扩展以实现反馈? 感谢。

1 个答案:

答案 0 :(得分:2)

假设您指的是this relevance feedback method,一旦您获得了原始查询的TopDocs,请重复多少次(假设我们需要前25个文档中的前25个术语)原始查询)您想要的记录,并致电IndexReader.getTermVectors(int),它将获取您需要的信息。遍历每个。将术语频率存储在哈希映射中将是我立即发生的实现。

类似的东西:

//Get the original results
TopDocs docs = indexsearcher.search(query,25);
HashMap<String,ScorePair> map = new HashMap<String,ScorePair>();
for (int i = 0; i < docs.scoreDocs.length; i++) {
    //Iterate fields for each result
    FieldsEnum fields = indexreader.getTermVectors(docs.scoreDocs[i].doc).iterator();
    String fieldname;
    while (fieldname = fields.next()) {
        //For each field, iterate it's terms
        TermsEnum terms = fields.terms().iterator();
        while (terms.next()) {
            //and store it
            putTermInMap(fieldname, terms.term(), terms.docFreq(), map);
        }
    }
}

List<ScorePair> byScore = new ArrayList<ScorePair>(map.values());
Collections.sort(byScore);

BooleanQuery bq = new BooleanQuery();
//Perhaps we want to give the original query a bit of a boost
query.setBoost(5);
bq.add(query,BooleanClause.Occur.SHOULD);
for (int i = 0; i < 25; i++) {
    //Add all our found terms to the final query
    ScorePair pair = byScore.get(i);
    bq.add(new TermQuery(new Term(pair.field,pair.term)),BooleanClause.Occur.SHOULD);
}
}

//Say, we want to score based on tf/idf
void putTermInMap(String field, String term, int freq, Map<String,ScorePair> map) {
    String key = field + ":" + term;
    if (map.containsKey(key))
        map.get(key).increment();
    else
        map.put(key,new ScorePair(freq,field,term));
}

private class ScorePair implements Comparable{
    int count = 0;
    double idf;
    String field;
    String term;

    ScorePair(int docfreq, String field, String term) {
        count++;
        //Standard Lucene idf calculation.  This is calculated once per field:term
        idf = (1 + Math.log(indexreader.numDocs()/((double)docfreq + 1))) ^ 2;
        this.field = field;
        this.term = term;
    }

    void increment() { count++; }

    double score() {
        return Math.sqrt(count) * idf;
    }

    //Standard Lucene TF/IDF calculation, if I'm not mistaken about it.
    int compareTo(ScorePair pair) {
        if (this.score() < pair.score()) return -1;
        else return 1;
    }
}

(我没有声称这是功能代码,在它的当前状态。)