lt-utils 0.0.1.dev0__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.
- lt_utils-0.0.1.dev0/LICENSE +201 -0
- lt_utils-0.0.1.dev0/PKG-INFO +34 -0
- lt_utils-0.0.1.dev0/README.md +1 -0
- lt_utils-0.0.1.dev0/lt_utils/__init__.py +11 -0
- lt_utils-0.0.1.dev0/lt_utils/_internal/texts/nlp_stuff.py +96 -0
- lt_utils-0.0.1.dev0/lt_utils/_internal/texts/requoter.py +115 -0
- lt_utils-0.0.1.dev0/lt_utils/_internal/texts/splitter.py +88 -0
- lt_utils-0.0.1.dev0/lt_utils/common.py +102 -0
- lt_utils-0.0.1.dev0/lt_utils/files_ops.py +360 -0
- lt_utils-0.0.1.dev0/lt_utils/math_ops.py +139 -0
- lt_utils-0.0.1.dev0/lt_utils/misc_utils.py +333 -0
- lt_utils-0.0.1.dev0/lt_utils/texts_ops.py +955 -0
- lt_utils-0.0.1.dev0/lt_utils/types_utils.py +254 -0
- lt_utils-0.0.1.dev0/lt_utils.egg-info/PKG-INFO +34 -0
- lt_utils-0.0.1.dev0/lt_utils.egg-info/SOURCES.txt +18 -0
- lt_utils-0.0.1.dev0/lt_utils.egg-info/dependency_links.txt +1 -0
- lt_utils-0.0.1.dev0/lt_utils.egg-info/requires.txt +10 -0
- lt_utils-0.0.1.dev0/lt_utils.egg-info/top_level.txt +1 -0
- lt_utils-0.0.1.dev0/setup.cfg +4 -0
- lt_utils-0.0.1.dev0/setup.py +35 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: lt-utils
|
|
3
|
+
Version: 0.0.1.dev0
|
|
4
|
+
Summary: Collection of utilities for file, type, and basic logic handling across Python projects.
|
|
5
|
+
Home-page: https://github.com/gr1336/lt-utils/
|
|
6
|
+
Author: gr1336
|
|
7
|
+
License: Apache Software License (Apache-2.0)
|
|
8
|
+
Classifier: Development Status :: 1 - Planning
|
|
9
|
+
Classifier: Topic :: Software Development
|
|
10
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
11
|
+
Classifier: Topic :: Utilities
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: numpy>=1.26.4
|
|
15
|
+
Requires-Dist: markdown2==2.5.*
|
|
16
|
+
Requires-Dist: markdownify==1.1.0
|
|
17
|
+
Requires-Dist: pyperclip>=1.8.2
|
|
18
|
+
Requires-Dist: textblob>=0.18.0
|
|
19
|
+
Requires-Dist: pyyaml>=6.0.0
|
|
20
|
+
Requires-Dist: nltk==3.9.*
|
|
21
|
+
Requires-Dist: scikit-learn>=1.4.0
|
|
22
|
+
Requires-Dist: spacy<4,>3.5
|
|
23
|
+
Requires-Dist: numba<0.70,>=0.60.0
|
|
24
|
+
Dynamic: author
|
|
25
|
+
Dynamic: classifier
|
|
26
|
+
Dynamic: description
|
|
27
|
+
Dynamic: description-content-type
|
|
28
|
+
Dynamic: home-page
|
|
29
|
+
Dynamic: license
|
|
30
|
+
Dynamic: license-file
|
|
31
|
+
Dynamic: requires-dist
|
|
32
|
+
Dynamic: summary
|
|
33
|
+
|
|
34
|
+
Collection of utilities for file, type, and basic logic handling across Python projects.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Collection of utilities for file, type, and basic logic handling across Python projects.
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
__all__ = ["nlp_instance", "nltk"]
|
|
2
|
+
import subprocess
|
|
3
|
+
import nltk
|
|
4
|
+
import spacy
|
|
5
|
+
from ...common import *
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class NPLP_BASE:
|
|
9
|
+
"""To avoid re-trying to download nltk more than once per session"""
|
|
10
|
+
|
|
11
|
+
spc: spacy.Language = None
|
|
12
|
+
spc_model: str = None
|
|
13
|
+
|
|
14
|
+
nltk_modules = {
|
|
15
|
+
"stopwords": False,
|
|
16
|
+
"wordnet": False,
|
|
17
|
+
"cmudict": False,
|
|
18
|
+
"punkt": False,
|
|
19
|
+
"punkt_tab": False,
|
|
20
|
+
"averaged_perceptron_tagger": False,
|
|
21
|
+
"maxent_ne_chunker": False,
|
|
22
|
+
"words": False,
|
|
23
|
+
"maxent_ne_chunker_tab": False,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
def __init__(self):
|
|
27
|
+
self.is_initialized = False
|
|
28
|
+
|
|
29
|
+
def initialize_spacy(
|
|
30
|
+
self,
|
|
31
|
+
model: Optional[
|
|
32
|
+
Literal[
|
|
33
|
+
"en_core_web_md",
|
|
34
|
+
"en_core_web_lg",
|
|
35
|
+
]
|
|
36
|
+
] = None,
|
|
37
|
+
):
|
|
38
|
+
try:
|
|
39
|
+
if model is None:
|
|
40
|
+
model = "en_core_web_md"
|
|
41
|
+
if self.spc is None:
|
|
42
|
+
self.spc = spacy.load("en_core_web_md", disable=["parser", "ner"])
|
|
43
|
+
self.spc_model = "en_core_web_md"
|
|
44
|
+
return
|
|
45
|
+
|
|
46
|
+
if self.spc_model == model:
|
|
47
|
+
return
|
|
48
|
+
self.spc = spacy.load(model, disable=["parser", "ner"])
|
|
49
|
+
self.spc_model = model
|
|
50
|
+
except OSError:
|
|
51
|
+
print("Model not found, trying to install it!")
|
|
52
|
+
res = subprocess.run(
|
|
53
|
+
f"python -m spacy download {model}",
|
|
54
|
+
shell=True,
|
|
55
|
+
stdout=subprocess.DEVNULL,
|
|
56
|
+
stderr=subprocess.STDOUT,
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
self.spc = spacy.load(model, disable=["parser", "ner"])
|
|
60
|
+
self.spc_model = model
|
|
61
|
+
|
|
62
|
+
def initialize_nltk(
|
|
63
|
+
self,
|
|
64
|
+
module_name: Literal[
|
|
65
|
+
"stopwords",
|
|
66
|
+
"wordnet",
|
|
67
|
+
"cmudict",
|
|
68
|
+
"punkt",
|
|
69
|
+
"punkt_tab",
|
|
70
|
+
"averaged_perceptron_tagger",
|
|
71
|
+
"maxent_ne_chunker",
|
|
72
|
+
"words",
|
|
73
|
+
"maxent_ne_chunker_tab",
|
|
74
|
+
"all",
|
|
75
|
+
] = "all",
|
|
76
|
+
):
|
|
77
|
+
# TODO: Make this as standard way to handle installation, remove the "all" from the list of options, thus saving space.
|
|
78
|
+
# Removing the self.is_initialized and using 'nltk_modules' modules to check if they are already installled or not.
|
|
79
|
+
if self.is_initialized:
|
|
80
|
+
return
|
|
81
|
+
for _nllib in [
|
|
82
|
+
"stopwords",
|
|
83
|
+
"wordnet",
|
|
84
|
+
"cmudict",
|
|
85
|
+
"punkt",
|
|
86
|
+
"punkt_tab",
|
|
87
|
+
"averaged_perceptron_tagger",
|
|
88
|
+
"maxent_ne_chunker",
|
|
89
|
+
"words",
|
|
90
|
+
"maxent_ne_chunker_tab",
|
|
91
|
+
]:
|
|
92
|
+
nltk.download(_nllib, quiet=True)
|
|
93
|
+
self.is_initialized = True
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
nlp_instance = NPLP_BASE()
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
__all__ = ["requoter"]
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from typing import List
|
|
5
|
+
|
|
6
|
+
_SINGLE_QUOTES_MAP = {
|
|
7
|
+
96: "'",
|
|
8
|
+
8216: "'",
|
|
9
|
+
8217: "'",
|
|
10
|
+
8219: "'",
|
|
11
|
+
10075: "'",
|
|
12
|
+
10076: "'",
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
_DOUBLE_QUOTES_MAP = {
|
|
16
|
+
8220: '"',
|
|
17
|
+
8221: '"',
|
|
18
|
+
8222: '"',
|
|
19
|
+
10080: '"',
|
|
20
|
+
12317: '"',
|
|
21
|
+
12318: '"',
|
|
22
|
+
12319: '"',
|
|
23
|
+
65282: '"',
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _get_quotes(text: str) -> List[int]:
|
|
28
|
+
"""
|
|
29
|
+
Count and return the total quotes and their indexes in the given text.
|
|
30
|
+
|
|
31
|
+
Args:
|
|
32
|
+
text (str): The input text.
|
|
33
|
+
|
|
34
|
+
Returns:
|
|
35
|
+
Tuple[int, List[int]]: A tuple containing the total quotes and their indexes.
|
|
36
|
+
"""
|
|
37
|
+
return [i for i, char in enumerate(text) if char == '"']
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _clear_subquotes(text: str) -> str:
|
|
41
|
+
"""
|
|
42
|
+
Remove sub-quotes and return the modified text.
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
text (str): The input text.
|
|
46
|
+
|
|
47
|
+
Returns:
|
|
48
|
+
str: The modified text without sub-quotes.
|
|
49
|
+
"""
|
|
50
|
+
return re.sub("''", "'", text.translate(_SINGLE_QUOTES_MAP))
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _simplify_quotes(text: str, simplify_everything: bool = False) -> str:
|
|
54
|
+
"""
|
|
55
|
+
Replace special quotation marks with simple double quotes in the given text.
|
|
56
|
+
|
|
57
|
+
Args:
|
|
58
|
+
text (str): The input text.
|
|
59
|
+
|
|
60
|
+
Returns:
|
|
61
|
+
str: The text with special quotation marks replaced by simple double quotes.
|
|
62
|
+
"""
|
|
63
|
+
res = re.sub('""', '"', text.translate(_DOUBLE_QUOTES_MAP))
|
|
64
|
+
if simplify_everything:
|
|
65
|
+
return _clear_subquotes(res)
|
|
66
|
+
return res
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _update_text(positions: List[int], text: str, q1: str = "“", q2: str = "”") -> str:
|
|
70
|
+
"""
|
|
71
|
+
Update the text by enclosing specified positions with given quotation marks.
|
|
72
|
+
|
|
73
|
+
Args:
|
|
74
|
+
positions (List[int]): A list of positions where quotation marks should be applied.
|
|
75
|
+
text (str): The input text.
|
|
76
|
+
q1 (str, optional): The opening quotation mark. Defaults to "“".
|
|
77
|
+
q2 (str, optional): The closing quotation mark. Defaults to "”".
|
|
78
|
+
|
|
79
|
+
Returns:
|
|
80
|
+
str: The updated text with specified positions enclosed by the given quotation marks.
|
|
81
|
+
"""
|
|
82
|
+
split_version = [positions[i : i + 2] for i in range(0, len(positions), 2)]
|
|
83
|
+
for pair in split_version:
|
|
84
|
+
if len(pair) == 2:
|
|
85
|
+
text = (
|
|
86
|
+
text[: pair[0]]
|
|
87
|
+
+ q1
|
|
88
|
+
+ text[pair[0] + 1 : pair[1]]
|
|
89
|
+
+ q2
|
|
90
|
+
+ text[pair[1] + 1 :]
|
|
91
|
+
)
|
|
92
|
+
elif len(pair) == 1:
|
|
93
|
+
text = text[: pair[0]] + "" + text[pair[0] + 1 :]
|
|
94
|
+
return text
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def requoter(input_text: str, use_simple: bool = False) -> str:
|
|
98
|
+
"""
|
|
99
|
+
Process the input text, fixing and changing the quotation style.
|
|
100
|
+
|
|
101
|
+
Args:
|
|
102
|
+
input_text (str): The text input that will be converted.
|
|
103
|
+
use_simple (bool, optional): If `True`, It will return the text with the simply quotes instead of the special markings. Defaults to False.
|
|
104
|
+
|
|
105
|
+
Returns:
|
|
106
|
+
str: The processed text with fixed and changed quotation style.
|
|
107
|
+
"""
|
|
108
|
+
input_text = _simplify_quotes(input_text, use_simple)
|
|
109
|
+
if use_simple:
|
|
110
|
+
return input_text
|
|
111
|
+
|
|
112
|
+
positions = _get_quotes(input_text)
|
|
113
|
+
if not positions:
|
|
114
|
+
return input_text.replace("'", "’")
|
|
115
|
+
return _update_text(positions, input_text).replace("'", "’")
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
__all__ = ["SplitText"]
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class SplitText:
|
|
7
|
+
def __init__(self):
|
|
8
|
+
self._reset()
|
|
9
|
+
|
|
10
|
+
def _reset(self):
|
|
11
|
+
self.rv = []
|
|
12
|
+
self.split_pos = []
|
|
13
|
+
self.pos = -1
|
|
14
|
+
self.current = ""
|
|
15
|
+
self.in_quote = False
|
|
16
|
+
|
|
17
|
+
def seek(self, delta):
|
|
18
|
+
is_neg = delta < 0
|
|
19
|
+
for _ in range(abs(delta)):
|
|
20
|
+
if is_neg:
|
|
21
|
+
self.pos -= 1
|
|
22
|
+
self.current = self.current[:-1]
|
|
23
|
+
else:
|
|
24
|
+
self.pos += 1
|
|
25
|
+
self.current += self.text[self.pos]
|
|
26
|
+
if self.text[self.pos] in '"“”':
|
|
27
|
+
self.in_quote = not self.in_quote
|
|
28
|
+
return self.text[self.pos]
|
|
29
|
+
|
|
30
|
+
def peek(self, delta):
|
|
31
|
+
p = self.pos + delta
|
|
32
|
+
return self.text[p] if p < self.end_pos and p >= 0 else ""
|
|
33
|
+
|
|
34
|
+
def commit(self):
|
|
35
|
+
self.rv.append(self.current)
|
|
36
|
+
self.current = ""
|
|
37
|
+
self.split_pos = []
|
|
38
|
+
|
|
39
|
+
def __call__(
|
|
40
|
+
self,
|
|
41
|
+
text: str,
|
|
42
|
+
desired_length: int = 100,
|
|
43
|
+
max_length: int = 200,
|
|
44
|
+
) -> list[str]:
|
|
45
|
+
|
|
46
|
+
self._reset()
|
|
47
|
+
self.text = text
|
|
48
|
+
self.end_pos = len(self.text) - 1
|
|
49
|
+
self.max_length = max_length
|
|
50
|
+
self.desired_length = desired_length
|
|
51
|
+
while self.pos < self.end_pos:
|
|
52
|
+
c = self.seek(1)
|
|
53
|
+
if len(self.current) >= self.max_length:
|
|
54
|
+
if len(self.split_pos) > 0 and len(self.current) > (
|
|
55
|
+
self.desired_length / 2
|
|
56
|
+
):
|
|
57
|
+
d = self.pos - self.split_pos[-1]
|
|
58
|
+
self.seek(-d)
|
|
59
|
+
else:
|
|
60
|
+
while (
|
|
61
|
+
c not in "!?.\n "
|
|
62
|
+
and self.pos > 0
|
|
63
|
+
and len(self.current) > self.desired_length
|
|
64
|
+
):
|
|
65
|
+
c = self.seek(-1)
|
|
66
|
+
self.commit()
|
|
67
|
+
elif not self.in_quote and (
|
|
68
|
+
c in "!?\n" or (c == "." and self.peek(1) in "\n ")
|
|
69
|
+
):
|
|
70
|
+
while (
|
|
71
|
+
self.pos < len(self.text) - 1
|
|
72
|
+
and len(self.current) < self.max_length
|
|
73
|
+
and self.peek(1) in "!?."
|
|
74
|
+
):
|
|
75
|
+
c = self.seek(1)
|
|
76
|
+
self.split_pos.append(self.pos)
|
|
77
|
+
if len(self.current) >= self.desired_length:
|
|
78
|
+
self.commit()
|
|
79
|
+
elif self.in_quote and self.peek(1) == '"“”' and self.peek(2) in "\n ":
|
|
80
|
+
self.seek(2)
|
|
81
|
+
self.split_pos.append(self.pos)
|
|
82
|
+
self.rv.append(self.current)
|
|
83
|
+
self.rv = [s.strip() for s in self.rv]
|
|
84
|
+
self.rv = [
|
|
85
|
+
s for s in self.rv if len(s) > 0 and not re.match(r"^[\s\.,;:!?]*$", s)
|
|
86
|
+
]
|
|
87
|
+
|
|
88
|
+
return self.rv
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
__all__ = [
|
|
2
|
+
"ABC",
|
|
3
|
+
"abstractmethod",
|
|
4
|
+
"Union",
|
|
5
|
+
"Literal",
|
|
6
|
+
"Optional",
|
|
7
|
+
"Callable",
|
|
8
|
+
"Dict",
|
|
9
|
+
"List",
|
|
10
|
+
"Tuple",
|
|
11
|
+
"Mapping",
|
|
12
|
+
"Set",
|
|
13
|
+
"TypedDict",
|
|
14
|
+
"TypeAlias",
|
|
15
|
+
"AnyStr",
|
|
16
|
+
"Array",
|
|
17
|
+
"Path",
|
|
18
|
+
"Deque",
|
|
19
|
+
"Any",
|
|
20
|
+
"TypeGuard",
|
|
21
|
+
"TYPE_CHECKING",
|
|
22
|
+
"Annotated",
|
|
23
|
+
"ClassVar",
|
|
24
|
+
"Sequence",
|
|
25
|
+
"namedtuple",
|
|
26
|
+
"OrderedDict",
|
|
27
|
+
"overload",
|
|
28
|
+
"TypeVar",
|
|
29
|
+
"DefaultDict",
|
|
30
|
+
"AbstractSet",
|
|
31
|
+
"Generator",
|
|
32
|
+
"NamedTuple",
|
|
33
|
+
"PathType",
|
|
34
|
+
"Iterable",
|
|
35
|
+
"Iterator",
|
|
36
|
+
"Sized",
|
|
37
|
+
"Text",
|
|
38
|
+
"Self",
|
|
39
|
+
"NotRequired",
|
|
40
|
+
"NoReturn",
|
|
41
|
+
"PathLike",
|
|
42
|
+
"Unpack",
|
|
43
|
+
"Number",
|
|
44
|
+
"Type",
|
|
45
|
+
"T",
|
|
46
|
+
# related functions
|
|
47
|
+
"get_args",
|
|
48
|
+
"get_origin",
|
|
49
|
+
"get_overloads",
|
|
50
|
+
"get_type_hints",
|
|
51
|
+
]
|
|
52
|
+
|
|
53
|
+
from abc import ABC, abstractmethod
|
|
54
|
+
|
|
55
|
+
from typing import (
|
|
56
|
+
Any,
|
|
57
|
+
Union,
|
|
58
|
+
Optional,
|
|
59
|
+
Literal,
|
|
60
|
+
Callable,
|
|
61
|
+
Generator,
|
|
62
|
+
NamedTuple,
|
|
63
|
+
Iterable,
|
|
64
|
+
Iterator,
|
|
65
|
+
Sized,
|
|
66
|
+
Dict,
|
|
67
|
+
List,
|
|
68
|
+
Tuple,
|
|
69
|
+
Type,
|
|
70
|
+
Mapping,
|
|
71
|
+
Set,
|
|
72
|
+
Deque,
|
|
73
|
+
DefaultDict,
|
|
74
|
+
TypedDict,
|
|
75
|
+
TypeAlias,
|
|
76
|
+
TypeGuard,
|
|
77
|
+
NotRequired,
|
|
78
|
+
TYPE_CHECKING,
|
|
79
|
+
Sequence,
|
|
80
|
+
TypeVar,
|
|
81
|
+
Annotated,
|
|
82
|
+
AbstractSet,
|
|
83
|
+
NoReturn,
|
|
84
|
+
ClassVar,
|
|
85
|
+
Unpack,
|
|
86
|
+
Self,
|
|
87
|
+
overload,
|
|
88
|
+
get_args,
|
|
89
|
+
get_origin,
|
|
90
|
+
get_overloads,
|
|
91
|
+
get_type_hints,
|
|
92
|
+
)
|
|
93
|
+
from numbers import Number
|
|
94
|
+
from collections import namedtuple, OrderedDict
|
|
95
|
+
from pathlib import Path
|
|
96
|
+
from os import PathLike
|
|
97
|
+
|
|
98
|
+
T = TypeVar("T") # Any type.
|
|
99
|
+
PathType: TypeAlias = Union[PathLike, str, bytes]
|
|
100
|
+
Text: TypeAlias = Union[str, bytes]
|
|
101
|
+
AnyStr: TypeAlias = Union[bytes, str]
|
|
102
|
+
Array: TypeAlias = Union[list, tuple]
|