webscout 7.0__py3-none-any.whl → 7.2__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.

Potentially problematic release.


This version of webscout might be problematic. Click here for more details.

Files changed (147) hide show
  1. webscout/AIauto.py +191 -191
  2. webscout/AIbase.py +122 -122
  3. webscout/AIutel.py +440 -440
  4. webscout/Bard.py +343 -161
  5. webscout/DWEBS.py +489 -492
  6. webscout/Extra/YTToolkit/YTdownloader.py +995 -995
  7. webscout/Extra/YTToolkit/__init__.py +2 -2
  8. webscout/Extra/YTToolkit/transcriber.py +476 -479
  9. webscout/Extra/YTToolkit/ytapi/channel.py +307 -307
  10. webscout/Extra/YTToolkit/ytapi/playlist.py +58 -58
  11. webscout/Extra/YTToolkit/ytapi/pool.py +7 -7
  12. webscout/Extra/YTToolkit/ytapi/utils.py +62 -62
  13. webscout/Extra/YTToolkit/ytapi/video.py +103 -103
  14. webscout/Extra/autocoder/__init__.py +9 -9
  15. webscout/Extra/autocoder/autocoder_utiles.py +199 -199
  16. webscout/Extra/autocoder/rawdog.py +5 -7
  17. webscout/Extra/autollama.py +230 -230
  18. webscout/Extra/gguf.py +3 -3
  19. webscout/Extra/weather.py +171 -171
  20. webscout/LLM.py +442 -442
  21. webscout/Litlogger/__init__.py +67 -681
  22. webscout/Litlogger/core/__init__.py +6 -0
  23. webscout/Litlogger/core/level.py +20 -0
  24. webscout/Litlogger/core/logger.py +123 -0
  25. webscout/Litlogger/handlers/__init__.py +12 -0
  26. webscout/Litlogger/handlers/console.py +50 -0
  27. webscout/Litlogger/handlers/file.py +143 -0
  28. webscout/Litlogger/handlers/network.py +174 -0
  29. webscout/Litlogger/styles/__init__.py +7 -0
  30. webscout/Litlogger/styles/colors.py +231 -0
  31. webscout/Litlogger/styles/formats.py +377 -0
  32. webscout/Litlogger/styles/text.py +87 -0
  33. webscout/Litlogger/utils/__init__.py +6 -0
  34. webscout/Litlogger/utils/detectors.py +154 -0
  35. webscout/Litlogger/utils/formatters.py +200 -0
  36. webscout/Provider/AISEARCH/DeepFind.py +250 -250
  37. webscout/Provider/Blackboxai.py +136 -137
  38. webscout/Provider/ChatGPTGratis.py +226 -0
  39. webscout/Provider/Cloudflare.py +91 -78
  40. webscout/Provider/DeepSeek.py +218 -0
  41. webscout/Provider/Deepinfra.py +59 -35
  42. webscout/Provider/Free2GPT.py +131 -124
  43. webscout/Provider/Gemini.py +100 -115
  44. webscout/Provider/Glider.py +74 -59
  45. webscout/Provider/Groq.py +30 -18
  46. webscout/Provider/Jadve.py +108 -77
  47. webscout/Provider/Llama3.py +117 -94
  48. webscout/Provider/Marcus.py +191 -137
  49. webscout/Provider/Netwrck.py +62 -50
  50. webscout/Provider/PI.py +79 -124
  51. webscout/Provider/PizzaGPT.py +129 -83
  52. webscout/Provider/QwenLM.py +311 -0
  53. webscout/Provider/TTI/AiForce/__init__.py +22 -22
  54. webscout/Provider/TTI/AiForce/async_aiforce.py +257 -257
  55. webscout/Provider/TTI/AiForce/sync_aiforce.py +242 -242
  56. webscout/Provider/TTI/Nexra/__init__.py +22 -22
  57. webscout/Provider/TTI/Nexra/async_nexra.py +286 -286
  58. webscout/Provider/TTI/Nexra/sync_nexra.py +258 -258
  59. webscout/Provider/TTI/PollinationsAI/__init__.py +23 -23
  60. webscout/Provider/TTI/PollinationsAI/async_pollinations.py +330 -330
  61. webscout/Provider/TTI/PollinationsAI/sync_pollinations.py +285 -285
  62. webscout/Provider/TTI/artbit/__init__.py +22 -22
  63. webscout/Provider/TTI/artbit/async_artbit.py +184 -184
  64. webscout/Provider/TTI/artbit/sync_artbit.py +176 -176
  65. webscout/Provider/TTI/blackbox/__init__.py +4 -4
  66. webscout/Provider/TTI/blackbox/async_blackbox.py +212 -212
  67. webscout/Provider/TTI/blackbox/sync_blackbox.py +199 -199
  68. webscout/Provider/TTI/deepinfra/__init__.py +4 -4
  69. webscout/Provider/TTI/deepinfra/async_deepinfra.py +227 -227
  70. webscout/Provider/TTI/deepinfra/sync_deepinfra.py +199 -199
  71. webscout/Provider/TTI/huggingface/__init__.py +22 -22
  72. webscout/Provider/TTI/huggingface/async_huggingface.py +199 -199
  73. webscout/Provider/TTI/huggingface/sync_huggingface.py +195 -195
  74. webscout/Provider/TTI/imgninza/__init__.py +4 -4
  75. webscout/Provider/TTI/imgninza/async_ninza.py +214 -214
  76. webscout/Provider/TTI/imgninza/sync_ninza.py +209 -209
  77. webscout/Provider/TTI/talkai/__init__.py +4 -4
  78. webscout/Provider/TTI/talkai/async_talkai.py +229 -229
  79. webscout/Provider/TTI/talkai/sync_talkai.py +207 -207
  80. webscout/Provider/TTS/deepgram.py +182 -182
  81. webscout/Provider/TTS/elevenlabs.py +136 -136
  82. webscout/Provider/TTS/gesserit.py +150 -150
  83. webscout/Provider/TTS/murfai.py +138 -138
  84. webscout/Provider/TTS/parler.py +133 -134
  85. webscout/Provider/TTS/streamElements.py +360 -360
  86. webscout/Provider/TTS/utils.py +280 -280
  87. webscout/Provider/TTS/voicepod.py +116 -116
  88. webscout/Provider/TextPollinationsAI.py +74 -47
  89. webscout/Provider/WiseCat.py +193 -0
  90. webscout/Provider/__init__.py +144 -136
  91. webscout/Provider/cerebras.py +242 -227
  92. webscout/Provider/chatglm.py +204 -204
  93. webscout/Provider/dgaf.py +67 -39
  94. webscout/Provider/gaurish.py +105 -66
  95. webscout/Provider/geminiapi.py +208 -208
  96. webscout/Provider/granite.py +223 -0
  97. webscout/Provider/hermes.py +218 -218
  98. webscout/Provider/llama3mitril.py +179 -179
  99. webscout/Provider/llamatutor.py +72 -62
  100. webscout/Provider/llmchat.py +60 -35
  101. webscout/Provider/meta.py +794 -794
  102. webscout/Provider/multichat.py +331 -230
  103. webscout/Provider/typegpt.py +359 -356
  104. webscout/Provider/yep.py +5 -5
  105. webscout/__main__.py +5 -5
  106. webscout/cli.py +319 -319
  107. webscout/conversation.py +241 -242
  108. webscout/exceptions.py +328 -328
  109. webscout/litagent/__init__.py +28 -28
  110. webscout/litagent/agent.py +2 -3
  111. webscout/litprinter/__init__.py +0 -58
  112. webscout/scout/__init__.py +8 -8
  113. webscout/scout/core.py +884 -884
  114. webscout/scout/element.py +459 -459
  115. webscout/scout/parsers/__init__.py +69 -69
  116. webscout/scout/parsers/html5lib_parser.py +172 -172
  117. webscout/scout/parsers/html_parser.py +236 -236
  118. webscout/scout/parsers/lxml_parser.py +178 -178
  119. webscout/scout/utils.py +38 -38
  120. webscout/swiftcli/__init__.py +811 -811
  121. webscout/update_checker.py +2 -12
  122. webscout/version.py +1 -1
  123. webscout/webscout_search.py +1142 -1140
  124. webscout/webscout_search_async.py +635 -635
  125. webscout/zeroart/__init__.py +54 -54
  126. webscout/zeroart/base.py +60 -60
  127. webscout/zeroart/effects.py +99 -99
  128. webscout/zeroart/fonts.py +816 -816
  129. {webscout-7.0.dist-info → webscout-7.2.dist-info}/METADATA +21 -28
  130. webscout-7.2.dist-info/RECORD +217 -0
  131. webstoken/__init__.py +30 -30
  132. webstoken/classifier.py +189 -189
  133. webstoken/keywords.py +216 -216
  134. webstoken/language.py +128 -128
  135. webstoken/ner.py +164 -164
  136. webstoken/normalizer.py +35 -35
  137. webstoken/processor.py +77 -77
  138. webstoken/sentiment.py +206 -206
  139. webstoken/stemmer.py +73 -73
  140. webstoken/tagger.py +60 -60
  141. webstoken/tokenizer.py +158 -158
  142. webscout/Provider/RUBIKSAI.py +0 -272
  143. webscout-7.0.dist-info/RECORD +0 -199
  144. {webscout-7.0.dist-info → webscout-7.2.dist-info}/LICENSE.md +0 -0
  145. {webscout-7.0.dist-info → webscout-7.2.dist-info}/WHEEL +0 -0
  146. {webscout-7.0.dist-info → webscout-7.2.dist-info}/entry_points.txt +0 -0
  147. {webscout-7.0.dist-info → webscout-7.2.dist-info}/top_level.txt +0 -0
