nightingale
nightingale

Reputation: 1

How to create CustomSimilarity Class using pylucene?

In Java, a custom similarity scoring function is created by extending the SimilarityBase Class and overriding the scoring method. However, I cannot find a way to do the same using pylucene.

I have tried extending the SimilarityBase class the same way as we do in Java.

class CustomSimilarity(SimilarityBase):
        def __init__(self):
        SimilarityBase.__init__(self)

    def score(self,stats,termfreq,doclen):
        return termfreq

    def toString(self):
        return "Term Frequency Scoring"

However, I get an error during the allocation of the CustomSimilairity Class to my Index Searcher

lucene.JavaError: <super: <class 'JavaError'>, <JavaError object>>
    Java stacktrace:
java.lang.InstantiationException: 

Upvotes: 0

Views: 144

Answers (2)

Tobias
Tobias

Reputation: 947

The way to fix this is by inheriting from PythonClassicSimilarity since there needs to happen some magic see here so the code should be:

from org.apache.pylucene.search.similarities import PythonSimilarity

class CustomSimilarity(PythonSimilarity):

    def score(self,stats,termfreq,doclen):
        return termfreq

    def toString(self):
        return "Term Frequency Scoring"

Upvotes: 0

nightingale
nightingale

Reputation: 1

I found a solution. I don't understand why this is correct though.

class CustomSimilarity(SimilarityBase):
    def __init__(self):
        #self.super = SimilarityBase()
        pass

    def score(self,stats,termfreq,doclen):
        return termfreq

    def toString(self):
        return "Term Frequency Scoring"

Upvotes: 0

Related Questions