blitztext 0.1.0__cp38-none-win32.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
blitztext/__init__.py ADDED
@@ -0,0 +1,5 @@
1
+ from .blitztext import *
2
+
3
+ __doc__ = blitztext.__doc__
4
+ if hasattr(blitztext, "__all__"):
5
+ __all__ = blitztext.__all__
blitztext/__init__.pyi ADDED
@@ -0,0 +1,159 @@
1
+ from typing import Dict, List, Optional, Tuple
2
+
3
+ class KeywordMatch:
4
+ """
5
+ Represents a matched keyword in the text.
6
+
7
+ Attributes:
8
+ keyword (str): The matched keyword or its clean name.
9
+ similarity (float): The similarity score of the match (1.0 for exact matches).
10
+ start (int): The starting index of the match in the text.
11
+ end (int): The ending index of the match in the text.
12
+ """
13
+ keyword: str
14
+ similarity: float
15
+ start: int
16
+ end: int
17
+
18
+ def __repr__(self) -> str:
19
+ """
20
+ Returns a string representation of the KeywordMatch object.
21
+ """
22
+ ...
23
+
24
+ class KeywordProcessor:
25
+ """
26
+ A processor for efficient keyword matching and replacement in text.
27
+
28
+ Based on AhoCorasick for fast keyword operations,
29
+ including fuzzy matching capabilities.
30
+ """
31
+
32
+ def __init__(self, case_sensitive: Optional[bool] = None, allow_overlaps: Optional[bool] = None) -> None:
33
+ """
34
+ Initialize a new KeywordProcessor.
35
+
36
+ Args:
37
+ case_sensitive: If True, matching will be case-sensitive. Default is False.
38
+ allow_overlaps: If True, allows overlapping matches. Default is True.
39
+ """
40
+ ...
41
+
42
+ def add_keyword(self, keyword: str, clean_name: Optional[str] = None) -> None:
43
+ """
44
+ Add a keyword to the processor.
45
+
46
+ Args:
47
+ keyword: The keyword to add.
48
+ clean_name: An optional 'clean' version of the keyword to return when found.
49
+ """
50
+ ...
51
+
52
+ def remove_keyword(self, keyword: str) -> bool:
53
+ """
54
+ Remove a keyword from the processor.
55
+
56
+ Args:
57
+ keyword: The keyword to remove.
58
+
59
+ Returns:
60
+ True if the keyword was found and removed, False otherwise.
61
+ """
62
+ ...
63
+
64
+ def extract_keywords(self, text: str, threshold: Optional[float] = None) -> List[KeywordMatch]:
65
+ """
66
+ Extract keywords from the given text.
67
+
68
+ Args:
69
+ text: The text to search for keywords.
70
+ threshold: Threshold for fuzzy matching (0.0 to 1.0). Default is 1.0 (exact matches only).
71
+
72
+ Returns:
73
+ A list of KeywordMatch objects representing the found keywords.
74
+ """
75
+ ...
76
+
77
+ def parallel_extract_keywords_from_texts(self, texts: List[str], threshold: Optional[float] = None) -> List[List[KeywordMatch]]:
78
+ """
79
+ Extract keywords from multiple texts in parallel.
80
+
81
+ Args:
82
+ texts: A list of texts to search for keywords.
83
+ threshold: Threshold for fuzzy matching (0.0 to 1.0). Default is 1.0 (exact matches only).
84
+
85
+ Returns:
86
+ A list of lists of KeywordMatch objects, one list per input text.
87
+ """
88
+ ...
89
+
90
+ def replace_keywords(self, text: str, threshold: Optional[float] = None) -> str:
91
+ """
92
+ Replace keywords in the given text with their clean names.
93
+
94
+ Args:
95
+ text: The text in which to replace keywords.
96
+ threshold: Threshold for fuzzy matching (0.0 to 1.0). Default is 1.0 (exact matches only).
97
+
98
+ Returns:
99
+ The text with keywords replaced by their clean names.
100
+ """
101
+ ...
102
+
103
+ def parallel_replace_keywords_from_texts(self, texts: List[str], threshold: Optional[float] = None) -> List[str]:
104
+ """
105
+ Replace keywords in multiple texts in parallel.
106
+
107
+ Args:
108
+ texts: A list of texts in which to replace keywords.
109
+ threshold: Threshold for fuzzy matching (0.0 to 1.0). Default is 1.0 (exact matches only).
110
+
111
+ Returns:
112
+ A list of strings with keywords replaced by their clean names.
113
+ """
114
+ ...
115
+
116
+ def get_all_keywords(self) -> Dict[str, str]:
117
+ """
118
+ Get all keywords and their clean names.
119
+
120
+ Returns:
121
+ A dictionary where keys are keywords and values are their clean names.
122
+ """
123
+ ...
124
+
125
+ def set_non_word_boundaries(self, boundaries: List[str]) -> None:
126
+ """
127
+ Set the characters that are considered part of a word.
128
+
129
+ Args:
130
+ boundaries: A list of characters to be set as non-word boundaries.
131
+ """
132
+ ...
133
+
134
+ def add_non_word_boundary(self, boundary: str) -> None:
135
+ """
136
+ Add a single character as a non-word boundary.
137
+
138
+ Args:
139
+ boundary: The character to be added as a non-word boundary.
140
+ """
141
+ ...
142
+
143
+ def __len__(self) -> int:
144
+ """
145
+ Get the number of keywords in the processor.
146
+
147
+ Returns:
148
+ The number of keywords.
149
+ """
150
+ ...
151
+
152
+ def __repr__(self) -> str:
153
+ """
154
+ Get a string representation of the KeywordProcessor.
155
+
156
+ Returns:
157
+ A string representation including the number of keywords.
158
+ """
159
+ ...
Binary file
blitztext/py.typed ADDED
File without changes
@@ -0,0 +1,282 @@
1
+ Metadata-Version: 2.3
2
+ Name: blitztext
3
+ Version: 0.1.0
4
+ Classifier: Programming Language :: Rust
5
+ Classifier: Programming Language :: Python :: Implementation :: CPython
6
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
7
+ License-File: LICENSE
8
+ Summary: A library for fast keywords extraction and replacement in strings.
9
+ Keywords: keyword,search,fuzzy,trie,aho-corasick
10
+ Author: Praise Oketola <oketola.praise@gmail.com>
11
+ Author-email: Praise Oketola <oketola.praise@gmail.com>
12
+ License: MIT
13
+ Requires-Python: >=3.7
14
+ Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
15
+ Project-URL: Source Code, https://github.com/praise2112/blitztext
16
+
17
+ # BlitzText
18
+
19
+ BlitzText is a high-performance library for efficient keyword extraction and replacement in strings. It is based on the [FlashText](https://github.com/vi3k6i5/flashtext) and [Aho-Corasick](https://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_algorithm) algorithm. There are both Rust and Python implementations.
20
+ Main difference form Aho-Corasick is that BlitzText only matches the longest pattern in a greedy manner.
21
+
22
+ <img src="benches/benchmark_results_single.png">
23
+
24
+ ## Table of Contents
25
+ - [Installation](#installation)
26
+ - [Usage](#usage)
27
+ - [Rust Usage](#rust-usage)
28
+ - [Python Usage](#python-usage)
29
+ - [Features](#Features)
30
+ - [1. Parallel Processing](#1-parallel-processing)
31
+ - [2. Fuzzy Matching](#2-fuzzy-matching)
32
+ - [3. Case Sensitivity](#3-case-sensitivity)
33
+ - [4. Overlapping Matches](#4-overlapping-matches)
34
+ - [5. Custom Non-Word Boundaries](#5-custom-non-word-boundaries)
35
+ - [Performance](#performance)
36
+ - [Contributing](#contributing)
37
+ - [License](#license)
38
+
39
+
40
+ ## Installation
41
+
42
+ ### Rust
43
+
44
+ Add this to your `Cargo.toml`:
45
+
46
+ ```toml
47
+ [dependencies]
48
+ blitztext = "0.1.0"
49
+ ```
50
+
51
+ or
52
+
53
+ ```shell
54
+ cargo add blitztext
55
+ ```
56
+
57
+
58
+ ### Python
59
+
60
+ Install the library using pip:
61
+
62
+ ```
63
+ pip install blitztext
64
+ ```
65
+
66
+ ## Usage
67
+
68
+ ### Rust Usage
69
+
70
+ ```rust
71
+ use blitztext::KeywordProcessor;
72
+
73
+ fn main() {
74
+ let mut processor = KeywordProcessor::new();
75
+ processor.add_keyword("rust", Some("Rust Lang"));
76
+ processor.add_keyword("programming", Some("Coding"));
77
+
78
+ let text = "I love rust programming";
79
+ let matches = processor.extract_keywords(text, None);
80
+
81
+ for m in matches {
82
+ println!("Found '{}' at [{}, {}]", m.keyword, m.start, m.end);
83
+ }
84
+
85
+ let replaced = processor.replace_keywords(text, None);
86
+ println!("Replaced text: {}", replaced);
87
+ // Output: "I love Rust Lang Coding"
88
+ }
89
+ ```
90
+
91
+ ### Python Usage
92
+
93
+ ```python
94
+ from blitztext import KeywordProcessor
95
+
96
+ processor = KeywordProcessor()
97
+ processor.add_keyword("rust", "Rust Lang")
98
+ processor.add_keyword("programming", "Coding")
99
+
100
+ text = "I love rust programming"
101
+ matches = processor.extract_keywords(text)
102
+
103
+ for m in matches:
104
+ print(f"Found '{m.keyword}' at [{m.start}, {m.end}]")
105
+
106
+ replaced = processor.replace_keywords(text)
107
+ // Output: "I love Rust Lang Coding"
108
+
109
+ print(f"Replaced text: {replaced}")
110
+ ```
111
+
112
+ ## Features
113
+
114
+ ### 1. Parallel Processing
115
+
116
+ For processing multiple texts in parallel:
117
+
118
+ ```rust
119
+ // Rust
120
+ let texts = vec!["Text 1", "Text 2", "Text 3"];
121
+ let results = processor.parallel_extract_keywords_from_texts(&texts, None);
122
+ ```
123
+
124
+ ```python
125
+ # Python
126
+ texts = ["Text 1", "Text 2", "Text 3"]
127
+ results = processor.parallel_extract_keywords_from_texts(texts)
128
+ ```
129
+
130
+
131
+ ### 2. Fuzzy Matching
132
+
133
+ Both Rust and Python implementations support fuzzy matching:
134
+
135
+ ```rust
136
+ // Rust
137
+ let matches = processor.extract_keywords(text, Some(0.8));
138
+ ```
139
+
140
+ ```python
141
+ # Python
142
+ matches = processor.extract_keywords(text, threshold=0.8)
143
+ ```
144
+
145
+ ### 3. Case Sensitivity
146
+
147
+ You can enable case-sensitive matching:
148
+
149
+ ```rust
150
+ // Rust
151
+ let mut processor = KeywordProcessor::with_options(true, false);
152
+ processor.add_keyword("Rust", Some("Rust Lang"));
153
+ let matches = processor.extract_keywords("I love Rust and rust", None);
154
+ // Only "Rust" will be matched, not "rust"
155
+ ```
156
+
157
+ ```python
158
+ # Python
159
+ processor = KeywordProcessor(case_sensitive=True)
160
+ processor.add_keyword("Rust", "Rust Lang")
161
+ matches = processor.extract_keywords("I love Rust and rust")
162
+ # Only "Rust" will be matched, not "rust"
163
+ ```
164
+
165
+ ### 4. Overlapping Matches
166
+
167
+ Enable overlapping matches:
168
+
169
+ ```rust
170
+ // Rust
171
+ let mut processor = KeywordProcessor::with_options(false, true);
172
+ processor.add_keyword("word", None);
173
+ processor.add_keyword("sword", None);
174
+ let matches = processor.extract_keywords("I have a sword", None);
175
+ // "word" will be matched
176
+ ```
177
+
178
+ ```python
179
+ # Python
180
+ processor = KeywordProcessor(allow_overlaps=True)
181
+ processor.add_keyword("word")
182
+ matches = processor.extract_keywords("I have a sword")
183
+ # "word" will be matched
184
+ ```
185
+
186
+
187
+ ### 5. Custom Non-Word Boundaries
188
+
189
+ This library uses the concept of non-word boundaries to determine where words begin and end. By default, alphanumeric characters and underscores are considered part of a word. You can customize this behavior to fit your specific needs.
190
+
191
+ #### Understanding Non-Word Boundaries
192
+
193
+ - Characters defined as non-word boundaries are considered part of a word.
194
+ - Characters not defined as non-word boundaries are treated as word separators.
195
+
196
+ #### Example
197
+
198
+ ```rust
199
+ // Rust
200
+ let mut processor = KeywordProcessor::new();
201
+
202
+ processor.add_keyword("rust", None);
203
+ processor.add_keyword("programming", Some("coding"));
204
+
205
+ let text = "I-love-rust-programming-and-1coding2";
206
+
207
+ // Default behavior: '-' is a word separator
208
+ let matches = processor.extract_keywords(text, None);
209
+ assert_eq!(matches.len(), 2);
210
+ // Matches: "rust" and "coding"
211
+
212
+ // Add '-' as a non-word boundary
213
+ processor.add_non_word_boundary('-');
214
+
215
+ // Now '-' is considered part of words
216
+ let matches = processor.extract_keywords(text, None);
217
+ assert_eq!(matches.len(), 0);
218
+ // No matches, because "rust" and "programming" are now part of larger "words"
219
+ ```
220
+
221
+ ```python
222
+ # Python
223
+ processor = KeywordProcessor()
224
+
225
+ processor.add_keyword("rust")
226
+ processor.add_keyword("programming", "coding")
227
+
228
+ text = "I-love-rust-programming-and-1coding2"
229
+
230
+ # Default behavior: '-' is a word separator
231
+ matches = processor.extract_keywords(text)
232
+ assert len(matches) == 2
233
+ # Matches: "rust" and "coding"
234
+
235
+ # Add '-' as a non-word boundary
236
+ processor.add_non_word_boundary('-')
237
+
238
+ # Now '-' is considered part of words
239
+ matches = processor.extract_keywords(text)
240
+ assert len(matches) == 0
241
+ # No matches, because "rust" and "programming" are now part of larger "words"
242
+ ```
243
+
244
+ #### Setting a whole new set of non-word boundaries
245
+
246
+ ```rust
247
+ // Rust
248
+ processor.set_non_word_boundaries(&['-', '_', '@']);
249
+ ```
250
+
251
+ ```python
252
+ # Python
253
+ processor.set_non_word_boundaries(['-', '_', '@'])
254
+ ```
255
+
256
+
257
+ ## Performance
258
+
259
+ ---
260
+
261
+ BlitzText is designed for high performance, making it suitable for processing large volumes of text.
262
+ Benchmark details [here](benches/benchmark_results.md).
263
+
264
+ Mult-threaded performance:
265
+ <img src="benches/benchmark_results_multi.png">
266
+
267
+ ## Contributing
268
+
269
+ ---
270
+
271
+ Contributions are welcome! Please feel free to submit a Pull Request.
272
+
273
+ ## Issues
274
+ If you encounter any problems, please file an [issue](https://github.com/praise2112/blitztext/issues) along with a detailed description.
275
+
276
+ ## License
277
+
278
+ ---
279
+
280
+ This project is licensed under the [MIT License](https://github.com/praise2112/blitztext/blob/main/LICENSE).
281
+
282
+
@@ -0,0 +1,8 @@
1
+ blitztext-0.1.0.dist-info/METADATA,sha256=Q7EzYeFF_Xt8cI6rJPIIusW0VAh07OVRxwXcYAzxTLo,7388
2
+ blitztext-0.1.0.dist-info/WHEEL,sha256=efqlbyoWVaRpv3_pmcpfQMmn5BxUR3OkPLmrUDmnQP0,90
3
+ blitztext-0.1.0.dist-info/license_files/LICENSE,sha256=M96WJMlfMKb2MSk9-Wnb432DqTdlu9qQiuLS0vutwEY,1119
4
+ blitztext/__init__.py,sha256=pOLO1hwZxoTihScRSVIFZYggcF89bbSUaeAETVbzv5E,119
5
+ blitztext/__init__.pyi,sha256=edWR9hXhqqGt3RDrMYevMT5rziujUWsq8I8fmK-blyk,4970
6
+ blitztext/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
+ blitztext/blitztext.cp38-win32.pyd,sha256=ZEpE-3Kg76vw7uMBiyGn37gjFj00sh3w79FLoBoL2gU,399360
8
+ blitztext-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: maturin (1.7.0)
3
+ Root-Is-Purelib: false
4
+ Tag: cp38-none-win32
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Praise Oketola (oketola.praise@gmail.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.