def __call__(self, token_tuple, terms):
"""
Input:
token_tuple : Token or tuple of Token objects
terms : term or iterable of terms to match
Output:
Returns None if no match is found.
Returns the first matched in case many of them show the same
similarity ratio.
"""
if not hasattr(terms, '__iter__'):
terms = [terms]
if not isinstance(token_tuple, tuple):
token_tuple = (token_tuple,)
try:
token_tuple = tuple(self.key(token) for token in token_tuple)
except Exception: # as e
token_tuple = tuple(str(token) for token in token_tuple)
best_term = None
best_ratio = 0
for term in terms:
ratio = max([Levenshtein.ratio(unicode(" ".join(token_tuple)),
unicode(" ".join(term_i)))*100
for term_i in term])
if ratio >= self.match and ratio > best_ratio:
best_term = term
best_ratio = ratio
return best_term
# ------- UTIL FUNCTIONS ------------------------------------------------------
评论列表
文章目录