fast-sentence-segment 1.4.2__tar.gz → 1.4.4__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.2 → fast_sentence_segment-1.4.4}/PKG-INFO +3 -3
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/cli.py +31 -15
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/dmo/spacy_doc_segmenter.py +51 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/pyproject.toml +1 -1
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/setup.py +1 -1
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/LICENSE +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/README.md +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/__init__.py +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/bp/__init__.py +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/bp/segmenter.py +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/core/__init__.py +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/core/base_object.py +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/core/stopwatch.py +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/dmo/__init__.py +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/dmo/abbreviation_merger.py +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/dmo/abbreviation_splitter.py +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/dmo/abbreviations.py +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/dmo/bullet_point_cleaner.py +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/dmo/dehyphenator.py +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/dmo/ellipsis_normalizer.py +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/dmo/group_quoted_sentences.py +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/dmo/newlines_to_periods.py +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/dmo/normalize_quotes.py +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/dmo/numbered_list_normalizer.py +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/dmo/post_process_sentences.py +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/dmo/question_exclamation_splitter.py +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/dmo/strip_trailing_period_after_quote.py +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/dmo/title_name_merger.py +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/dmo/unwrap_hard_wrapped_text.py +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/svc/__init__.py +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/svc/perform_paragraph_segmentation.py +0 -0
- {fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/svc/perform_sentence_segmentation.py +0 -0
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: fast-sentence-segment
|
|
3
|
-
Version: 1.4.
|
|
3
|
+
Version: 1.4.4
|
|
4
4
|
Summary: Fast and Efficient Sentence Segmentation
|
|
5
|
-
Home-page: https://github.com/craigtrim/fast-sentence-segment
|
|
6
5
|
License: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
7
|
Keywords: nlp,text,preprocess,segment
|
|
8
8
|
Author: Craig Trim
|
|
9
9
|
Author-email: craigtrim@gmail.com
|
|
@@ -143,34 +143,50 @@ def file_main():
|
|
|
143
143
|
)
|
|
144
144
|
args = parser.parse_args()
|
|
145
145
|
|
|
146
|
+
# Echo command immediately
|
|
146
147
|
_header("segment-file")
|
|
148
|
+
print(f" {DIM}Segmenting text file into sentences{RESET}")
|
|
149
|
+
print()
|
|
150
|
+
|
|
151
|
+
# Show configuration
|
|
147
152
|
_param("Input", args.input_file)
|
|
148
153
|
_param("Output", args.output_file)
|
|
149
154
|
_param("Size", _file_size(args.input_file))
|
|
150
155
|
_param("Unwrap", "enabled" if args.unwrap else "disabled")
|
|
151
156
|
_param("Normalize quotes", "disabled" if args.no_normalize_quotes else "enabled")
|
|
157
|
+
print()
|
|
152
158
|
|
|
159
|
+
# Step 1: Read file
|
|
160
|
+
print(f" {YELLOW}→{RESET} Reading input file...")
|
|
153
161
|
with open(args.input_file, "r", encoding="utf-8") as f:
|
|
154
162
|
text = f.read()
|
|
163
|
+
print(f" {GREEN}✓{RESET} Read {len(text):,} characters")
|
|
155
164
|
|
|
165
|
+
# Step 2: Segment text
|
|
166
|
+
print(f" {YELLOW}→{RESET} Segmenting text...", end="", flush=True)
|
|
156
167
|
start = time.perf_counter()
|
|
157
168
|
normalize = not args.no_normalize_quotes
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
)
|
|
169
|
+
sentences = segment_text(
|
|
170
|
+
text.strip(), flatten=True, unwrap=args.unwrap, normalize=normalize,
|
|
171
|
+
)
|
|
162
172
|
elapsed = time.perf_counter() - start
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
173
|
+
print(f"\r {GREEN}✓{RESET} Segmented into {len(sentences):,} sentences ({elapsed:.2f}s)")
|
|
174
|
+
|
|
175
|
+
# Step 3: Write output
|
|
176
|
+
total = len(sentences)
|
|
177
|
+
with open(args.output_file, "w", encoding="utf-8") as f:
|
|
178
|
+
if args.unwrap:
|
|
179
|
+
f.write(format_grouped_sentences(sentences) + "\n")
|
|
180
|
+
print(f" {GREEN}✓{RESET} Written {total:,} sentences to {args.output_file}")
|
|
181
|
+
else:
|
|
182
|
+
for i, sentence in enumerate(sentences, 1):
|
|
183
|
+
f.write(sentence + "\n")
|
|
184
|
+
if i % 500 == 0 or i == total:
|
|
185
|
+
pct = (i / total) * 100
|
|
186
|
+
print(f"\r {YELLOW}→{RESET} Writing... {pct:.0f}% ({i:,}/{total:,})", end="", flush=True)
|
|
187
|
+
print(f"\r {GREEN}✓{RESET} Written {total:,} sentences to {args.output_file} ")
|
|
188
|
+
|
|
189
|
+
print(f"\n {GREEN}Done!{RESET}")
|
|
174
190
|
print()
|
|
175
191
|
|
|
176
192
|
|
|
@@ -65,6 +65,54 @@ class SpacyDocSegmenter(BaseObject):
|
|
|
65
65
|
return False
|
|
66
66
|
return True
|
|
67
67
|
|
|
68
|
+
@staticmethod
|
|
69
|
+
def _merge_orphaned_quotes(sentences: list) -> list:
|
|
70
|
+
"""Merge orphaned opening quotes with the following sentence.
|
|
71
|
+
|
|
72
|
+
spaCy sometimes splits on opening quotes, producing sentences like:
|
|
73
|
+
["'", "Oh, the funeral..."]
|
|
74
|
+
This merges them into:
|
|
75
|
+
["'Oh, the funeral..."]
|
|
76
|
+
|
|
77
|
+
Also handles trailing orphaned quotes that should belong to next sentence:
|
|
78
|
+
["He said. '", "Hello!'"]
|
|
79
|
+
Becomes:
|
|
80
|
+
["He said.", "'Hello!'"]
|
|
81
|
+
"""
|
|
82
|
+
if not sentences:
|
|
83
|
+
return sentences
|
|
84
|
+
|
|
85
|
+
result = []
|
|
86
|
+
i = 0
|
|
87
|
+
while i < len(sentences):
|
|
88
|
+
sent = sentences[i]
|
|
89
|
+
# Check if this sentence is just an opening quote
|
|
90
|
+
if sent.strip() in ("'", '"', "'.", '".'):
|
|
91
|
+
# Merge with the next sentence if available
|
|
92
|
+
if i + 1 < len(sentences):
|
|
93
|
+
quote_char = sent.strip().rstrip('.')
|
|
94
|
+
result.append(quote_char + sentences[i + 1])
|
|
95
|
+
i += 2
|
|
96
|
+
continue
|
|
97
|
+
result.append(sent)
|
|
98
|
+
i += 1
|
|
99
|
+
|
|
100
|
+
# Second pass: handle trailing orphaned quotes
|
|
101
|
+
# Pattern: sentence ends with `. '` or `. "` - move quote to next sentence
|
|
102
|
+
fixed = []
|
|
103
|
+
for i, sent in enumerate(result):
|
|
104
|
+
# Check for trailing orphaned quote (`. '` or `? '` or `! '`)
|
|
105
|
+
if len(sent) >= 3 and sent[-2:] in (" '", ' "') and sent[-3] in '.?!':
|
|
106
|
+
# Strip the trailing quote
|
|
107
|
+
trailing_quote = sent[-1]
|
|
108
|
+
sent = sent[:-2]
|
|
109
|
+
# Prepend to next sentence if available
|
|
110
|
+
if i + 1 < len(result) and not result[i + 1].startswith(('"', "'")):
|
|
111
|
+
result[i + 1] = trailing_quote + result[i + 1]
|
|
112
|
+
fixed.append(sent)
|
|
113
|
+
|
|
114
|
+
return fixed
|
|
115
|
+
|
|
68
116
|
@staticmethod
|
|
69
117
|
def _cleanse(sentences: list) -> str:
|
|
70
118
|
sentences = [sent for sent in sentences
|
|
@@ -103,6 +151,9 @@ class SpacyDocSegmenter(BaseObject):
|
|
|
103
151
|
sentences = [sent for sent in sentences if
|
|
104
152
|
sent and len(sent) and sent != 'None']
|
|
105
153
|
|
|
154
|
+
# Merge orphaned opening quotes with following sentence
|
|
155
|
+
sentences = self._merge_orphaned_quotes(sentences)
|
|
156
|
+
|
|
106
157
|
sentences = [self._append_period(sent)
|
|
107
158
|
for sent in sentences]
|
|
108
159
|
|
|
@@ -11,7 +11,7 @@ description = "Fast and Efficient Sentence Segmentation"
|
|
|
11
11
|
license = "MIT"
|
|
12
12
|
name = "fast-sentence-segment"
|
|
13
13
|
readme = "README.md"
|
|
14
|
-
version = "1.4.
|
|
14
|
+
version = "1.4.4"
|
|
15
15
|
|
|
16
16
|
keywords = ["nlp", "text", "preprocess", "segment"]
|
|
17
17
|
repository = "https://github.com/craigtrim/fast-sentence-segment"
|
|
@@ -20,7 +20,7 @@ entry_points = \
|
|
|
20
20
|
|
|
21
21
|
setup_kwargs = {
|
|
22
22
|
'name': 'fast-sentence-segment',
|
|
23
|
-
'version': '1.4.
|
|
23
|
+
'version': '1.4.4',
|
|
24
24
|
'description': 'Fast and Efficient Sentence Segmentation',
|
|
25
25
|
'long_description': '# Fast Sentence Segmentation\n\n[](https://pypi.org/project/fast-sentence-segment/)\n[](https://pypi.org/project/fast-sentence-segment/)\n[](https://github.com/craigtrim/fast-sentence-segment/actions/workflows/ci.yml)\n[](https://github.com/craigtrim/fast-sentence-segment/tree/master/tests)\n[](https://opensource.org/licenses/MIT)\n[](https://github.com/astral-sh/ruff)\n[](https://pepy.tech/project/fast-sentence-segment)\n[](https://pepy.tech/project/fast-sentence-segment)\n\nFast 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.\n\n## Why This Library?\n\n1. **Keep it local**: LLM API calls cost money and send your data to third parties. Run sentence segmentation entirely on your machine.\n2. **spaCy perfected**: spaCy is a great local model, but it makes mistakes. This library fixes most of spaCy\'s shortcomings.\n\n## Features\n\n- **Paragraph-aware segmentation**: Returns sentences grouped by paragraph\n- **Abbreviation handling**: Correctly handles "Dr.", "Mr.", "etc.", "p.m.", "a.m." without false splits\n- **Ellipsis preservation**: Keeps `...` intact while detecting sentence boundaries\n- **Question/exclamation splitting**: Properly splits on `?` and `!` followed by capital letters\n- **Cached processing**: LRU cache for repeated text processing\n- **Flexible output**: Nested lists (by paragraph) or flattened list of sentences\n- **Bullet point & numbered list normalization**: Cleans common list formats\n- **CLI tool**: Command-line interface for quick segmentation\n\n## Installation\n\n```bash\npip install fast-sentence-segment\n```\n\nAfter installation, download the spaCy model:\n\n```bash\npython -m spacy download en_core_web_sm\n```\n\n## Quick Start\n\n```python\nfrom fast_sentence_segment import segment_text\n\ntext = "Do you like Dr. Who? I prefer Dr. Strange! Mr. T is also cool."\n\nresults = segment_text(text, flatten=True)\n```\n\n```json\n[\n "Do you like Dr. Who?",\n "I prefer Dr. Strange!",\n "Mr. T is also cool."\n]\n```\n\nNotice 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.\n\n## Usage\n\n### Basic Segmentation\n\nThe `segment_text` function returns a list of lists, where each inner list represents a paragraph containing its sentences:\n\n```python\nfrom fast_sentence_segment import segment_text\n\ntext = """Gandalf spoke softly. "All we have to decide is what to do with the time given us."\n\nFrodo nodded. The weight of the Ring pressed against his chest."""\n\nresults = segment_text(text)\n```\n\n```json\n[\n [\n "Gandalf spoke softly.",\n "\\"All we have to decide is what to do with the time given us.\\"."\n ],\n [\n "Frodo nodded.",\n "The weight of the Ring pressed against his chest."\n ]\n]\n```\n\n### Flattened Output\n\nIf you don\'t need paragraph boundaries, use the `flatten` parameter:\n\n```python\ntext = "At 9 a.m. the hobbits set out. By 3 p.m. they reached Rivendell. Mr. Frodo was exhausted."\n\nresults = segment_text(text, flatten=True)\n```\n\n```json\n[\n "At 9 a.m. the hobbits set out.",\n "By 3 p.m. they reached Rivendell.",\n "Mr. Frodo was exhausted."\n]\n```\n\n### Direct Segmenter Access\n\nFor more control, use the `Segmenter` class directly:\n\n```python\nfrom fast_sentence_segment import Segmenter\n\nsegmenter = Segmenter()\nresults = segmenter.input_text("Your text here.")\n```\n\n### Command Line Interface\n\n```bash\n# Inline text\nsegment "Gandalf paused... You shall not pass! The Balrog roared."\n\n# Pipe from stdin\necho "Have you seen Dr. Who? It\'s brilliant!" | segment\n\n# Numbered output\nsegment -n -f silmarillion.txt\n\n# File-to-file (one sentence per line)\nsegment-file --input-file book.txt --output-file sentences.txt\n\n# Unwrap hard-wrapped e-texts (Project Gutenberg, etc.)\nsegment-file --input-file book.txt --output-file sentences.txt --unwrap\n```\n\n## API Reference\n\n| Function | Parameters | Returns | Description |\n|----------|------------|---------|-------------|\n| `segment_text()` | `input_text: str`, `flatten: bool = False`, `unwrap: bool = False` | `list` | Main entry point for segmentation |\n| `Segmenter.input_text()` | `input_text: str` | `list[list[str]]` | Cached paragraph-aware segmentation |\n\n### CLI Commands\n\n| Command | Description |\n|---------|-------------|\n| `segment [text]` | Segment text from argument, `-f FILE`, or stdin. Use `-n` for numbered output. |\n| `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. |\n\n## Why Nested Lists?\n\nThe 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:\n\n- Document structure analysis\n- Paragraph-level processing\n- Maintaining original text organization\n\nUse `flatten=True` when you only need sentences without paragraph context.\n\n## Requirements\n\n- Python 3.9+\n- spaCy 3.8+\n- en_core_web_sm spaCy model\n\n## How It Works\n\nThis library uses spaCy for initial sentence segmentation, then applies surgical post-processing fixes for cases where spaCy\'s default behavior is incorrect:\n\n1. **Pre-processing**: Normalize numbered lists, preserve ellipses with placeholders\n2. **spaCy segmentation**: Use spaCy\'s sentence boundary detection\n3. **Post-processing**: Split on abbreviation boundaries, handle `?`/`!` + capital patterns\n4. **Denormalization**: Restore placeholders to original text\n\n## License\n\nMIT License - see [LICENSE](LICENSE) for details.\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n1. Fork the repository\n2. Create your feature branch (`git checkout -b feature/amazing-feature`)\n3. Run tests (`make test`)\n4. Commit your changes\n5. Push to the branch\n6. Open a Pull Request\n',
|
|
26
26
|
'author': 'Craig Trim',
|
|
File without changes
|
|
File without changes
|
{fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/__init__.py
RENAMED
|
File without changes
|
{fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/bp/__init__.py
RENAMED
|
File without changes
|
{fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/bp/segmenter.py
RENAMED
|
File without changes
|
{fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/core/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
{fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/core/stopwatch.py
RENAMED
|
File without changes
|
{fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/dmo/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{fast_sentence_segment-1.4.2 → fast_sentence_segment-1.4.4}/fast_sentence_segment/svc/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|