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 textsearchVerified import paths — ran on the pinned version, not inferred.
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.
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.
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`.
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.
Install the 'Build Tools for Visual Studio' from Microsoft's website and ensure the 'Desktop development with C++' workload is selected during installation.
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.')
```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')
```