|
1 | | -"""Utility functions for the LanguageTool library.""" |
2 | | - |
3 | 1 | from typing import List, Tuple, Optional |
4 | 2 | from shutil import which |
5 | 3 |
|
@@ -73,15 +71,6 @@ class PathError(LanguageToolError): |
73 | 71 | pass |
74 | 72 |
|
75 | 73 |
|
76 | | -class RateLimitError(LanguageToolError): |
77 | | - """ |
78 | | - Exception raised for errors related to rate limiting in the LanguageTool server. |
79 | | - This exception is a subclass of `LanguageToolError` and is used to indicate |
80 | | - issues such as exceeding the allowed number of requests to the public API without a key. |
81 | | - """ |
82 | | - pass |
83 | | - |
84 | | - |
85 | 74 | def parse_url(url_str: str) -> str: |
86 | 75 | """ |
87 | 76 | Parse the given URL string and ensure it has a scheme. |
@@ -112,24 +101,26 @@ def correct(text: str, matches: List[Match]) -> str: |
112 | 101 | :rtype: str |
113 | 102 | """ |
114 | 103 | ltext = list(text) |
115 | | - matches = [match for match in matches if match.replacements] |
116 | | - if matches: |
117 | | - errors = [ltext[match.offset:match.offset + match.errorLength] |
118 | | - for match in matches] |
119 | | - correct_offset = 0 |
120 | | - for n, match in enumerate(matches): |
121 | | - frompos, topos = (correct_offset + match.offset, |
122 | | - correct_offset + match.offset + match.errorLength) |
123 | | - if ltext[frompos:topos] != errors[n]: |
124 | | - continue |
125 | | - repl = match.replacements[0] |
126 | | - ltext[frompos:topos] = list(repl) |
127 | | - correct_offset += len(repl) - len(errors[n]) |
128 | | - return ''.join(ltext) |
| 104 | + if len(matches): |
| 105 | + matches = [match for match in matches if match.replacements] |
| 106 | + if matches: |
| 107 | + errors = [ltext[match.offset:match.offset + match.errorLength] |
| 108 | + for match in matches] |
| 109 | + correct_offset = 0 |
| 110 | + for n, match in enumerate(matches): |
| 111 | + frompos, topos = (correct_offset + match.offset, |
| 112 | + correct_offset + match.offset + match.errorLength) |
| 113 | + if ltext[frompos:topos] != errors[n]: |
| 114 | + continue |
| 115 | + repl = match.replacements[0] |
| 116 | + ltext[frompos:topos] = list(repl) |
| 117 | + correct_offset += len(repl) - len(errors[n]) |
| 118 | + return ''.join(ltext) |
| 119 | + else: |
| 120 | + return str() |
129 | 121 | else: |
130 | 122 | return None |
131 | 123 |
|
132 | | - |
133 | 124 | def get_language_tool_download_path() -> str: |
134 | 125 | """ |
135 | 126 | Get the download path for LanguageTool. |
|
0 commit comments