Corpus Matcher
Fast substring matching in large text corpora using optimized Levenshtein distance algorithms.
This library provides efficient fuzzy string matching capabilities, particularly useful for finding the best matching substring within large text corpora. It implements both a quick heuristic approach and a thorough search algorithm to balance speed and accuracy.
Features
- Dual Algorithm Approach: Quick heuristic matching for speed, with fallback to thorough search for accuracy
- Parallel Processing: Leverages joblib for multi-threaded processing
- Caching: Built-in result caching using joblib Memory
- Case Sensitivity Control: Optional case-sensitive or case-insensitive matching
- Configurable Parameters: Adjustable step factors and search granularity
Installation
pip install corpus-matcher
Quick Start
from corpus_matcher import find_best_substring_match
# Basic usage
query = "machine learning algorithms"
corpus = "This document discusses various machine learning algorithms and their applications in data science."
result = find_best_substring_match(query, corpus)
print(f"Best matches: {result.matches}")
print(f"Similarity ratio: {result.ratio}")
print(f"Distance: {result.distance}")
print(f"Quick match used: {result.quick_match_used}")
Advanced Usage
from corpus_matcher import find_best_substring_match
# Case-insensitive matching with custom parameters
result = find_best_substring_match(
query="PYTHON programming",
corpus="Learn python programming from basics to advanced concepts",
case_sensitive=False,
step_factor=300, # Higher step factor for more thorough search
n_jobs=4 # Use 4 parallel jobs
)
print(f"Matches: {result.matches}")
print(f"Ratio: {result.ratio:.3f}")
API Reference
find_best_substring_match(query, corpus, case_sensitive=True, step_factor=500, n_jobs=-1)
Find the best matching substring(s) in a corpus for a given query.
Parameters:
query(str): The text to search forcorpus(str): The text to search withincase_sensitive(bool, optional): Whether matching should be case-sensitive. Default: Truestep_factor(int, optional): Controls search resolution. Higher values = more thorough search. Default: 500n_jobs(int, optional): Number of parallel jobs (-1 for all available cores). Default: -1
Returns:
MatchResult: Object containing matches, ratio, distance, and algorithm info
MatchResult
A dataclass containing the results of the matching operation:
matches(List[str]): List of best matching substringsratio(float): Levenshtein similarity ratio (0-100)distance(float): Normalized Levenshtein distance (0-1)quick_match_used(bool): Whether the quick algorithm was sufficient
Algorithm Details
The library uses a two-stage approach:
- Quick Match: Identifies potential regions using word-based heuristics, then performs localized search
- Thorough Search: Falls back to comprehensive n-gram analysis if quick match fails
This approach provides good performance for most use cases while maintaining accuracy.
Requirements
- Python ≥ 3.8
- joblib
- rapidfuzz
Development
This project was developed with assistance from aider.chat.
License
GPL-v3
Metadata
Release files for corpus-matcher 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| corpus_matcher-1.0.0.tar.gz | 23.2 kB | Without provenance | Details for corpus_matcher-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| corpus_matcher-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for corpus_matcher-1.0.0-py3-none-any.whl |
Total release size: 44.2 kB
Release files / corpus_matcher-1.0.0.tar.gz
| Download URL | corpus_matcher-1.0.0.tar.gz |
|---|---|
| Size | 23.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c2b18364e2399dba014a2769d4ab0d2f93ed39f1bd99b677f7383bb32c8ed6c4
|
|
BLAKE2b-256 checksum How to use checksums |
6e4263204a0a9306df8700d1748960619ed7b3bc672bda4aef13a2c30895ec5a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.11
|
Release files / corpus_matcher-1.0.0-py3-none-any.whl
| Download URL | corpus_matcher-1.0.0-py3-none-any.whl |
|---|---|
| Size | 21.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d0ec9df0ef15d0cd6dc711f97cb90e9517b40b12d9bb440d11b7f30461a0c4a6
|
|
BLAKE2b-256 checksum How to use checksums |
0103197fa2e1254365f1b65eca0fc872e9846b89706c06b1b7bc6f19330049d5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.11
|
Security for corpus-matcher 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.