【发布时间】:2017-11-06 12:59:45
【问题描述】:
在 lucene 中自定义 tf-idf 算法
我要更改 lucene 中的 tf-idf 算法,所以我创建一个新的 IndexSearcher 实例并调用 setSimilarity 函数。
is.setSimilarity(new TFIDFSimilarity() {
@Override
// Computes a score factor based on the fraction of all query terms that a document contains.
// @param overlap - the number of query terms matched in the document
// @param maxOverlap - the total number of terms in the query
public float coord(int overlap, int maxOverlap) {
return 1;
}
@Override
// Computes the normalization value for a query given the sum of the squared weights of each of the query terms.
// @param sumOfSquaredWeights - the sum of the squares of query term weights
public float queryNorm(float valueForNormalization) {
return 100;
}
@Override
// Computes a score factor based on a term or phrase's frequency in a document.
public float tf(float freq) {
return freq;
}
@Override
// Computes a score factor based on a term's document frequency (the number of documents which contain the term).
public float idf(long docFreq, long numDocs) {
return numDocs/ docFreq;
}
@Override
// Compute an index-time normalization value for this field instance.
// @param state - statistics of the current field (such as length, boost, etc)
public float lengthNorm(FieldInvertState state) {
return 1;
}
@Override
// Decodes a normalization factor stored in an index.
public float decodeNormValue(long l) {
return l;
}
@Override
// Encodes a normalization factor for storage in an index.
public long encodeNormValue(float v) {
return 0;
}
@Override
// Computes the amount of a sloppy phrase match, based on an edit distance.
// @param distance - the edit distance of this sloppy phrase match
public float sloppyFreq(int distance) {
return 1;
}
@Override
// Calculate a scoring factor based on the data in the payload.
public float scorePayload(int doc, int start, int end, BytesRef payload) {
return 1;
}
});
如何使用这些函数实现真正的 tf-idf 算法,然后将它们自定义为我自己的算法?
【问题讨论】:
-
不确定您在这里要求什么。所有这些方法are well documented,根据您的需要修改它们,无论它们是什么。
-
我需要一个真正实现的例子,你能帮帮我吗? @femtoRgon
标签: lucene information-retrieval tf-idf