我想在PyLucene中编写一个自定义的Similarity类来实现我自己的检索模型。
通常,使用java版本的Lucene,您必须扩展Similarity类并覆盖方法。例如:
public class IDFSimilarity extends TFIDFSimilarity {
/** Sole constructor: parameter-free */
public IDFSimilarity() {
}
/** Implemented as <code>overlap / maxOverlap</code>. */
@Override
public float coord(int overlap, int maxOverlap) {
return overlap / (float) maxOverlap;
}
/** Implemented as <code>1/sqrt(sumOfSquaredWeights)</code>. */
@Override
public float queryNorm(float sumOfSquaredWeights) {
return (float) (1/Math.sqrt(sumOfSquaredWeights));
}
.
.
etc
然而,PyLucene使用JCC,我不清楚如何在python脚本中扩展类。它会是这样的:
import lucene
from org.apache.lucene.search.similarities import TFIDFSimilarity
class IDFSimilarity (TFIDFSimilarity):
def __init__(self):
TFIDFSimilarity.__init__()
?
?
但我不知道怎么办。我在网上找不到任何示例或文档。
有什么想法吗?
答案 0 :(得分:1)
在原始问题的@JanŠpaček评论中,谢谢!
在PyLucene源代码中有一个在Python中定义相似性的示例。
from org.apache.pylucene.search.similarities import PythonClassicSimilarity
class SimpleSimilarity(PythonClassicSimilarity):
def lengthNorm(self, numTerms):
return 1.0
def tf(self, freq):
return freq
def sloppyFreq(self, distance):
return 2.0
def idf(self, docFreq, numDocs):
return 1.0
def idfExplain(self, collectionStats, termStats):
return Explanation.match(1.0, "inexplicable", [])
单击here查看示例。