fast-sentence-segment 1.3.0__py3-none-any.whl → 1.4.0__py3-none-any.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.
- fast_sentence_segment/cli.py +6 -1
- fast_sentence_segment/dmo/__init__.py +1 -0
- fast_sentence_segment/dmo/dehyphenator.py +55 -0
- fast_sentence_segment/dmo/unwrap_hard_wrapped_text.py +44 -3
- fast_sentence_segment/svc/perform_sentence_segmentation.py +6 -0
- {fast_sentence_segment-1.3.0.dist-info → fast_sentence_segment-1.4.0.dist-info}/METADATA +3 -2
- {fast_sentence_segment-1.3.0.dist-info → fast_sentence_segment-1.4.0.dist-info}/RECORD +10 -9
- {fast_sentence_segment-1.3.0.dist-info → fast_sentence_segment-1.4.0.dist-info}/LICENSE +0 -0
- {fast_sentence_segment-1.3.0.dist-info → fast_sentence_segment-1.4.0.dist-info}/WHEEL +0 -0
- {fast_sentence_segment-1.3.0.dist-info → fast_sentence_segment-1.4.0.dist-info}/entry_points.txt +0 -0
fast_sentence_segment/cli.py
CHANGED
|
@@ -62,6 +62,11 @@ def main():
|
|
|
62
62
|
action="store_true",
|
|
63
63
|
help="Number output lines",
|
|
64
64
|
)
|
|
65
|
+
parser.add_argument(
|
|
66
|
+
"--unwrap",
|
|
67
|
+
action="store_true",
|
|
68
|
+
help="Unwrap hard-wrapped lines and dehyphenate split words",
|
|
69
|
+
)
|
|
65
70
|
args = parser.parse_args()
|
|
66
71
|
|
|
67
72
|
# Get input text
|
|
@@ -77,7 +82,7 @@ def main():
|
|
|
77
82
|
sys.exit(1)
|
|
78
83
|
|
|
79
84
|
# Segment and output
|
|
80
|
-
sentences = segment_text(text.strip(), flatten=True)
|
|
85
|
+
sentences = segment_text(text.strip(), flatten=True, unwrap=args.unwrap)
|
|
81
86
|
for i, sentence in enumerate(sentences, 1):
|
|
82
87
|
if args.numbered:
|
|
83
88
|
print(f"{i}. {sentence}")
|
|
@@ -2,6 +2,7 @@ from .abbreviation_merger import AbbreviationMerger
|
|
|
2
2
|
from .abbreviation_splitter import AbbreviationSplitter
|
|
3
3
|
from .title_name_merger import TitleNameMerger
|
|
4
4
|
from .bullet_point_cleaner import BulletPointCleaner
|
|
5
|
+
from .dehyphenator import Dehyphenator
|
|
5
6
|
from .ellipsis_normalizer import EllipsisNormalizer
|
|
6
7
|
from .newlines_to_periods import NewlinesToPeriods
|
|
7
8
|
from .post_process_sentences import PostProcessStructure
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# -*- coding: UTF-8 -*-
|
|
2
|
+
"""Dehyphenate words split across lines.
|
|
3
|
+
|
|
4
|
+
Related GitHub Issue:
|
|
5
|
+
#8 - Add dehyphenation support for words split across lines
|
|
6
|
+
https://github.com/craigtrim/fast-sentence-segment/issues/8
|
|
7
|
+
|
|
8
|
+
When processing ebooks and scanned documents, words are often hyphenated
|
|
9
|
+
at line breaks for typesetting purposes. This module rejoins those words.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
import re
|
|
13
|
+
|
|
14
|
+
from fast_sentence_segment.core import BaseObject
|
|
15
|
+
|
|
16
|
+
# Pattern to match hyphenated word breaks at end of line:
|
|
17
|
+
# - A single hyphen (not -- em-dash)
|
|
18
|
+
# - Followed by newline and optional whitespace
|
|
19
|
+
# - Followed by a lowercase letter (continuation of word)
|
|
20
|
+
_HYPHEN_LINE_BREAK_PATTERN = re.compile(r'(?<!-)-\n\s*([a-z])')
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class Dehyphenator(BaseObject):
|
|
24
|
+
"""Rejoin words that were hyphenated across line breaks."""
|
|
25
|
+
|
|
26
|
+
def __init__(self):
|
|
27
|
+
"""Change Log
|
|
28
|
+
|
|
29
|
+
Created:
|
|
30
|
+
3-Feb-2026
|
|
31
|
+
craigtrim@gmail.com
|
|
32
|
+
* add dehyphenation support for words split across lines
|
|
33
|
+
https://github.com/craigtrim/fast-sentence-segment/issues/8
|
|
34
|
+
"""
|
|
35
|
+
BaseObject.__init__(self, __name__)
|
|
36
|
+
|
|
37
|
+
@staticmethod
|
|
38
|
+
def process(input_text: str) -> str:
|
|
39
|
+
"""Rejoin words that were hyphenated across line breaks.
|
|
40
|
+
|
|
41
|
+
Detects the pattern of a word fragment ending with a hyphen
|
|
42
|
+
at the end of a line, followed by the word continuation
|
|
43
|
+
starting with a lowercase letter on the next line.
|
|
44
|
+
|
|
45
|
+
Examples:
|
|
46
|
+
"bot-\\ntle" -> "bottle"
|
|
47
|
+
"cham-\\n bermaid" -> "chambermaid"
|
|
48
|
+
|
|
49
|
+
Args:
|
|
50
|
+
input_text: Text that may contain hyphenated line breaks.
|
|
51
|
+
|
|
52
|
+
Returns:
|
|
53
|
+
Text with hyphenated word breaks rejoined.
|
|
54
|
+
"""
|
|
55
|
+
return _HYPHEN_LINE_BREAK_PATTERN.sub(r'\1', input_text)
|
|
@@ -2,30 +2,71 @@
|
|
|
2
2
|
"""Unwrap hard-wrapped text (e.g., Project Gutenberg e-texts).
|
|
3
3
|
|
|
4
4
|
Joins lines within paragraphs into continuous strings while
|
|
5
|
-
preserving paragraph boundaries (blank lines).
|
|
5
|
+
preserving paragraph boundaries (blank lines). Also dehyphenates
|
|
6
|
+
words that were split across lines for typesetting.
|
|
7
|
+
|
|
8
|
+
Related GitHub Issue:
|
|
9
|
+
#8 - Add dehyphenation support for words split across lines
|
|
10
|
+
https://github.com/craigtrim/fast-sentence-segment/issues/8
|
|
6
11
|
"""
|
|
7
12
|
|
|
8
13
|
import re
|
|
9
14
|
|
|
15
|
+
# Pattern to match hyphenated word breaks at end of line:
|
|
16
|
+
# - A single hyphen (not -- em-dash)
|
|
17
|
+
# - Followed by newline and optional whitespace
|
|
18
|
+
# - Followed by a lowercase letter (continuation of word)
|
|
19
|
+
_HYPHEN_LINE_BREAK_PATTERN = re.compile(r'(?<!-)-\n\s*([a-z])')
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _dehyphenate_block(block: str) -> str:
|
|
23
|
+
"""Remove hyphens from words split across lines.
|
|
24
|
+
|
|
25
|
+
Detects the pattern of a word fragment ending with a hyphen
|
|
26
|
+
at the end of a line, followed by the word continuation
|
|
27
|
+
starting with a lowercase letter on the next line.
|
|
28
|
+
|
|
29
|
+
Examples:
|
|
30
|
+
"bot-\\ntle" -> "bottle"
|
|
31
|
+
"cham-\\n bermaid" -> "chambermaid"
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
block: A paragraph block that may contain hyphenated line breaks.
|
|
35
|
+
|
|
36
|
+
Returns:
|
|
37
|
+
The block with hyphenated word breaks rejoined.
|
|
38
|
+
"""
|
|
39
|
+
return _HYPHEN_LINE_BREAK_PATTERN.sub(r'\1', block)
|
|
40
|
+
|
|
10
41
|
|
|
11
42
|
def unwrap_hard_wrapped_text(text: str) -> str:
|
|
12
43
|
"""Unwrap hard-wrapped paragraphs into continuous lines.
|
|
13
44
|
|
|
14
45
|
Splits on blank lines to identify paragraphs, then joins
|
|
15
46
|
lines within each paragraph into a single string with
|
|
16
|
-
single spaces.
|
|
47
|
+
single spaces. Also dehyphenates words that were split
|
|
48
|
+
across lines for typesetting purposes.
|
|
49
|
+
|
|
50
|
+
Examples:
|
|
51
|
+
>>> unwrap_hard_wrapped_text("a bot-\\ntle of wine")
|
|
52
|
+
'a bottle of wine'
|
|
53
|
+
>>> unwrap_hard_wrapped_text("line one\\nline two")
|
|
54
|
+
'line one line two'
|
|
17
55
|
|
|
18
56
|
Args:
|
|
19
57
|
text: Raw text with hard-wrapped lines.
|
|
20
58
|
|
|
21
59
|
Returns:
|
|
22
60
|
Text with paragraphs unwrapped into continuous strings,
|
|
23
|
-
separated by double newlines.
|
|
61
|
+
separated by double newlines, with hyphenated words rejoined.
|
|
24
62
|
"""
|
|
25
63
|
blocks = re.split(r'\n\s*\n', text)
|
|
26
64
|
unwrapped = []
|
|
27
65
|
|
|
28
66
|
for block in blocks:
|
|
67
|
+
# First, dehyphenate words split across lines
|
|
68
|
+
block = _dehyphenate_block(block)
|
|
69
|
+
# Then join remaining lines with spaces
|
|
29
70
|
lines = block.splitlines()
|
|
30
71
|
joined = ' '.join(line.strip() for line in lines if line.strip())
|
|
31
72
|
if joined:
|
|
@@ -18,6 +18,7 @@ from fast_sentence_segment.dmo import QuestionExclamationSplitter
|
|
|
18
18
|
from fast_sentence_segment.dmo import SpacyDocSegmenter
|
|
19
19
|
from fast_sentence_segment.dmo import PostProcessStructure
|
|
20
20
|
from fast_sentence_segment.dmo import StripTrailingPeriodAfterQuote
|
|
21
|
+
from fast_sentence_segment.dmo import Dehyphenator
|
|
21
22
|
|
|
22
23
|
|
|
23
24
|
class PerformSentenceSegmentation(BaseObject):
|
|
@@ -46,6 +47,7 @@ class PerformSentenceSegmentation(BaseObject):
|
|
|
46
47
|
if not self.__nlp:
|
|
47
48
|
self.__nlp = spacy.load("en_core_web_sm")
|
|
48
49
|
|
|
50
|
+
self._dehyphenate = Dehyphenator.process
|
|
49
51
|
self._newlines_to_periods = NewlinesToPeriods.process
|
|
50
52
|
self._normalize_numbered_lists = NumberedListNormalizer().process
|
|
51
53
|
self._normalize_ellipses = EllipsisNormalizer().process
|
|
@@ -96,6 +98,10 @@ class PerformSentenceSegmentation(BaseObject):
|
|
|
96
98
|
# Normalize tabs to spaces
|
|
97
99
|
input_text = input_text.replace('\t', ' ')
|
|
98
100
|
|
|
101
|
+
# Dehyphenate words split across lines (issue #8)
|
|
102
|
+
# Must happen before newlines are converted to periods
|
|
103
|
+
input_text = self._dehyphenate(input_text)
|
|
104
|
+
|
|
99
105
|
input_text = self._normalize_numbered_lists(input_text)
|
|
100
106
|
input_text = self._normalize_ellipses(input_text)
|
|
101
107
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: fast-sentence-segment
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.4.0
|
|
4
4
|
Summary: Fast and Efficient Sentence Segmentation
|
|
5
5
|
Home-page: https://github.com/craigtrim/fast-sentence-segment
|
|
6
6
|
License: MIT
|
|
@@ -33,8 +33,9 @@ Description-Content-Type: text/markdown
|
|
|
33
33
|
|
|
34
34
|
[](https://pypi.org/project/fast-sentence-segment/)
|
|
35
35
|
[](https://pypi.org/project/fast-sentence-segment/)
|
|
36
|
+
[](https://github.com/craigtrim/fast-sentence-segment/actions/workflows/ci.yml)
|
|
36
37
|
[](https://opensource.org/licenses/MIT)
|
|
37
|
-
[](https://github.com/astral-sh/ruff)
|
|
38
39
|
[](https://pepy.tech/project/fast-sentence-segment)
|
|
39
40
|
[](https://pepy.tech/project/fast-sentence-segment)
|
|
40
41
|
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
fast_sentence_segment/__init__.py,sha256=jeb4yCy89ivyqbo-4ldJLquPAG_XR_33Q7nrDjqPxvE,1465
|
|
2
2
|
fast_sentence_segment/bp/__init__.py,sha256=j2-WfQ9WwVuXeGSjvV6XLVwEdvau8sdAQe4Pa4DrYi8,33
|
|
3
3
|
fast_sentence_segment/bp/segmenter.py,sha256=UW6DguPgA56h-pPYRsfJhjIzBe40j6NdjkwYxamASyA,1928
|
|
4
|
-
fast_sentence_segment/cli.py,sha256=
|
|
4
|
+
fast_sentence_segment/cli.py,sha256=Y89BH-xbJ0vykg301D2543MtGP4kYLnA6i3UQ7Hg5YA,3869
|
|
5
5
|
fast_sentence_segment/core/__init__.py,sha256=uoBersYyVStJ5a8zJpQz1GDGaloEdAv2jGHw1292hRM,108
|
|
6
6
|
fast_sentence_segment/core/base_object.py,sha256=AYr7yzusIwawjbKdvcv4yTEnhmx6M583kDZzhzPOmq4,635
|
|
7
7
|
fast_sentence_segment/core/stopwatch.py,sha256=hE6hMz2q6rduaKi58KZmiAL-lRtyh_wWCANhl4KLkRQ,879
|
|
8
|
-
fast_sentence_segment/dmo/__init__.py,sha256=
|
|
8
|
+
fast_sentence_segment/dmo/__init__.py,sha256=N0lLHVn6zKeg6h1LIfoc4XeXPUY-uSbMT45dP2_vn8M,862
|
|
9
9
|
fast_sentence_segment/dmo/abbreviation_merger.py,sha256=tCXM6yCfMryJvMIVWIxP_EocoibZi8vohFzJ5tvMYr0,4432
|
|
10
10
|
fast_sentence_segment/dmo/abbreviation_splitter.py,sha256=03mSyJcLooNyIjXx6mPlrnjmKgZW-uhUIqG4U-MbIGw,2981
|
|
11
11
|
fast_sentence_segment/dmo/abbreviations.py,sha256=CGJrJDo6pmYd3pTNEQbdOo8N6tnkCnwyL2X7Si663Os,2530
|
|
12
12
|
fast_sentence_segment/dmo/bullet_point_cleaner.py,sha256=WOZQRWXiiyRi8rOuEIw36EmkaXmATHL9_Dxb2rderw4,1606
|
|
13
|
+
fast_sentence_segment/dmo/dehyphenator.py,sha256=6BJTie7tClRAifeiW8V2CdAAbcbknhtqmKylAdRZ7ko,1776
|
|
13
14
|
fast_sentence_segment/dmo/ellipsis_normalizer.py,sha256=lHs9dLFfKJe-2vFNe17Hik90g3_kXX347OzGP_IOT08,1521
|
|
14
15
|
fast_sentence_segment/dmo/group_quoted_sentences.py,sha256=Ifh_kUwi7sMbzbZvrTgEKkzWe50AafUDhVKVPR9h7wQ,5092
|
|
15
16
|
fast_sentence_segment/dmo/newlines_to_periods.py,sha256=PUrXreqZWiITINfoJL5xRRlXJH6noH0cdXtW1EqAh8I,1517
|
|
@@ -20,12 +21,12 @@ fast_sentence_segment/dmo/question_exclamation_splitter.py,sha256=cRsWRu8zb6wOWG
|
|
|
20
21
|
fast_sentence_segment/dmo/spacy_doc_segmenter.py,sha256=_oTsrIL2rjysjt_8bPJVNTn230pUtL-geCC8g174iC4,3163
|
|
21
22
|
fast_sentence_segment/dmo/strip_trailing_period_after_quote.py,sha256=wYkoLy5XJKZIblJXBvDAB8-a81UTQOhOf2u91wjJWUw,2259
|
|
22
23
|
fast_sentence_segment/dmo/title_name_merger.py,sha256=zbG04_VjwM8TtT8LhavvmZqIZL_2xgT2OTxWkK_Zt1s,5133
|
|
23
|
-
fast_sentence_segment/dmo/unwrap_hard_wrapped_text.py,sha256=
|
|
24
|
+
fast_sentence_segment/dmo/unwrap_hard_wrapped_text.py,sha256=V1T5RsJBaII_iGJMyWvv6rb2mny8pnVd428oVZL0n5I,2457
|
|
24
25
|
fast_sentence_segment/svc/__init__.py,sha256=9B12mXxBnlalH4OAm1AMLwUMa-RLi2ilv7qhqv26q7g,144
|
|
25
26
|
fast_sentence_segment/svc/perform_paragraph_segmentation.py,sha256=zLKw9rSzb0NNfx4MyEeoGrHwhxTtH5oDrYcAL2LMVHY,1378
|
|
26
|
-
fast_sentence_segment/svc/perform_sentence_segmentation.py,sha256=
|
|
27
|
-
fast_sentence_segment-1.
|
|
28
|
-
fast_sentence_segment-1.
|
|
29
|
-
fast_sentence_segment-1.
|
|
30
|
-
fast_sentence_segment-1.
|
|
31
|
-
fast_sentence_segment-1.
|
|
27
|
+
fast_sentence_segment/svc/perform_sentence_segmentation.py,sha256=Qaj7oxVHfUd6pIlwCD1O8P14LaGUdolFGuykmrF6gw8,6276
|
|
28
|
+
fast_sentence_segment-1.4.0.dist-info/LICENSE,sha256=vou5JCLAT5nHcsUv-AkjUYAihYfN9mwPDXxV2DHyHBo,1067
|
|
29
|
+
fast_sentence_segment-1.4.0.dist-info/METADATA,sha256=XernLjKJbfSBxiqhQm-SZC8JFtggSoPe1hUD2X6D9N8,7853
|
|
30
|
+
fast_sentence_segment-1.4.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
|
31
|
+
fast_sentence_segment-1.4.0.dist-info/entry_points.txt,sha256=Zc8OwFKj3ofnjy5ZIFqHzDkIEWweV1AP1xap1ZFGD8M,107
|
|
32
|
+
fast_sentence_segment-1.4.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
{fast_sentence_segment-1.3.0.dist-info → fast_sentence_segment-1.4.0.dist-info}/entry_points.txt
RENAMED
|
File without changes
|