Registry / ai-ml / textsearch

textsearch

JSON →
library0.0.24pypypi✓ verified 86d ago

TextSearch is a Python library designed for efficient and convenient searching and replacing of multiple strings within text. It leverages C-speed through an Aho-Corasick implementation, making it significantly faster than equivalent regex operations for specific tasks. The library focuses on providing convenience for Natural Language Processing (NLP) and text search tasks, often defaulting to full word matches rather than sub-matches. The current version is 0.0.24, with releases appearing on an as-needed basis.

pip install textsearch
INSTALL
IMPORT
SIG · TEXTSEARCH
T
textsearch
ai-mlpythonv0.0.24
Install
1.8s avg
Import
53ms
Disk
19MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.0.24 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.055s · 20.7MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.8s · import 0.052s · 21MB
19MB installed
● package 19MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

TextSearch
from textsearch import TextSearch

This quickstart demonstrates how to initialize TextSearch, add multiple keywords, and then use it to find all occurrences or replace them within a given text. It highlights the `case` and `returns` parameters for flexible search behavior.

from textsearch import TextSearch ts = TextSearch(case="ignore", returns="match") words_to_find = ["hi", "bye", "hello"] ts.add(words_to_find) text = "Hello, hi Pascal, bye, how are you?" found_matches = ts.findall(text) print(f"Original text: {text}") print(f"Words added for search: {words_to_find}") print(f"Found matches: {found_matches}") # Example of replacement ts_replace = TextSearch(case="ignore", returns="replace") ts_replace.add("hi", "GREETING") ts_replace.add("bye", "FAREWELL") replaced_text = ts_replace.replace(text) print(f"Text after replacement: {replaced_text}")
Debug
Known issues
gotchaBy default, TextSearch focuses on full word matches (tokens). Users accustomed to standard regex might expect sub-string matches. This behavior can be configured but is a key distinction from typical regex patterns.
fix
Understand the `TextSearch` constructor parameters, especially how it processes and matches tokens. If sub-string matching is required, ensure your search terms and configuration align with that expectation, or consider alternative libraries if the core 'full word' matching is not desired.
affects: All versions
gotchaThe library relies on a C-module (`pyahocorasick`) for its performance benefits. This means installation might require a C compiler and development headers on some systems, potentially leading to build errors if not available.
fix
Ensure your system has the necessary build tools (e.g., `build-essential` on Debian/Ubuntu, Xcode command-line tools on macOS, or Visual C++ build tools on Windows) installed before attempting `pip install textsearch`.
affects: All versions
gotchaWhile TextSearch is significantly faster than regex for *multiple* string searches, for searching a *single* simple string, Python's built-in `str.find()` or `in` operator might be sufficient and have less overhead.
fix
Evaluate your use case. If you are searching for one or very few fixed strings, simpler built-in Python methods might be more appropriate. TextSearch's benefits shine when dealing with a large dictionary of terms to find or replace.
affects: All versions
Errors
Common errors & fixes
error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft Visual C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
The `textsearch` library uses C extensions for performance, requiring a C++ compiler on Windows to build during installation.
fix
Install the 'Build Tools for Visual Studio' from Microsoft's website and ensure the 'Desktop development with C++' workload is selected during installation.
AttributeError: module 'textsearch' has no attribute 'search'
The `search` method is an instance method of the `TextSearch` class, not a direct function available on the top-level `textsearch` module.
fix
First, create an instance of the `TextSearch` class with your terms, then call the `search` method on that instance:
```python
from textsearch import TextSearch
ts = TextSearch(['apple', 'banana'])
results = ts.search('I like apple and banana.')
```
TypeError: 'str' object is not iterable
The `terms` argument in the `TextSearch` constructor expects an iterable (like a list or tuple) of strings, even if you are searching for only one term.
fix
Wrap the single search term (or all terms) in a list:
```python
from textsearch import TextSearch
# Correct: pass a list of strings
ts = TextSearch(['single term'])
# Incorrect: passing a single string will raise the TypeError
# ts = TextSearch('single term')
```
Upgrade
Version history
0.0.24latest on PyPI · released Sep 2, 2022
Audit
Dependencies
pyahocorasickrequiredCore functionality relies on a C-module implementation of the Aho-Corasick algorithm for speed.
Agent activity
13 hits · last 30 days
node
10
Amazon
1
OpenAI (training)
1
Resources
textsearch — pip install textsearch · libregistry