fast-sentence-segment 0.1.9__py3-none-any.whl → 1.1.8__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/__init__.py +18 -18
- fast_sentence_segment/bp/__init__.py +1 -1
- fast_sentence_segment/bp/segmenter.py +65 -68
- fast_sentence_segment/core/__init__.py +4 -0
- fast_sentence_segment/core/base_object.py +18 -0
- fast_sentence_segment/core/stopwatch.py +38 -0
- fast_sentence_segment/dmo/__init__.py +6 -6
- fast_sentence_segment/dmo/bullet_point_cleaner.py +55 -55
- fast_sentence_segment/dmo/delimiters_to_periods.py +37 -37
- fast_sentence_segment/dmo/newlines_to_periods.py +57 -57
- fast_sentence_segment/dmo/numbered_list_normalizer.py +53 -53
- fast_sentence_segment/dmo/post_process_sentences.py +48 -48
- fast_sentence_segment/dmo/spacy_doc_segmenter.py +101 -101
- fast_sentence_segment/svc/__init__.py +2 -2
- fast_sentence_segment/svc/perform_paragraph_segmentation.py +50 -50
- fast_sentence_segment/svc/perform_sentence_segmentation.py +129 -129
- fast_sentence_segment-1.1.8.dist-info/METADATA +146 -0
- fast_sentence_segment-1.1.8.dist-info/RECORD +20 -0
- {fast_sentence_segment-0.1.9.dist-info → fast_sentence_segment-1.1.8.dist-info}/WHEEL +1 -1
- fast_sentence_segment-1.1.8.dist-info/licenses/LICENSE +21 -0
- fast_sentence_segment-0.1.9.dist-info/METADATA +0 -54
- fast_sentence_segment-0.1.9.dist-info/RECORD +0 -16
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
from .bp import *
|
|
2
|
-
from .svc import *
|
|
3
|
-
from .dmo import *
|
|
4
|
-
|
|
5
|
-
from .bp.segmenter import Segmenter
|
|
6
|
-
|
|
7
|
-
segment = Segmenter().input_text
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
def segment_text(input_text: str, flatten: bool = False) -> list:
|
|
11
|
-
results = segment(input_text)
|
|
12
|
-
|
|
13
|
-
if flatten:
|
|
14
|
-
flat = []
|
|
15
|
-
[[flat.append(y) for y in x] for x in results]
|
|
16
|
-
return flat
|
|
17
|
-
|
|
18
|
-
return results
|
|
1
|
+
from .bp import *
|
|
2
|
+
from .svc import *
|
|
3
|
+
from .dmo import *
|
|
4
|
+
|
|
5
|
+
from .bp.segmenter import Segmenter
|
|
6
|
+
|
|
7
|
+
segment = Segmenter().input_text
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def segment_text(input_text: str, flatten: bool = False) -> list:
|
|
11
|
+
results = segment(input_text)
|
|
12
|
+
|
|
13
|
+
if flatten:
|
|
14
|
+
flat = []
|
|
15
|
+
[[flat.append(y) for y in x] for x in results]
|
|
16
|
+
return flat
|
|
17
|
+
|
|
18
|
+
return results
|
|
@@ -1 +1 @@
|
|
|
1
|
-
from .segmenter import Segmenter
|
|
1
|
+
from .segmenter import Segmenter
|
|
@@ -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
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""Base object providing logging functionality."""
|
|
3
|
+
|
|
4
|
+
import logging
|
|
5
|
+
|
|
6
|
+
logging.basicConfig(
|
|
7
|
+
format='%(asctime)s : %(levelname)s : %(filename)s : %(funcName)s() : %(lineno)d : %(message)s',
|
|
8
|
+
level=logging.DEBUG)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class BaseObject:
|
|
12
|
+
"""Base class providing logging capabilities."""
|
|
13
|
+
|
|
14
|
+
def __init__(self, component_name: str):
|
|
15
|
+
self.logger = logging.getLogger(component_name)
|
|
16
|
+
self.isEnabledForDebug = self.logger.isEnabledFor(logging.DEBUG)
|
|
17
|
+
self.isEnabledForInfo = self.logger.isEnabledFor(logging.INFO)
|
|
18
|
+
self.isEnabledForWarning = self.logger.isEnabledFor(logging.WARNING)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""Simple stopwatch for timing operations."""
|
|
3
|
+
|
|
4
|
+
import time
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Stopwatch:
|
|
8
|
+
"""A simple stopwatch for measuring elapsed time."""
|
|
9
|
+
|
|
10
|
+
def __init__(self):
|
|
11
|
+
self._start = time.perf_counter()
|
|
12
|
+
self._end = None
|
|
13
|
+
|
|
14
|
+
@property
|
|
15
|
+
def duration(self):
|
|
16
|
+
return self._end - self._start if self._end else time.perf_counter() - self._start
|
|
17
|
+
|
|
18
|
+
@property
|
|
19
|
+
def running(self):
|
|
20
|
+
return not self._end
|
|
21
|
+
|
|
22
|
+
def restart(self):
|
|
23
|
+
self._start = time.perf_counter()
|
|
24
|
+
self._end = None
|
|
25
|
+
return self
|
|
26
|
+
|
|
27
|
+
def stop(self):
|
|
28
|
+
if self.running:
|
|
29
|
+
self._end = time.perf_counter()
|
|
30
|
+
return self
|
|
31
|
+
|
|
32
|
+
def __str__(self):
|
|
33
|
+
ms = self.duration * 1000
|
|
34
|
+
if ms >= 1000:
|
|
35
|
+
return f'{ms / 1000:.2f}s'
|
|
36
|
+
if ms >= 1:
|
|
37
|
+
return f'{ms:.2f}ms'
|
|
38
|
+
return f'{ms * 1000:.2f}μs'
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
from .bullet_point_cleaner import BulletPointCleaner
|
|
2
|
-
from .delimiters_to_periods import DelimitersToPeriods
|
|
3
|
-
from .newlines_to_periods import NewlinesToPeriods
|
|
4
|
-
from .post_process_sentences import PostProcessStructure
|
|
5
|
-
from .spacy_doc_segmenter import SpacyDocSegmenter
|
|
6
|
-
from .numbered_list_normalizer import NumberedListNormalizer
|
|
1
|
+
from .bullet_point_cleaner import BulletPointCleaner
|
|
2
|
+
from .delimiters_to_periods import DelimitersToPeriods
|
|
3
|
+
from .newlines_to_periods import NewlinesToPeriods
|
|
4
|
+
from .post_process_sentences import PostProcessStructure
|
|
5
|
+
from .spacy_doc_segmenter import SpacyDocSegmenter
|
|
6
|
+
from .numbered_list_normalizer import NumberedListNormalizer
|
|
@@ -1,55 +1,55 @@
|
|
|
1
|
-
#!/usr/bin/env python
|
|
2
|
-
# -*- coding: UTF-8 -*-
|
|
3
|
-
""" Prevent Bullet Points from Triggering False Positive Segmentation """
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
from
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class BulletPointCleaner(BaseObject):
|
|
10
|
-
""" Prevent Bullet Points from Triggering False Positive Segmentation """
|
|
11
|
-
|
|
12
|
-
def __init__(self):
|
|
13
|
-
""" Change Log
|
|
14
|
-
|
|
15
|
-
Created:
|
|
16
|
-
30-Sept-2021
|
|
17
|
-
craigtrim@gmail.com
|
|
18
|
-
Updated:
|
|
19
|
-
19-Oct-2022
|
|
20
|
-
craigtrim@gmail.com
|
|
21
|
-
* clean up for segment_text_3_test.py
|
|
22
|
-
"""
|
|
23
|
-
BaseObject.__init__(self, __name__)
|
|
24
|
-
|
|
25
|
-
@staticmethod
|
|
26
|
-
def process(input_text: str) -> str:
|
|
27
|
-
"""
|
|
28
|
-
Purpose:
|
|
29
|
-
prevent numbered bullet points from triggering sentence detection
|
|
30
|
-
:param input_text:
|
|
31
|
-
any input text
|
|
32
|
-
:return:
|
|
33
|
-
preprocessed input text
|
|
34
|
-
"""
|
|
35
|
-
if input_text.startswith("-"):
|
|
36
|
-
input_text = input_text[1:] # segment_text_3_test.py
|
|
37
|
-
|
|
38
|
-
if " " in input_text:
|
|
39
|
-
input_text = input_text.replace(" ", " ")
|
|
40
|
-
|
|
41
|
-
# the replacement routine above leaves double '..' in the text
|
|
42
|
-
# this replacement will solve that
|
|
43
|
-
while ".." in input_text:
|
|
44
|
-
input_text = input_text.replace("..", ".")
|
|
45
|
-
|
|
46
|
-
while ". -" in input_text: # segment_text_3_test.py
|
|
47
|
-
input_text = input_text.replace(". -", ". ")
|
|
48
|
-
|
|
49
|
-
while ". . " in input_text:
|
|
50
|
-
input_text = input_text.replace(". . ", ".")
|
|
51
|
-
|
|
52
|
-
while ' ' in input_text:
|
|
53
|
-
input_text = input_text.replace(' ', ' ')
|
|
54
|
-
|
|
55
|
-
return input_text
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: UTF-8 -*-
|
|
3
|
+
""" Prevent Bullet Points from Triggering False Positive Segmentation """
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
from fast_sentence_segment.core import BaseObject
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class BulletPointCleaner(BaseObject):
|
|
10
|
+
""" Prevent Bullet Points from Triggering False Positive Segmentation """
|
|
11
|
+
|
|
12
|
+
def __init__(self):
|
|
13
|
+
""" Change Log
|
|
14
|
+
|
|
15
|
+
Created:
|
|
16
|
+
30-Sept-2021
|
|
17
|
+
craigtrim@gmail.com
|
|
18
|
+
Updated:
|
|
19
|
+
19-Oct-2022
|
|
20
|
+
craigtrim@gmail.com
|
|
21
|
+
* clean up for segment_text_3_test.py
|
|
22
|
+
"""
|
|
23
|
+
BaseObject.__init__(self, __name__)
|
|
24
|
+
|
|
25
|
+
@staticmethod
|
|
26
|
+
def process(input_text: str) -> str:
|
|
27
|
+
"""
|
|
28
|
+
Purpose:
|
|
29
|
+
prevent numbered bullet points from triggering sentence detection
|
|
30
|
+
:param input_text:
|
|
31
|
+
any input text
|
|
32
|
+
:return:
|
|
33
|
+
preprocessed input text
|
|
34
|
+
"""
|
|
35
|
+
if input_text.startswith("-"):
|
|
36
|
+
input_text = input_text[1:] # segment_text_3_test.py
|
|
37
|
+
|
|
38
|
+
if " " in input_text:
|
|
39
|
+
input_text = input_text.replace(" ", " ")
|
|
40
|
+
|
|
41
|
+
# the replacement routine above leaves double '..' in the text
|
|
42
|
+
# this replacement will solve that
|
|
43
|
+
while ".." in input_text:
|
|
44
|
+
input_text = input_text.replace("..", ".")
|
|
45
|
+
|
|
46
|
+
while ". -" in input_text: # segment_text_3_test.py
|
|
47
|
+
input_text = input_text.replace(". -", ". ")
|
|
48
|
+
|
|
49
|
+
while ". . " in input_text:
|
|
50
|
+
input_text = input_text.replace(". . ", ".")
|
|
51
|
+
|
|
52
|
+
while ' ' in input_text:
|
|
53
|
+
input_text = input_text.replace(' ', ' ')
|
|
54
|
+
|
|
55
|
+
return input_text
|
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
#!/usr/bin/env python
|
|
2
|
-
# -*- coding: UTF-8 -*-
|
|
3
|
-
""" Convert Delimiters into Periods """
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
from
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class DelimitersToPeriods(BaseObject):
|
|
10
|
-
""" Convert Delimiters into Periods """
|
|
11
|
-
|
|
12
|
-
def __init__(self):
|
|
13
|
-
"""
|
|
14
|
-
Created:
|
|
15
|
-
30-Sept-2021
|
|
16
|
-
"""
|
|
17
|
-
BaseObject.__init__(self, __name__)
|
|
18
|
-
|
|
19
|
-
@staticmethod
|
|
20
|
-
def process(input_text: str,
|
|
21
|
-
delimiter: str):
|
|
22
|
-
"""
|
|
23
|
-
Purpose:
|
|
24
|
-
Take a CSV list and transform to sentences
|
|
25
|
-
:param input_text:
|
|
26
|
-
:return:
|
|
27
|
-
"""
|
|
28
|
-
total_len = len(input_text)
|
|
29
|
-
total_delims = input_text.count(delimiter)
|
|
30
|
-
|
|
31
|
-
if total_delims == 0:
|
|
32
|
-
return input_text
|
|
33
|
-
|
|
34
|
-
if total_delims / total_len > 0.04:
|
|
35
|
-
return input_text.replace(delimiter, '.')
|
|
36
|
-
|
|
37
|
-
return input_text
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: UTF-8 -*-
|
|
3
|
+
""" Convert Delimiters into Periods """
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
from fast_sentence_segment.core import BaseObject
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class DelimitersToPeriods(BaseObject):
|
|
10
|
+
""" Convert Delimiters into Periods """
|
|
11
|
+
|
|
12
|
+
def __init__(self):
|
|
13
|
+
"""
|
|
14
|
+
Created:
|
|
15
|
+
30-Sept-2021
|
|
16
|
+
"""
|
|
17
|
+
BaseObject.__init__(self, __name__)
|
|
18
|
+
|
|
19
|
+
@staticmethod
|
|
20
|
+
def process(input_text: str,
|
|
21
|
+
delimiter: str):
|
|
22
|
+
"""
|
|
23
|
+
Purpose:
|
|
24
|
+
Take a CSV list and transform to sentences
|
|
25
|
+
:param input_text:
|
|
26
|
+
:return:
|
|
27
|
+
"""
|
|
28
|
+
total_len = len(input_text)
|
|
29
|
+
total_delims = input_text.count(delimiter)
|
|
30
|
+
|
|
31
|
+
if total_delims == 0:
|
|
32
|
+
return input_text
|
|
33
|
+
|
|
34
|
+
if total_delims / total_len > 0.04:
|
|
35
|
+
return input_text.replace(delimiter, '.')
|
|
36
|
+
|
|
37
|
+
return input_text
|
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
#!/usr/bin/env python
|
|
2
|
-
# -*- coding: UTF-8 -*-
|
|
3
|
-
""" Convert New Lines into Periods """
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
from
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class NewlinesToPeriods(BaseObject):
|
|
10
|
-
""" Convert New Lines into Periods """
|
|
11
|
-
|
|
12
|
-
def __init__(self):
|
|
13
|
-
"""
|
|
14
|
-
Created:
|
|
15
|
-
30-Sept-2021
|
|
16
|
-
"""
|
|
17
|
-
BaseObject.__init__(self, __name__)
|
|
18
|
-
|
|
19
|
-
@staticmethod
|
|
20
|
-
def process(input_text: str):
|
|
21
|
-
"""
|
|
22
|
-
Purpose:
|
|
23
|
-
Take a CSV list and transform to sentences
|
|
24
|
-
:param input_text:
|
|
25
|
-
:return:
|
|
26
|
-
"""
|
|
27
|
-
|
|
28
|
-
# def replace(input_text: str,
|
|
29
|
-
# variant: str,
|
|
30
|
-
# canon: str) -> str:
|
|
31
|
-
|
|
32
|
-
# v1 = f" {variant} "
|
|
33
|
-
# if v1 in input_text:
|
|
34
|
-
# return input_text.replace(
|
|
35
|
-
# v1, f" {canon} ")
|
|
36
|
-
|
|
37
|
-
# v2 = f"{variant} "
|
|
38
|
-
# if v2 in input_text:
|
|
39
|
-
# return input_text.replace(
|
|
40
|
-
# v2, f"{canon} ")
|
|
41
|
-
|
|
42
|
-
# v3 = f" {variant}"
|
|
43
|
-
# if v3 in input_text:
|
|
44
|
-
# return input_text.replace(
|
|
45
|
-
# v3, f" {canon}")
|
|
46
|
-
|
|
47
|
-
# return input_text
|
|
48
|
-
|
|
49
|
-
# result = replace(input_text=input_text,
|
|
50
|
-
# variant='\n',
|
|
51
|
-
# canon=' . ')
|
|
52
|
-
|
|
53
|
-
# 20230309; don't replace a newline with a period
|
|
54
|
-
# that too often causes confusion and puts a period where one should not exist
|
|
55
|
-
result = input_text.replace('\n', ' ')
|
|
56
|
-
|
|
57
|
-
return result
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: UTF-8 -*-
|
|
3
|
+
""" Convert New Lines into Periods """
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
from fast_sentence_segment.core import BaseObject
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class NewlinesToPeriods(BaseObject):
|
|
10
|
+
""" Convert New Lines into Periods """
|
|
11
|
+
|
|
12
|
+
def __init__(self):
|
|
13
|
+
"""
|
|
14
|
+
Created:
|
|
15
|
+
30-Sept-2021
|
|
16
|
+
"""
|
|
17
|
+
BaseObject.__init__(self, __name__)
|
|
18
|
+
|
|
19
|
+
@staticmethod
|
|
20
|
+
def process(input_text: str):
|
|
21
|
+
"""
|
|
22
|
+
Purpose:
|
|
23
|
+
Take a CSV list and transform to sentences
|
|
24
|
+
:param input_text:
|
|
25
|
+
:return:
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
# def replace(input_text: str,
|
|
29
|
+
# variant: str,
|
|
30
|
+
# canon: str) -> str:
|
|
31
|
+
|
|
32
|
+
# v1 = f" {variant} "
|
|
33
|
+
# if v1 in input_text:
|
|
34
|
+
# return input_text.replace(
|
|
35
|
+
# v1, f" {canon} ")
|
|
36
|
+
|
|
37
|
+
# v2 = f"{variant} "
|
|
38
|
+
# if v2 in input_text:
|
|
39
|
+
# return input_text.replace(
|
|
40
|
+
# v2, f"{canon} ")
|
|
41
|
+
|
|
42
|
+
# v3 = f" {variant}"
|
|
43
|
+
# if v3 in input_text:
|
|
44
|
+
# return input_text.replace(
|
|
45
|
+
# v3, f" {canon}")
|
|
46
|
+
|
|
47
|
+
# return input_text
|
|
48
|
+
|
|
49
|
+
# result = replace(input_text=input_text,
|
|
50
|
+
# variant='\n',
|
|
51
|
+
# canon=' . ')
|
|
52
|
+
|
|
53
|
+
# 20230309; don't replace a newline with a period
|
|
54
|
+
# that too often causes confusion and puts a period where one should not exist
|
|
55
|
+
result = input_text.replace('\n', ' ')
|
|
56
|
+
|
|
57
|
+
return result
|
|
@@ -1,53 +1,53 @@
|
|
|
1
|
-
#!/usr/bin/env python
|
|
2
|
-
# -*- coding: UTF-8 -*-
|
|
3
|
-
""" Normalize Numbered Lists to prevent False Positive Segmentation """
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
from
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class NumberedListNormalizer(BaseObject):
|
|
10
|
-
""" Normalize Numbered Lists to prevent False Positive Segmentation """
|
|
11
|
-
|
|
12
|
-
__d_candidate_list_elements = {
|
|
13
|
-
"1. ": "1_ ",
|
|
14
|
-
"2. ": "2_ ",
|
|
15
|
-
"3. ": "3_ ",
|
|
16
|
-
"4. ": "4_ ",
|
|
17
|
-
"5. ": "5_ ",
|
|
18
|
-
"6. ": "6_ ",
|
|
19
|
-
"7. ": "7_ ",
|
|
20
|
-
"8. ": "8_ ",
|
|
21
|
-
"9. ": "9_ ",
|
|
22
|
-
"10. ": "10_ ",
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
def __init__(self):
|
|
26
|
-
"""
|
|
27
|
-
Created:
|
|
28
|
-
19-Oct-2022
|
|
29
|
-
craigtrim@gmail.com
|
|
30
|
-
* https://github.com/craigtrim/fast-sentence-segment/issues/1
|
|
31
|
-
"""
|
|
32
|
-
BaseObject.__init__(self, __name__)
|
|
33
|
-
|
|
34
|
-
def process(self,
|
|
35
|
-
input_text: str,
|
|
36
|
-
denormalize: bool = False) -> str:
|
|
37
|
-
|
|
38
|
-
if not denormalize:
|
|
39
|
-
for candidate in self.__d_candidate_list_elements:
|
|
40
|
-
if candidate in input_text:
|
|
41
|
-
input_text = input_text.replace(
|
|
42
|
-
candidate, self.__d_candidate_list_elements[candidate])
|
|
43
|
-
|
|
44
|
-
else: # reverse the process
|
|
45
|
-
d_rev = {self.__d_candidate_list_elements[k]: k
|
|
46
|
-
for k in self.__d_candidate_list_elements}
|
|
47
|
-
|
|
48
|
-
for candidate in d_rev:
|
|
49
|
-
if candidate in input_text:
|
|
50
|
-
input_text = input_text.replace(
|
|
51
|
-
candidate, d_rev[candidate])
|
|
52
|
-
|
|
53
|
-
return input_text
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: UTF-8 -*-
|
|
3
|
+
""" Normalize Numbered Lists to prevent False Positive Segmentation """
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
from fast_sentence_segment.core import BaseObject
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class NumberedListNormalizer(BaseObject):
|
|
10
|
+
""" Normalize Numbered Lists to prevent False Positive Segmentation """
|
|
11
|
+
|
|
12
|
+
__d_candidate_list_elements = {
|
|
13
|
+
"1. ": "1_ ",
|
|
14
|
+
"2. ": "2_ ",
|
|
15
|
+
"3. ": "3_ ",
|
|
16
|
+
"4. ": "4_ ",
|
|
17
|
+
"5. ": "5_ ",
|
|
18
|
+
"6. ": "6_ ",
|
|
19
|
+
"7. ": "7_ ",
|
|
20
|
+
"8. ": "8_ ",
|
|
21
|
+
"9. ": "9_ ",
|
|
22
|
+
"10. ": "10_ ",
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
def __init__(self):
|
|
26
|
+
"""
|
|
27
|
+
Created:
|
|
28
|
+
19-Oct-2022
|
|
29
|
+
craigtrim@gmail.com
|
|
30
|
+
* https://github.com/craigtrim/fast-sentence-segment/issues/1
|
|
31
|
+
"""
|
|
32
|
+
BaseObject.__init__(self, __name__)
|
|
33
|
+
|
|
34
|
+
def process(self,
|
|
35
|
+
input_text: str,
|
|
36
|
+
denormalize: bool = False) -> str:
|
|
37
|
+
|
|
38
|
+
if not denormalize:
|
|
39
|
+
for candidate in self.__d_candidate_list_elements:
|
|
40
|
+
if candidate in input_text:
|
|
41
|
+
input_text = input_text.replace(
|
|
42
|
+
candidate, self.__d_candidate_list_elements[candidate])
|
|
43
|
+
|
|
44
|
+
else: # reverse the process
|
|
45
|
+
d_rev = {self.__d_candidate_list_elements[k]: k
|
|
46
|
+
for k in self.__d_candidate_list_elements}
|
|
47
|
+
|
|
48
|
+
for candidate in d_rev:
|
|
49
|
+
if candidate in input_text:
|
|
50
|
+
input_text = input_text.replace(
|
|
51
|
+
candidate, d_rev[candidate])
|
|
52
|
+
|
|
53
|
+
return input_text
|