fast-sentence-segment 0.1.9__tar.gz → 1.4.0__tar.gz
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.
- fast_sentence_segment-1.4.0/LICENSE +21 -0
- fast_sentence_segment-1.4.0/PKG-INFO +222 -0
- fast_sentence_segment-1.4.0/README.md +190 -0
- fast_sentence_segment-1.4.0/fast_sentence_segment/__init__.py +51 -0
- {fast-sentence-segment-0.1.9 → fast_sentence_segment-1.4.0}/fast_sentence_segment/bp/__init__.py +1 -1
- {fast-sentence-segment-0.1.9 → fast_sentence_segment-1.4.0}/fast_sentence_segment/bp/segmenter.py +65 -68
- fast_sentence_segment-1.4.0/fast_sentence_segment/cli.py +149 -0
- fast_sentence_segment-1.4.0/fast_sentence_segment/core/__init__.py +4 -0
- fast_sentence_segment-1.4.0/fast_sentence_segment/core/base_object.py +18 -0
- fast_sentence_segment-1.4.0/fast_sentence_segment/core/stopwatch.py +38 -0
- fast_sentence_segment-1.4.0/fast_sentence_segment/dmo/__init__.py +15 -0
- fast_sentence_segment-1.4.0/fast_sentence_segment/dmo/abbreviation_merger.py +146 -0
- fast_sentence_segment-1.4.0/fast_sentence_segment/dmo/abbreviation_splitter.py +95 -0
- fast_sentence_segment-1.4.0/fast_sentence_segment/dmo/abbreviations.py +149 -0
- {fast-sentence-segment-0.1.9 → fast_sentence_segment-1.4.0}/fast_sentence_segment/dmo/bullet_point_cleaner.py +55 -55
- fast_sentence_segment-1.4.0/fast_sentence_segment/dmo/dehyphenator.py +55 -0
- fast_sentence_segment-1.4.0/fast_sentence_segment/dmo/ellipsis_normalizer.py +45 -0
- fast_sentence_segment-1.4.0/fast_sentence_segment/dmo/group_quoted_sentences.py +141 -0
- {fast-sentence-segment-0.1.9 → fast_sentence_segment-1.4.0}/fast_sentence_segment/dmo/newlines_to_periods.py +57 -57
- fast_sentence_segment-1.4.0/fast_sentence_segment/dmo/normalize_quotes.py +80 -0
- fast_sentence_segment-1.4.0/fast_sentence_segment/dmo/numbered_list_normalizer.py +47 -0
- {fast-sentence-segment-0.1.9 → fast_sentence_segment-1.4.0}/fast_sentence_segment/dmo/post_process_sentences.py +48 -48
- fast_sentence_segment-1.4.0/fast_sentence_segment/dmo/question_exclamation_splitter.py +59 -0
- {fast-sentence-segment-0.1.9 → fast_sentence_segment-1.4.0}/fast_sentence_segment/dmo/spacy_doc_segmenter.py +114 -101
- fast_sentence_segment-1.4.0/fast_sentence_segment/dmo/strip_trailing_period_after_quote.py +70 -0
- fast_sentence_segment-1.4.0/fast_sentence_segment/dmo/title_name_merger.py +152 -0
- fast_sentence_segment-1.4.0/fast_sentence_segment/dmo/unwrap_hard_wrapped_text.py +75 -0
- {fast-sentence-segment-0.1.9 → fast_sentence_segment-1.4.0}/fast_sentence_segment/svc/__init__.py +2 -2
- {fast-sentence-segment-0.1.9 → fast_sentence_segment-1.4.0}/fast_sentence_segment/svc/perform_paragraph_segmentation.py +50 -50
- fast_sentence_segment-1.4.0/fast_sentence_segment/svc/perform_sentence_segmentation.py +176 -0
- fast_sentence_segment-1.4.0/pyproject.toml +65 -0
- fast_sentence_segment-1.4.0/setup.py +39 -0
- fast-sentence-segment-0.1.9/PKG-INFO +0 -54
- fast-sentence-segment-0.1.9/README.md +0 -29
- fast-sentence-segment-0.1.9/fast_sentence_segment/__init__.py +0 -18
- fast-sentence-segment-0.1.9/fast_sentence_segment/dmo/__init__.py +0 -6
- fast-sentence-segment-0.1.9/fast_sentence_segment/dmo/delimiters_to_periods.py +0 -37
- fast-sentence-segment-0.1.9/fast_sentence_segment/dmo/numbered_list_normalizer.py +0 -53
- fast-sentence-segment-0.1.9/fast_sentence_segment/svc/perform_sentence_segmentation.py +0 -129
- fast-sentence-segment-0.1.9/pyproject.toml +0 -61
- fast-sentence-segment-0.1.9/setup.py +0 -33
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Craig Trim
|
|
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.
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: fast-sentence-segment
|
|
3
|
+
Version: 1.4.0
|
|
4
|
+
Summary: Fast and Efficient Sentence Segmentation
|
|
5
|
+
Home-page: https://github.com/craigtrim/fast-sentence-segment
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: nlp,text,preprocess,segment
|
|
8
|
+
Author: Craig Trim
|
|
9
|
+
Author-email: craigtrim@gmail.com
|
|
10
|
+
Maintainer: Craig Trim
|
|
11
|
+
Maintainer-email: craigtrim@gmail.com
|
|
12
|
+
Requires-Python: >=3.9,<4.0
|
|
13
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Intended Audience :: Science/Research
|
|
16
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
24
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
25
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
26
|
+
Classifier: Typing :: Typed
|
|
27
|
+
Requires-Dist: spacy (>=3.8.0,<4.0.0)
|
|
28
|
+
Project-URL: Bug Tracker, https://github.com/craigtrim/fast-sentence-segment/issues
|
|
29
|
+
Project-URL: Repository, https://github.com/craigtrim/fast-sentence-segment
|
|
30
|
+
Description-Content-Type: text/markdown
|
|
31
|
+
|
|
32
|
+
# Fast Sentence Segmentation
|
|
33
|
+
|
|
34
|
+
[](https://pypi.org/project/fast-sentence-segment/)
|
|
35
|
+
[](https://pypi.org/project/fast-sentence-segment/)
|
|
36
|
+
[](https://github.com/craigtrim/fast-sentence-segment/actions/workflows/ci.yml)
|
|
37
|
+
[](https://opensource.org/licenses/MIT)
|
|
38
|
+
[](https://github.com/astral-sh/ruff)
|
|
39
|
+
[](https://pepy.tech/project/fast-sentence-segment)
|
|
40
|
+
[](https://pepy.tech/project/fast-sentence-segment)
|
|
41
|
+
|
|
42
|
+
Fast and efficient sentence segmentation using spaCy with surgical post-processing fixes. Handles complex edge cases like abbreviations (Dr., Mr., etc.), ellipses, quoted text, and multi-paragraph documents.
|
|
43
|
+
|
|
44
|
+
## Why This Library?
|
|
45
|
+
|
|
46
|
+
1. **Keep it local**: LLM API calls cost money and send your data to third parties. Run sentence segmentation entirely on your machine.
|
|
47
|
+
2. **spaCy perfected**: spaCy is a great local model, but it makes mistakes. This library fixes most of spaCy's shortcomings.
|
|
48
|
+
|
|
49
|
+
## Features
|
|
50
|
+
|
|
51
|
+
- **Paragraph-aware segmentation**: Returns sentences grouped by paragraph
|
|
52
|
+
- **Abbreviation handling**: Correctly handles "Dr.", "Mr.", "etc.", "p.m.", "a.m." without false splits
|
|
53
|
+
- **Ellipsis preservation**: Keeps `...` intact while detecting sentence boundaries
|
|
54
|
+
- **Question/exclamation splitting**: Properly splits on `?` and `!` followed by capital letters
|
|
55
|
+
- **Cached processing**: LRU cache for repeated text processing
|
|
56
|
+
- **Flexible output**: Nested lists (by paragraph) or flattened list of sentences
|
|
57
|
+
- **Bullet point & numbered list normalization**: Cleans common list formats
|
|
58
|
+
- **CLI tool**: Command-line interface for quick segmentation
|
|
59
|
+
|
|
60
|
+
## Installation
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
pip install fast-sentence-segment
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
After installation, download the spaCy model:
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
python -m spacy download en_core_web_sm
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Quick Start
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
from fast_sentence_segment import segment_text
|
|
76
|
+
|
|
77
|
+
text = "Do you like Dr. Who? I prefer Dr. Strange! Mr. T is also cool."
|
|
78
|
+
|
|
79
|
+
results = segment_text(text, flatten=True)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
```json
|
|
83
|
+
[
|
|
84
|
+
"Do you like Dr. Who?",
|
|
85
|
+
"I prefer Dr. Strange!",
|
|
86
|
+
"Mr. T is also cool."
|
|
87
|
+
]
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Notice how "Dr. Who?" stays together as a single sentence—the library correctly recognizes that a title followed by a single-word name ending in `?` or `!` is a name reference, not a sentence boundary.
|
|
91
|
+
|
|
92
|
+
## Usage
|
|
93
|
+
|
|
94
|
+
### Basic Segmentation
|
|
95
|
+
|
|
96
|
+
The `segment_text` function returns a list of lists, where each inner list represents a paragraph containing its sentences:
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
from fast_sentence_segment import segment_text
|
|
100
|
+
|
|
101
|
+
text = """Gandalf spoke softly. "All we have to decide is what to do with the time given us."
|
|
102
|
+
|
|
103
|
+
Frodo nodded. The weight of the Ring pressed against his chest."""
|
|
104
|
+
|
|
105
|
+
results = segment_text(text)
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
```json
|
|
109
|
+
[
|
|
110
|
+
[
|
|
111
|
+
"Gandalf spoke softly.",
|
|
112
|
+
"\"All we have to decide is what to do with the time given us.\"."
|
|
113
|
+
],
|
|
114
|
+
[
|
|
115
|
+
"Frodo nodded.",
|
|
116
|
+
"The weight of the Ring pressed against his chest."
|
|
117
|
+
]
|
|
118
|
+
]
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Flattened Output
|
|
122
|
+
|
|
123
|
+
If you don't need paragraph boundaries, use the `flatten` parameter:
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
text = "At 9 a.m. the hobbits set out. By 3 p.m. they reached Rivendell. Mr. Frodo was exhausted."
|
|
127
|
+
|
|
128
|
+
results = segment_text(text, flatten=True)
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
```json
|
|
132
|
+
[
|
|
133
|
+
"At 9 a.m. the hobbits set out.",
|
|
134
|
+
"By 3 p.m. they reached Rivendell.",
|
|
135
|
+
"Mr. Frodo was exhausted."
|
|
136
|
+
]
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Direct Segmenter Access
|
|
140
|
+
|
|
141
|
+
For more control, use the `Segmenter` class directly:
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
from fast_sentence_segment import Segmenter
|
|
145
|
+
|
|
146
|
+
segmenter = Segmenter()
|
|
147
|
+
results = segmenter.input_text("Your text here.")
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Command Line Interface
|
|
151
|
+
|
|
152
|
+
```bash
|
|
153
|
+
# Inline text
|
|
154
|
+
segment "Gandalf paused... You shall not pass! The Balrog roared."
|
|
155
|
+
|
|
156
|
+
# Pipe from stdin
|
|
157
|
+
echo "Have you seen Dr. Who? It's brilliant!" | segment
|
|
158
|
+
|
|
159
|
+
# Numbered output
|
|
160
|
+
segment -n -f silmarillion.txt
|
|
161
|
+
|
|
162
|
+
# File-to-file (one sentence per line)
|
|
163
|
+
segment-file --input-file book.txt --output-file sentences.txt
|
|
164
|
+
|
|
165
|
+
# Unwrap hard-wrapped e-texts (Project Gutenberg, etc.)
|
|
166
|
+
segment-file --input-file book.txt --output-file sentences.txt --unwrap
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## API Reference
|
|
170
|
+
|
|
171
|
+
| Function | Parameters | Returns | Description |
|
|
172
|
+
|----------|------------|---------|-------------|
|
|
173
|
+
| `segment_text()` | `input_text: str`, `flatten: bool = False`, `unwrap: bool = False` | `list` | Main entry point for segmentation |
|
|
174
|
+
| `Segmenter.input_text()` | `input_text: str` | `list[list[str]]` | Cached paragraph-aware segmentation |
|
|
175
|
+
|
|
176
|
+
### CLI Commands
|
|
177
|
+
|
|
178
|
+
| Command | Description |
|
|
179
|
+
|---------|-------------|
|
|
180
|
+
| `segment [text]` | Segment text from argument, `-f FILE`, or stdin. Use `-n` for numbered output. |
|
|
181
|
+
| `segment-file --input-file IN --output-file OUT [--unwrap]` | Segment a file and write one sentence per line. Use `--unwrap` for hard-wrapped e-texts. |
|
|
182
|
+
|
|
183
|
+
## Why Nested Lists?
|
|
184
|
+
|
|
185
|
+
The segmentation process preserves document structure by segmenting into both paragraphs and sentences. Each outer list represents a paragraph, and each inner list contains that paragraph's sentences. This is useful for:
|
|
186
|
+
|
|
187
|
+
- Document structure analysis
|
|
188
|
+
- Paragraph-level processing
|
|
189
|
+
- Maintaining original text organization
|
|
190
|
+
|
|
191
|
+
Use `flatten=True` when you only need sentences without paragraph context.
|
|
192
|
+
|
|
193
|
+
## Requirements
|
|
194
|
+
|
|
195
|
+
- Python 3.9+
|
|
196
|
+
- spaCy 3.8+
|
|
197
|
+
- en_core_web_sm spaCy model
|
|
198
|
+
|
|
199
|
+
## How It Works
|
|
200
|
+
|
|
201
|
+
This library uses spaCy for initial sentence segmentation, then applies surgical post-processing fixes for cases where spaCy's default behavior is incorrect:
|
|
202
|
+
|
|
203
|
+
1. **Pre-processing**: Normalize numbered lists, preserve ellipses with placeholders
|
|
204
|
+
2. **spaCy segmentation**: Use spaCy's sentence boundary detection
|
|
205
|
+
3. **Post-processing**: Split on abbreviation boundaries, handle `?`/`!` + capital patterns
|
|
206
|
+
4. **Denormalization**: Restore placeholders to original text
|
|
207
|
+
|
|
208
|
+
## License
|
|
209
|
+
|
|
210
|
+
MIT License - see [LICENSE](LICENSE) for details.
|
|
211
|
+
|
|
212
|
+
## Contributing
|
|
213
|
+
|
|
214
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
215
|
+
|
|
216
|
+
1. Fork the repository
|
|
217
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
218
|
+
3. Run tests (`make test`)
|
|
219
|
+
4. Commit your changes
|
|
220
|
+
5. Push to the branch
|
|
221
|
+
6. Open a Pull Request
|
|
222
|
+
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
# Fast Sentence Segmentation
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/fast-sentence-segment/)
|
|
4
|
+
[](https://pypi.org/project/fast-sentence-segment/)
|
|
5
|
+
[](https://github.com/craigtrim/fast-sentence-segment/actions/workflows/ci.yml)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
[](https://github.com/astral-sh/ruff)
|
|
8
|
+
[](https://pepy.tech/project/fast-sentence-segment)
|
|
9
|
+
[](https://pepy.tech/project/fast-sentence-segment)
|
|
10
|
+
|
|
11
|
+
Fast and efficient sentence segmentation using spaCy with surgical post-processing fixes. Handles complex edge cases like abbreviations (Dr., Mr., etc.), ellipses, quoted text, and multi-paragraph documents.
|
|
12
|
+
|
|
13
|
+
## Why This Library?
|
|
14
|
+
|
|
15
|
+
1. **Keep it local**: LLM API calls cost money and send your data to third parties. Run sentence segmentation entirely on your machine.
|
|
16
|
+
2. **spaCy perfected**: spaCy is a great local model, but it makes mistakes. This library fixes most of spaCy's shortcomings.
|
|
17
|
+
|
|
18
|
+
## Features
|
|
19
|
+
|
|
20
|
+
- **Paragraph-aware segmentation**: Returns sentences grouped by paragraph
|
|
21
|
+
- **Abbreviation handling**: Correctly handles "Dr.", "Mr.", "etc.", "p.m.", "a.m." without false splits
|
|
22
|
+
- **Ellipsis preservation**: Keeps `...` intact while detecting sentence boundaries
|
|
23
|
+
- **Question/exclamation splitting**: Properly splits on `?` and `!` followed by capital letters
|
|
24
|
+
- **Cached processing**: LRU cache for repeated text processing
|
|
25
|
+
- **Flexible output**: Nested lists (by paragraph) or flattened list of sentences
|
|
26
|
+
- **Bullet point & numbered list normalization**: Cleans common list formats
|
|
27
|
+
- **CLI tool**: Command-line interface for quick segmentation
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pip install fast-sentence-segment
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
After installation, download the spaCy model:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
python -m spacy download en_core_web_sm
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Quick Start
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from fast_sentence_segment import segment_text
|
|
45
|
+
|
|
46
|
+
text = "Do you like Dr. Who? I prefer Dr. Strange! Mr. T is also cool."
|
|
47
|
+
|
|
48
|
+
results = segment_text(text, flatten=True)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
```json
|
|
52
|
+
[
|
|
53
|
+
"Do you like Dr. Who?",
|
|
54
|
+
"I prefer Dr. Strange!",
|
|
55
|
+
"Mr. T is also cool."
|
|
56
|
+
]
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Notice how "Dr. Who?" stays together as a single sentence—the library correctly recognizes that a title followed by a single-word name ending in `?` or `!` is a name reference, not a sentence boundary.
|
|
60
|
+
|
|
61
|
+
## Usage
|
|
62
|
+
|
|
63
|
+
### Basic Segmentation
|
|
64
|
+
|
|
65
|
+
The `segment_text` function returns a list of lists, where each inner list represents a paragraph containing its sentences:
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
from fast_sentence_segment import segment_text
|
|
69
|
+
|
|
70
|
+
text = """Gandalf spoke softly. "All we have to decide is what to do with the time given us."
|
|
71
|
+
|
|
72
|
+
Frodo nodded. The weight of the Ring pressed against his chest."""
|
|
73
|
+
|
|
74
|
+
results = segment_text(text)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
```json
|
|
78
|
+
[
|
|
79
|
+
[
|
|
80
|
+
"Gandalf spoke softly.",
|
|
81
|
+
"\"All we have to decide is what to do with the time given us.\"."
|
|
82
|
+
],
|
|
83
|
+
[
|
|
84
|
+
"Frodo nodded.",
|
|
85
|
+
"The weight of the Ring pressed against his chest."
|
|
86
|
+
]
|
|
87
|
+
]
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Flattened Output
|
|
91
|
+
|
|
92
|
+
If you don't need paragraph boundaries, use the `flatten` parameter:
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
text = "At 9 a.m. the hobbits set out. By 3 p.m. they reached Rivendell. Mr. Frodo was exhausted."
|
|
96
|
+
|
|
97
|
+
results = segment_text(text, flatten=True)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
```json
|
|
101
|
+
[
|
|
102
|
+
"At 9 a.m. the hobbits set out.",
|
|
103
|
+
"By 3 p.m. they reached Rivendell.",
|
|
104
|
+
"Mr. Frodo was exhausted."
|
|
105
|
+
]
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Direct Segmenter Access
|
|
109
|
+
|
|
110
|
+
For more control, use the `Segmenter` class directly:
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
from fast_sentence_segment import Segmenter
|
|
114
|
+
|
|
115
|
+
segmenter = Segmenter()
|
|
116
|
+
results = segmenter.input_text("Your text here.")
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Command Line Interface
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
# Inline text
|
|
123
|
+
segment "Gandalf paused... You shall not pass! The Balrog roared."
|
|
124
|
+
|
|
125
|
+
# Pipe from stdin
|
|
126
|
+
echo "Have you seen Dr. Who? It's brilliant!" | segment
|
|
127
|
+
|
|
128
|
+
# Numbered output
|
|
129
|
+
segment -n -f silmarillion.txt
|
|
130
|
+
|
|
131
|
+
# File-to-file (one sentence per line)
|
|
132
|
+
segment-file --input-file book.txt --output-file sentences.txt
|
|
133
|
+
|
|
134
|
+
# Unwrap hard-wrapped e-texts (Project Gutenberg, etc.)
|
|
135
|
+
segment-file --input-file book.txt --output-file sentences.txt --unwrap
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## API Reference
|
|
139
|
+
|
|
140
|
+
| Function | Parameters | Returns | Description |
|
|
141
|
+
|----------|------------|---------|-------------|
|
|
142
|
+
| `segment_text()` | `input_text: str`, `flatten: bool = False`, `unwrap: bool = False` | `list` | Main entry point for segmentation |
|
|
143
|
+
| `Segmenter.input_text()` | `input_text: str` | `list[list[str]]` | Cached paragraph-aware segmentation |
|
|
144
|
+
|
|
145
|
+
### CLI Commands
|
|
146
|
+
|
|
147
|
+
| Command | Description |
|
|
148
|
+
|---------|-------------|
|
|
149
|
+
| `segment [text]` | Segment text from argument, `-f FILE`, or stdin. Use `-n` for numbered output. |
|
|
150
|
+
| `segment-file --input-file IN --output-file OUT [--unwrap]` | Segment a file and write one sentence per line. Use `--unwrap` for hard-wrapped e-texts. |
|
|
151
|
+
|
|
152
|
+
## Why Nested Lists?
|
|
153
|
+
|
|
154
|
+
The segmentation process preserves document structure by segmenting into both paragraphs and sentences. Each outer list represents a paragraph, and each inner list contains that paragraph's sentences. This is useful for:
|
|
155
|
+
|
|
156
|
+
- Document structure analysis
|
|
157
|
+
- Paragraph-level processing
|
|
158
|
+
- Maintaining original text organization
|
|
159
|
+
|
|
160
|
+
Use `flatten=True` when you only need sentences without paragraph context.
|
|
161
|
+
|
|
162
|
+
## Requirements
|
|
163
|
+
|
|
164
|
+
- Python 3.9+
|
|
165
|
+
- spaCy 3.8+
|
|
166
|
+
- en_core_web_sm spaCy model
|
|
167
|
+
|
|
168
|
+
## How It Works
|
|
169
|
+
|
|
170
|
+
This library uses spaCy for initial sentence segmentation, then applies surgical post-processing fixes for cases where spaCy's default behavior is incorrect:
|
|
171
|
+
|
|
172
|
+
1. **Pre-processing**: Normalize numbered lists, preserve ellipses with placeholders
|
|
173
|
+
2. **spaCy segmentation**: Use spaCy's sentence boundary detection
|
|
174
|
+
3. **Post-processing**: Split on abbreviation boundaries, handle `?`/`!` + capital patterns
|
|
175
|
+
4. **Denormalization**: Restore placeholders to original text
|
|
176
|
+
|
|
177
|
+
## License
|
|
178
|
+
|
|
179
|
+
MIT License - see [LICENSE](LICENSE) for details.
|
|
180
|
+
|
|
181
|
+
## Contributing
|
|
182
|
+
|
|
183
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
184
|
+
|
|
185
|
+
1. Fork the repository
|
|
186
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
187
|
+
3. Run tests (`make test`)
|
|
188
|
+
4. Commit your changes
|
|
189
|
+
5. Push to the branch
|
|
190
|
+
6. Open a Pull Request
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
from .bp import *
|
|
2
|
+
from .svc import *
|
|
3
|
+
from .dmo import *
|
|
4
|
+
|
|
5
|
+
from .bp.segmenter import Segmenter
|
|
6
|
+
from .dmo.unwrap_hard_wrapped_text import unwrap_hard_wrapped_text
|
|
7
|
+
from .dmo.normalize_quotes import normalize_quotes
|
|
8
|
+
|
|
9
|
+
segment = Segmenter().input_text
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def segment_text(
|
|
13
|
+
input_text: str,
|
|
14
|
+
flatten: bool = False,
|
|
15
|
+
unwrap: bool = False,
|
|
16
|
+
normalize: bool = True,
|
|
17
|
+
) -> list:
|
|
18
|
+
"""Segment text into sentences.
|
|
19
|
+
|
|
20
|
+
Args:
|
|
21
|
+
input_text: The text to segment.
|
|
22
|
+
flatten: If True, return a flat list of sentences instead of
|
|
23
|
+
nested paragraphs.
|
|
24
|
+
unwrap: If True, unwrap hard-wrapped lines (e.g., Project
|
|
25
|
+
Gutenberg e-texts) before segmenting.
|
|
26
|
+
normalize: If True (default), normalize unicode quote variants
|
|
27
|
+
to ASCII equivalents before segmenting. Ensures consistent
|
|
28
|
+
quote characters for downstream processing.
|
|
29
|
+
|
|
30
|
+
Returns:
|
|
31
|
+
List of sentences (if flatten=True) or list of paragraph
|
|
32
|
+
groups, each containing a list of sentences.
|
|
33
|
+
|
|
34
|
+
Related GitHub Issue:
|
|
35
|
+
#6 - Review findings from Issue #5
|
|
36
|
+
https://github.com/craigtrim/fast-sentence-segment/issues/6
|
|
37
|
+
"""
|
|
38
|
+
if unwrap:
|
|
39
|
+
input_text = unwrap_hard_wrapped_text(input_text)
|
|
40
|
+
|
|
41
|
+
if normalize:
|
|
42
|
+
input_text = normalize_quotes(input_text)
|
|
43
|
+
|
|
44
|
+
results = segment(input_text)
|
|
45
|
+
|
|
46
|
+
if flatten:
|
|
47
|
+
flat = []
|
|
48
|
+
[[flat.append(y) for y in x] for x in results]
|
|
49
|
+
return flat
|
|
50
|
+
|
|
51
|
+
return results
|
{fast-sentence-segment-0.1.9 → fast_sentence_segment-1.4.0}/fast_sentence_segment/bp/__init__.py
RENAMED
|
@@ -1 +1 @@
|
|
|
1
|
-
from .segmenter import Segmenter
|
|
1
|
+
from .segmenter import Segmenter
|
{fast-sentence-segment-0.1.9 → fast_sentence_segment-1.4.0}/fast_sentence_segment/bp/segmenter.py
RENAMED
|
@@ -1,68 +1,65 @@
|
|
|
1
|
-
#!/usr/bin/env python
|
|
2
|
-
# -*- coding: UTF-8 -*-
|
|
3
|
-
""" Orchestrate Sentence Segmentation """
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
from functools import lru_cache
|
|
7
|
-
|
|
8
|
-
from
|
|
9
|
-
from
|
|
10
|
-
from
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
f"\tTotal Time: {str(sw)}"]))
|
|
67
|
-
|
|
68
|
-
return paragraphs
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: UTF-8 -*-
|
|
3
|
+
""" Orchestrate Sentence Segmentation """
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
from functools import lru_cache
|
|
7
|
+
|
|
8
|
+
from fast_sentence_segment.core import BaseObject, Stopwatch
|
|
9
|
+
from fast_sentence_segment.svc import PerformParagraphSegmentation
|
|
10
|
+
from fast_sentence_segment.svc import PerformSentenceSegmentation
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Segmenter(BaseObject):
|
|
14
|
+
""" Orchestrate Sentence Segmentation """
|
|
15
|
+
|
|
16
|
+
def __init__(self):
|
|
17
|
+
""" Change Log
|
|
18
|
+
|
|
19
|
+
Created:
|
|
20
|
+
30-Sept-2021
|
|
21
|
+
"""
|
|
22
|
+
BaseObject.__init__(self, __name__)
|
|
23
|
+
self._segment_paragraphs = PerformParagraphSegmentation().process
|
|
24
|
+
self._segment_sentences = PerformSentenceSegmentation().process
|
|
25
|
+
|
|
26
|
+
def _input_text(self,
|
|
27
|
+
input_text: str) -> list:
|
|
28
|
+
paragraphs = []
|
|
29
|
+
|
|
30
|
+
for paragraph in self._segment_paragraphs(input_text):
|
|
31
|
+
paragraphs.append(self._segment_sentences(paragraph))
|
|
32
|
+
|
|
33
|
+
return paragraphs
|
|
34
|
+
|
|
35
|
+
@lru_cache(maxsize=1024, typed=True)
|
|
36
|
+
def input_text(self,
|
|
37
|
+
input_text: str) -> list:
|
|
38
|
+
"""Segment Input Text into Paragraphs and Sentences
|
|
39
|
+
|
|
40
|
+
Args:
|
|
41
|
+
input_text (str): An input string of any length or type
|
|
42
|
+
|
|
43
|
+
Raises:
|
|
44
|
+
ValueError: input must be a string
|
|
45
|
+
|
|
46
|
+
Returns:
|
|
47
|
+
list: returns a list of lists.
|
|
48
|
+
Each outer list is a paragraph.
|
|
49
|
+
Each inner list contains 1..* sentences
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
if self.isEnabledForDebug and not isinstance(input_text, str):
|
|
53
|
+
raise ValueError(f"Expected str, got {type(input_text)}")
|
|
54
|
+
|
|
55
|
+
sw = Stopwatch()
|
|
56
|
+
|
|
57
|
+
paragraphs = self._input_text(input_text)
|
|
58
|
+
|
|
59
|
+
if self.isEnabledForInfo:
|
|
60
|
+
self.logger.info('\n'.join([
|
|
61
|
+
"Segmentation of Input Text Complete",
|
|
62
|
+
f"\tTotal Paragraphs: {len(paragraphs)}",
|
|
63
|
+
f"\tTotal Time: {str(sw)}"]))
|
|
64
|
+
|
|
65
|
+
return paragraphs
|