Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

fix(tokenizer): respect lowercase=False in ngramtokenize

JSap0914f507c442fd2c
diff --git a/src/python/txtai/pipeline/data/tokenizer.py b/src/python/txtai/pipeline/data/tokenizer.py
index 202f0ecb..6dc575b8 100644
--- a/src/python/txtai/pipeline/data/tokenizer.py
+++ b/src/python/txtai/pipeline/data/tokenizer.py
@@ -162,7 +162,7 @@ class Tokenizer(Pipeline):
edge = self.ngrams.get("edge", False)
# Split on non-whitespace and apply optional word padding
- words = [f"{lpad}{x}{rpad}" for x in re.split(r"\W+", text.lower()) if x.strip()]
+ words = [f"{lpad}{x}{rpad}" for x in re.split(r"\W+", text) if x.strip()]
# Generate ngrams
ngrams = []
diff --git a/test/python/testpipeline/testdata/testtokenizer.py b/test/python/testpipeline/testdata/testtokenizer.py
index e27e473d..88daaa8c 100644
--- a/test/python/testpipeline/testdata/testtokenizer.py
+++ b/test/python/testpipeline/testdata/testtokenizer.py
@@ -51,3 +51,22 @@ class TestTokenizer(unittest.TestCase):
for test, result in tests:
# Unicode Text Segmentation per Unicode Annex #29
self.assertEqual(tokenizer(test), result)
+
+ def testNgramLowercase(self):
+ """
+ Test that the lowercase parameter is honoured inside ngramtokenize.
+ Before the fix, text.lower() was called unconditionally inside
+ ngramtokenize, causing lowercase=False to be silently ignored.
+ """
+
+ # lowercase=False must preserve the original case of ngram tokens
+ tokenizer = Tokenizer(lowercase=False, ngrams=3)
+ result = tokenizer("CAT")
+ self.assertIn("CAT", result)
+ self.assertNotIn("cat", result)
+
+ # lowercase=True (default) must still produce lowercase ngrams
+ tokenizer = Tokenizer(lowercase=True, ngrams=3)
+ result = tokenizer("CAT")
+ self.assertIn("cat", result)
+ self.assertNotIn("CAT", result)
\ No newline at end of file