The relevance.py module is a python module part of kupfer, providing no less function than assigning the importance of each catalog item for a search or filter query. [1]
The relevance code is taken directly from Gnome-Do and was ported to Python by Christian Hergert
The module has two user-visible functions, score and formatCommonSubstrings. The most important function, is of course score:
def score(s, query):
"""
A relevancy score for the string ranging from 0 to 1
@s: a string to be scored
@query: a string query to score against
`s' is treated case-insensitively while `query' is interpreted literally,
including case and whitespace.
Returns: a float between 0 and 1
>>> print(score('terminal', 'trml'))
0.735098684211
>>> print(score('terminal', 'term'))
0.992302631579
"""
I laid hand on the module and ported it to using faster Python methods, making Python's unicode.find (or str.find if the input is str) do most of the heavy lifting -- along with an algorithmic change to speed up the string score calculation by quite a factor.
Since the module's functions are pure and fundamental, the module has broad compatibility, it works identically on all python versions I have tested: python2.4, python2.5, python2.6 and python3.1!
Only two important things make the module simultaneously Python 2- and Python 3-compatible:
from __future__ import division
# This module is compatible with both Python 2 and Python 3;
# we need the iterator form of range for either version, stored in range()
try:
range = xrange
except NameError:
pass
Neither change is strictly necessary; the first is always a good guard (and solved a bug in the original Python port), and the second is necessary if we want the iterator form of range() in Python 2.
| [1] | It provides the base score, after which the catalog item may be boosted if it is used a lot. |