From b0400bcb378195ce21c57dea77f6a3ff4ed07be2 Mon Sep 17 00:00:00 2001 From: Vincent Gao Date: Wed, 24 Jun 2026 09:53:47 +0200 Subject: [PATCH] fix: prevent ZeroDivisionError in token-based algos for sequences shorter than qval When qval > 1 and the input sequence is shorter than qval characters, find_ngrams returns an empty list, causing _get_sequences to produce empty sequences. Token-based algorithms (Jaccard, Sorensen, Overlap, Cosine, Tversky) then divide by zero when computing intersection/denominator. Fall back to individual characters via list(s) when n-gram extraction yields no n-grams, preserving the information in the input sequence rather than silently treating it as empty. --- textdistance/algorithms/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/textdistance/algorithms/base.py b/textdistance/algorithms/base.py index 5ce8cc7..7d8f81c 100644 --- a/textdistance/algorithms/base.py +++ b/textdistance/algorithms/base.py @@ -128,7 +128,7 @@ def _get_sequences(self, *sequences: Sequence[object]) -> list: if self.qval == 1: return list(sequences) # by n-grams - return [find_ngrams(s, self.qval) for s in sequences] + return [find_ngrams(s, self.qval) or list(s) for s in sequences] def _get_counters(self, *sequences: Sequence[object]) -> list[Counter]: """Prepare sequences and convert it to Counters.