@@ -1,280 +1,280 @@
1
- """
2
- Text processing utilities for TTS providers.
3
- """
4
- from typing import List, Dict, Tuple, Set, Optional, Pattern
5
- import re
6
-
7
-
8
- class SentenceTokenizer:
9
- """Advanced sentence tokenizer with support for complex cases and proper formatting."""
10
-
11
- def __init__(self) -> None:
12
- # Common abbreviations by category
13
- self.TITLES: Set[str] = {
14
- 'mr', 'mrs', 'ms', 'dr', 'prof', 'rev', 'sr', 'jr', 'esq',
15
- 'hon', 'pres', 'gov', 'atty', 'supt', 'det', 'rev', 'col','maj', 'gen', 'capt', 'cmdr',
16
- 'lt', 'sgt', 'cpl', 'pvt'
17
- }
18
-
19
- self.ACADEMIC: Set[str] = {
20
- 'ph.d', 'phd', 'm.d', 'md', 'b.a', 'ba', 'm.a', 'ma', 'd.d.s', 'dds',
21
- 'm.b.a', 'mba', 'b.sc', 'bsc', 'm.sc', 'msc', 'llb', 'll.b', 'bl'
22
- }
23
-
24
- self.ORGANIZATIONS: Set[str] = {
25
- 'inc', 'ltd', 'co', 'corp', 'llc', 'llp', 'assn', 'bros', 'plc', 'cos',
26
- 'intl', 'dept', 'est', 'dist', 'mfg', 'div'
27
- }
28
-
29
- self.MONTHS: Set[str] = {
30
- 'jan', 'feb', 'mar', 'apr', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'
31
- }
32
-
33
- self.UNITS: Set[str] = {
34
- 'oz', 'pt', 'qt', 'gal', 'ml', 'cc', 'km', 'cm', 'mm', 'ft', 'in',
35
- 'kg', 'lb', 'lbs', 'hz', 'khz', 'mhz', 'ghz', 'kb', 'mb', 'gb', 'tb'
36
- }
37
-
38
- self.TECHNOLOGY: Set[str] = {
39
- 'v', 'ver', 'app', 'sys', 'dir', 'exe', 'lib', 'api', 'sdk', 'url',
40
- 'cpu', 'gpu', 'ram', 'rom', 'hdd', 'ssd', 'lan', 'wan', 'sql', 'html'
41
- }
42
-
43
- self.MISC: Set[str] = {
44
- 'vs', 'etc', 'ie', 'eg', 'no', 'al', 'ca', 'cf', 'pp', 'est', 'st',
45
- 'approx', 'appt', 'apt', 'dept', 'depts', 'min', 'max', 'avg'
46
- }
47
-
48
- # Combine all abbreviations
49
- self.all_abbreviations: Set[str] = (
50
- self.TITLES | self.ACADEMIC | self.ORGANIZATIONS |
51
- self.MONTHS | self.UNITS | self.TECHNOLOGY | self.MISC
52
- )
53
-
54
- # Special patterns
55
- self.ELLIPSIS: str = r'\.{2,}|…'
56
- self.URL_PATTERN: str = (
57
- r'(?:https?:\/\/|www\.)[\w\-\.]+\.[a-zA-Z]{2,}(?:\/[^\s]*)?'
58
- )
59
- self.EMAIL_PATTERN: str = r'[\w\.-]+@[\w\.-]+\.\w+'
60
- self.NUMBER_PATTERN: str = (
61
- r'\d+(?:\.\d+)?(?:%|°|km|cm|mm|m|kg|g|lb|ft|in|mph|kmh|hz|mhz|ghz)?'
62
- )
63
-
64
- # Quote and bracket pairs
65
- self.QUOTE_PAIRS: Dict[str, str] = {
66
- '"': '"', "'": "'", '"': '"', "「": "」", "『": "』",
67
- "«": "»", "‹": "›", "'": "'", "‚": "'"
68
- }
69
-
70
- self.BRACKETS: Dict[str, str] = {
71
- '(': ')', '[': ']', '{': '}', '⟨': '⟩', '「': '」',
72
- '『': '』', '【': '】', '〖': '〗', '「': '」'
73
- }
74
-
75
- # Compile regex patterns
76
- self._compile_patterns()
77
-
78
- def _compile_patterns(self) -> None:
79
- """Compile regex patterns for better performance."""
80
- # Pattern for finding potential sentence boundaries
81
- self.SENTENCE_END: Pattern = re.compile(
82
- r'''
83
- # Group for sentence endings
84
- (?:
85
- # Standard endings with optional quotes/brackets
86
- (?<=[.!?])[\"\'\)\]\}»›」』\s]*
87
-
88
- # Ellipsis
89
- |(?:\.{2,}|…)
90
-
91
- # Asian-style endings
92
- |(?<=[。!?」』】\s])
93
- )
94
-
95
- # Must be followed by whitespace and capital letter or number
96
- (?=\s+(?:[A-Z0-9]|["'({[\[「『《‹〈][A-Z]))
97
- ''',
98
- re.VERBOSE
99
- )
100
-
101
- # Pattern for abbreviations
102
- abbrev_pattern = '|'.join(re.escape(abbr) for abbr in self.all_abbreviations)
103
- self.ABBREV_PATTERN: Pattern = re.compile(
104
- fr'\b(?:{abbrev_pattern})\.?',
105
- re.IGNORECASE
106
- )
107
-
108
- def _protect_special_cases(self, text: str) -> Tuple[str, Dict[str, str]]:
109
- """Protect URLs, emails, and other special cases from being split."""
110
- protected = text
111
- placeholders: Dict[str, str] = {}
112
- counter = 0
113
-
114
- # Protect URLs and emails
115
- for pattern in [self.URL_PATTERN, self.EMAIL_PATTERN]:
116
- for match in re.finditer(pattern, protected):
117
- placeholder = f'__PROTECTED_{counter}__'
118
- placeholders[placeholder] = match.group()
119
- protected = protected.replace(match.group(), placeholder)
120
- counter += 1
121
-
122
- # Protect quoted content
123
- stack = []
124
- protected_chars = list(protected)
125
- i = 0
126
- while i < len(protected_chars):
127
- char = protected_chars[i]
128
- if char in self.QUOTE_PAIRS:
129
- stack.append((char, i))
130
- elif stack and char == self.QUOTE_PAIRS[stack[-1][0]]:
131
- start_quote, start_idx = stack.pop()
132
- content = ''.join(protected_chars[start_idx:i + 1])
133
- placeholder = f'__PROTECTED_{counter}__'
134
- placeholders[placeholder] = content
135
- protected_chars[start_idx:i + 1] = list(placeholder)
136
- counter += 1
137
- i += 1
138
-
139
- return ''.join(protected_chars), placeholders
140
-
141
- def _restore_special_cases(self, text: str, placeholders: Dict[str, str]) -> str:
142
- """Restore protected content."""
143
- restored = text
144
- for placeholder, original in placeholders.items():
145
- restored = restored.replace(placeholder, original)
146
- return restored
147
-
148
- def _handle_abbreviations(self, text: str) -> str:
149
- """Handle abbreviations to prevent incorrect sentence splitting."""
150
- def replace_abbrev(match: re.Match) -> str:
151
- abbr = match.group().lower().rstrip('.')
152
- if abbr in self.all_abbreviations:
153
- return match.group().replace('.', '__DOT__')
154
- return match.group()
155
-
156
- return self.ABBREV_PATTERN.sub(replace_abbrev, text)
157
-
158
- def _normalize_whitespace(self, text: str) -> str:
159
- """Normalize whitespace while preserving paragraph breaks."""
160
- # Replace multiple newlines with special marker
161
- text = re.sub(r'\n\s*\n', ' __PARA__ ', text)
162
- # Normalize remaining whitespace
163
- text = re.sub(r'\s+', ' ', text)
164
- return text.strip()
165
-
166
- def _restore_formatting(self, sentences: List[str]) -> List[str]:
167
- """Restore original formatting and clean up sentences."""
168
- restored = []
169
- for sentence in sentences:
170
- # Restore dots in abbreviations
171
- sentence = sentence.replace('__DOT__', '.')
172
-
173
- # Restore paragraph breaks
174
- sentence = sentence.replace('__PARA__', '\n\n')
175
-
176
- # Clean up whitespace
177
- sentence = re.sub(r'\s+', ' ', sentence).strip()
178
-
179
- # Capitalize first letter if it's lowercase and not an abbreviation
180
- words = sentence.split()
181
- if words and words[0].lower() not in self.all_abbreviations:
182
- sentence = sentence[0].upper() + sentence[1:]
183
-
184
- if sentence:
185
- restored.append(sentence)
186
-
187
- return restored
188
-
189
- def tokenize(self, text: str) -> List[str]:
190
- """
191
- Split text into sentences while handling complex cases.
192
-
193
- Args:
194
- text (str): Input text to split into sentences.
195
-
196
- Returns:
197
- List[str]: List of properly formatted sentences.
198
- """
199
- if not text or not text.strip():
200
- return []
201
-
202
- # Step 1: Protect special cases
203
- protected_text, placeholders = self._protect_special_cases(text)
204
-
205
- # Step 2: Normalize whitespace
206
- protected_text = self._normalize_whitespace(protected_text)
207
-
208
- # Step 3: Handle abbreviations
209
- protected_text = self._handle_abbreviations(protected_text)
210
-
211
- # Step 4: Split into potential sentences
212
- potential_sentences = self.SENTENCE_END.split(protected_text)
213
-
214
- # Step 5: Process and restore formatting
215
- sentences = self._restore_formatting(potential_sentences)
216
-
217
- # Step 6: Restore special cases
218
- sentences = [self._restore_special_cases(s, placeholders) for s in sentences]
219
-
220
- # Step 7: Post-process sentences
221
- final_sentences = []
222
- current_sentence = []
223
-
224
- for sentence in sentences:
225
- # Skip empty sentences
226
- if not sentence.strip():
227
- continue
228
-
229
- # Check if sentence might be continuation of previous
230
- if current_sentence and sentence[0].islower():
231
- current_sentence.append(sentence)
232
- else:
233
- if current_sentence:
234
- final_sentences.append(' '.join(current_sentence))
235
- current_sentence = [sentence]
236
-
237
- # Add last sentence if exists
238
- if current_sentence:
239
- final_sentences.append(' '.join(current_sentence))
240
-
241
- return final_sentences
242
-
243
-
244
- def split_sentences(text: str) -> List[str]:
245
- """
246
- Convenience function to split text into sentences using SentenceTokenizer.
247
-
248
- Args:
249
- text (str): Input text to split into sentences.
250
-
251
- Returns:
252
- List[str]: List of properly formatted sentences.
253
- """
254
- tokenizer = SentenceTokenizer()
255
- return tokenizer.tokenize(text)
256
-
257
-
258
- if __name__ == "__main__":
259
- # Test text with various challenging cases
260
- test_text: str = """
261
- Dr. Smith (Ph.D., M.D.) visited Washington D.C. on Jan. 20, 2024! He met with Prof. Johnson at 3:30 p.m.
262
- They discussed A.I. and machine learning... "What about the U.S. market?" asked Dr. Smith.
263
- The meeting ended at 5 p.m. Later, they went to Mr. Wilson's house (located at 123 Main St.) for dinner.
264
-
265
- Visit our website at https://www.example.com or email us at test@example.com!
266
- The temperature was 72.5°F (22.5°C). The company's Q3 2023 revenue was $12.5M USD.
267
-
268
- 「これは日本語の文章です。」This is a mixed-language text! How cool is that?
269
-
270
- Some technical specs: CPU: 3.5GHz, RAM: 16GB, Storage: 2TB SSD.
271
- Common abbreviations: etc., i.e., e.g., vs., cf., approx. 100 units.
272
- """
273
-
274
- # Process and print each sentence
275
- sentences: List[str] = split_sentences(test_text)
276
- print("Detected sentences:")
277
- print("-" * 80)
278
- for i, sentence in enumerate(sentences, 1):
279
- print(f"{i}. {sentence}")
280
- print("-" * 80)
1
+ """
2
+ Text processing utilities for TTS providers.
3
+ """
4
+ from typing import List, Dict, Tuple, Set, Optional, Pattern
5
+ import re
6
+
7
+
8
+ class SentenceTokenizer:
9
+ """Advanced sentence tokenizer with support for complex cases and proper formatting."""
10
+
11
+ def __init__(self) -> None:
12
+ # Common abbreviations by category
13
+ self.TITLES: Set[str] = {
14
+ 'mr', 'mrs', 'ms', 'dr', 'prof', 'rev', 'sr', 'jr', 'esq',
15
+ 'hon', 'pres', 'gov', 'atty', 'supt', 'det', 'rev', 'col','maj', 'gen', 'capt', 'cmdr',
16
+ 'lt', 'sgt', 'cpl', 'pvt'
17
+ }
18
+
19
+ self.ACADEMIC: Set[str] = {
20
+ 'ph.d', 'phd', 'm.d', 'md', 'b.a', 'ba', 'm.a', 'ma', 'd.d.s', 'dds',
21
+ 'm.b.a', 'mba', 'b.sc', 'bsc', 'm.sc', 'msc', 'llb', 'll.b', 'bl'
22
+ }
23
+
24
+ self.ORGANIZATIONS: Set[str] = {
25
+ 'inc', 'ltd', 'co', 'corp', 'llc', 'llp', 'assn', 'bros', 'plc', 'cos',
26
+ 'intl', 'dept', 'est', 'dist', 'mfg', 'div'
27
+ }
28
+
29
+ self.MONTHS: Set[str] = {
30
+ 'jan', 'feb', 'mar', 'apr', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'
31
+ }
32
+
33
+ self.UNITS: Set[str] = {
34
+ 'oz', 'pt', 'qt', 'gal', 'ml', 'cc', 'km', 'cm', 'mm', 'ft', 'in',
35
+ 'kg', 'lb', 'lbs', 'hz', 'khz', 'mhz', 'ghz', 'kb', 'mb', 'gb', 'tb'
36
+ }
37
+
38
+ self.TECHNOLOGY: Set[str] = {
39
+ 'v', 'ver', 'app', 'sys', 'dir', 'exe', 'lib', 'api', 'sdk', 'url',
40
+ 'cpu', 'gpu', 'ram', 'rom', 'hdd', 'ssd', 'lan', 'wan', 'sql', 'html'
41
+ }
42
+
43
+ self.MISC: Set[str] = {
44
+ 'vs', 'etc', 'ie', 'eg', 'no', 'al', 'ca', 'cf', 'pp', 'est', 'st',
45
+ 'approx', 'appt', 'apt', 'dept', 'depts', 'min', 'max', 'avg'
46
+ }
47
+
48
+ # Combine all abbreviations
49
+ self.all_abbreviations: Set[str] = (
50
+ self.TITLES | self.ACADEMIC | self.ORGANIZATIONS |
51
+ self.MONTHS | self.UNITS | self.TECHNOLOGY | self.MISC
52
+ )
53
+
54
+ # Special patterns
55
+ self.ELLIPSIS: str = r'\.{2,}|…'
56
+ self.URL_PATTERN: str = (
57
+ r'(?:https?:\/\/|www\.)[\w\-\.]+\.[a-zA-Z]{2,}(?:\/[^\s]*)?'
58
+ )
59
+ self.EMAIL_PATTERN: str = r'[\w\.-]+@[\w\.-]+\.\w+'
60
+ self.NUMBER_PATTERN: str = (
61
+ r'\d+(?:\.\d+)?(?:%|°|km|cm|mm|m|kg|g|lb|ft|in|mph|kmh|hz|mhz|ghz)?'
62
+ )
63
+
64
+ # Quote and bracket pairs
65
+ self.QUOTE_PAIRS: Dict[str, str] = {
66
+ '"': '"', "'": "'", '"': '"', "「": "」", "『": "』",
67
+ "«": "»", "‹": "›", "'": "'", "‚": "'"
68
+ }
69
+
70
+ self.BRACKETS: Dict[str, str] = {
71
+ '(': ')', '[': ']', '{': '}', '⟨': '⟩', '「': '」',
72
+ '『': '』', '【': '】', '〖': '〗', '「': '」'
73
+ }
74
+
75
+ # Compile regex patterns
76
+ self._compile_patterns()
77
+
78
+ def _compile_patterns(self) -> None:
79
+ """Compile regex patterns for better performance."""
80
+ # Pattern for finding potential sentence boundaries
81
+ self.SENTENCE_END: Pattern = re.compile(
82
+ r'''
83
+ # Group for sentence endings
84
+ (?:
85
+ # Standard endings with optional quotes/brackets
86
+ (?<=[.!?])[\"\'\)\]\}»›」』\s]*
87
+
88
+ # Ellipsis
89
+ |(?:\.{2,}|…)
90
+
91
+ # Asian-style endings
92
+ |(?<=[。!?」』】\s])
93
+ )
94
+
95
+ # Must be followed by whitespace and capital letter or number
96
+ (?=\s+(?:[A-Z0-9]|["'({[\[「『《‹〈][A-Z]))
97
+ ''',
98
+ re.VERBOSE
99
+ )
100
+
101
+ # Pattern for abbreviations
102
+ abbrev_pattern = '|'.join(re.escape(abbr) for abbr in self.all_abbreviations)
103
+ self.ABBREV_PATTERN: Pattern = re.compile(
104
+ fr'\b(?:{abbrev_pattern})\.?',
105
+ re.IGNORECASE
106
+ )
107
+
108
+ def _protect_special_cases(self, text: str) -> Tuple[str, Dict[str, str]]:
109
+ """Protect URLs, emails, and other special cases from being split."""
110
+ protected = text
111
+ placeholders: Dict[str, str] = {}
112
+ counter = 0
113
+
114
+ # Protect URLs and emails
115
+ for pattern in [self.URL_PATTERN, self.EMAIL_PATTERN]:
116
+ for match in re.finditer(pattern, protected):
117
+ placeholder = f'__PROTECTED_{counter}__'
118
+ placeholders[placeholder] = match.group()
119
+ protected = protected.replace(match.group(), placeholder)
120
+ counter += 1
121
+
122
+ # Protect quoted content
123
+ stack = []
124
+ protected_chars = list(protected)
125
+ i = 0
126
+ while i < len(protected_chars):
127
+ char = protected_chars[i]
128
+ if char in self.QUOTE_PAIRS:
129
+ stack.append((char, i))
130
+ elif stack and char == self.QUOTE_PAIRS[stack[-1][0]]:
131
+ start_quote, start_idx = stack.pop()
132
+ content = ''.join(protected_chars[start_idx:i + 1])
133
+ placeholder = f'__PROTECTED_{counter}__'
134
+ placeholders[placeholder] = content
135
+ protected_chars[start_idx:i + 1] = list(placeholder)
136
+ counter += 1
137
+ i += 1
138
+
139
+ return ''.join(protected_chars), placeholders
140
+
141
+ def _restore_special_cases(self, text: str, placeholders: Dict[str, str]) -> str:
142
+ """Restore protected content."""
143
+ restored = text
144
+ for placeholder, original in placeholders.items():
145
+ restored = restored.replace(placeholder, original)
146
+ return restored
147
+
148
+ def _handle_abbreviations(self, text: str) -> str:
149
+ """Handle abbreviations to prevent incorrect sentence splitting."""
150
+ def replace_abbrev(match: re.Match) -> str:
151
+ abbr = match.group().lower().rstrip('.')
152
+ if abbr in self.all_abbreviations:
153
+ return match.group().replace('.', '__DOT__')
154
+ return match.group()
155
+
156
+ return self.ABBREV_PATTERN.sub(replace_abbrev, text)
157
+
158
+ def _normalize_whitespace(self, text: str) -> str:
159
+ """Normalize whitespace while preserving paragraph breaks."""
160
+ # Replace multiple newlines with special marker
161
+ text = re.sub(r'\n\s*\n', ' __PARA__ ', text)
162
+ # Normalize remaining whitespace
163
+ text = re.sub(r'\s+', ' ', text)
164
+ return text.strip()
165
+
166
+ def _restore_formatting(self, sentences: List[str]) -> List[str]:
167
+ """Restore original formatting and clean up sentences."""
168
+ restored = []
169
+ for sentence in sentences:
170
+ # Restore dots in abbreviations
171
+ sentence = sentence.replace('__DOT__', '.')
172
+
173
+ # Restore paragraph breaks
174
+ sentence = sentence.replace('__PARA__', '\n\n')
175
+
176
+ # Clean up whitespace
177
+ sentence = re.sub(r'\s+', ' ', sentence).strip()
178
+
179
+ # Capitalize first letter if it's lowercase and not an abbreviation
180
+ words = sentence.split()
181
+ if words and words[0].lower() not in self.all_abbreviations:
182
+ sentence = sentence[0].upper() + sentence[1:]
183
+
184
+ if sentence:
185
+ restored.append(sentence)
186
+
187
+ return restored
188
+
189
+ def tokenize(self, text: str) -> List[str]:
190
+ """
191
+ Split text into sentences while handling complex cases.
192
+
193
+ Args:
194
+ text (str): Input text to split into sentences.
195
+
196
+ Returns:
197
+ List[str]: List of properly formatted sentences.
198
+ """
199
+ if not text or not text.strip():
200
+ return []
201
+
202
+ # Step 1: Protect special cases
203
+ protected_text, placeholders = self._protect_special_cases(text)
204
+
205
+ # Step 2: Normalize whitespace
206
+ protected_text = self._normalize_whitespace(protected_text)
207
+
208
+ # Step 3: Handle abbreviations
209
+ protected_text = self._handle_abbreviations(protected_text)
210
+
211
+ # Step 4: Split into potential sentences
212
+ potential_sentences = self.SENTENCE_END.split(protected_text)
213
+
214
+ # Step 5: Process and restore formatting
215
+ sentences = self._restore_formatting(potential_sentences)
216
+
217
+ # Step 6: Restore special cases
218
+ sentences = [self._restore_special_cases(s, placeholders) for s in sentences]
219
+
220
+ # Step 7: Post-process sentences
221
+ final_sentences = []
222
+ current_sentence = []
223
+
224
+ for sentence in sentences:
225
+ # Skip empty sentences
226
+ if not sentence.strip():
227
+ continue
228
+
229
+ # Check if sentence might be continuation of previous
230
+ if current_sentence and sentence[0].islower():
231
+ current_sentence.append(sentence)
232
+ else:
233
+ if current_sentence:
234
+ final_sentences.append(' '.join(current_sentence))
235
+ current_sentence = [sentence]
236
+
237
+ # Add last sentence if exists
238
+ if current_sentence:
239
+ final_sentences.append(' '.join(current_sentence))
240
+
241
+ return final_sentences
242
+
243
+
244
+ def split_sentences(text: str) -> List[str]:
245
+ """
246
+ Convenience function to split text into sentences using SentenceTokenizer.
247
+
248
+ Args:
249
+ text (str): Input text to split into sentences.
250
+
251
+ Returns:
252
+ List[str]: List of properly formatted sentences.
253
+ """
254
+ tokenizer = SentenceTokenizer()
255
+ return tokenizer.tokenize(text)
256
+
257
+
258
+ if __name__ == "__main__":
259
+ # Test text with various challenging cases
260
+ test_text: str = """
261
+ Dr. Smith (Ph.D., M.D.) visited Washington D.C. on Jan. 20, 2024! He met with Prof. Johnson at 3:30 p.m.
262
+ They discussed A.I. and machine learning... "What about the U.S. market?" asked Dr. Smith.
263
+ The meeting ended at 5 p.m. Later, they went to Mr. Wilson's house (located at 123 Main St.) for dinner.
264
+
265
+ Visit our website at https://www.example.com or email us at test@example.com!
266
+ The temperature was 72.5°F (22.5°C). The company's Q3 2023 revenue was $12.5M USD.
267
+
268
+ 「これは日本語の文章です。」This is a mixed-language text! How cool is that?
269
+
270
+ Some technical specs: CPU: 3.5GHz, RAM: 16GB, Storage: 2TB SSD.
271
+ Common abbreviations: etc., i.e., e.g., vs., cf., approx. 100 units.
272
+ """
273
+
274
+ # Process and print each sentence
275
+ sentences: List[str] = split_sentences(test_text)
276
+ print("Detected sentences:")
277
+ print("-" * 80)
278
+ for i, sentence in enumerate(sentences, 1):
279
+ print(f"{i}. {sentence}")
280
+ print("-" * 80)