Skip to content

Commit 8a8edda

Browse files
authored
Merge pull request #1 from mdevolde/Matches-fix
Amazing, thank you very much!
2 parents dd31d9b + aad0aed commit 8a8edda

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

language_tool_python/utils.py

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import os
99
import subprocess
1010
import urllib.parse
11-
import urllib.request
11+
from enum import Enum
1212
import psutil
1313

1414
from .config_file import LanguageToolConfig
@@ -99,6 +99,32 @@ def parse_url(url_str: str) -> str:
9999
return urllib.parse.urlparse(url_str).geturl()
100100

101101

102+
class TextStatus(Enum):
103+
CORRECT = "correct"
104+
FAULTY = "faulty"
105+
GARBAGE = "garbage"
106+
107+
108+
def classify_matches(matches: List[Match]) -> TextStatus:
109+
"""
110+
Classify the matches (result of a check on a text) into one of three categories:
111+
CORRECT, FAULTY, or GARBAGE.
112+
This function checks the status of the matches and returns a corresponding
113+
`TextStatus` value.
114+
115+
:param matches: A list of Match objects to be classified.
116+
:type matches: List[Match]
117+
:return: The classification of the matches as a `TextStatus` value.
118+
:rtype: TextStatus
119+
"""
120+
if not len(matches):
121+
return TextStatus.CORRECT
122+
matches = [match for match in matches if match.replacements]
123+
if not len(matches):
124+
return TextStatus.GARBAGE
125+
return TextStatus.FAULTY
126+
127+
102128
def correct(text: str, matches: List[Match]) -> str:
103129
"""
104130
Corrects the given text based on the provided matches.
@@ -112,25 +138,19 @@ def correct(text: str, matches: List[Match]) -> str:
112138
:rtype: str
113139
"""
114140
ltext = list(text)
115-
if len(matches): # some suggestions available, we'll use first/best
116-
matches = [match for match in matches if match.replacements]
117-
if matches:
118-
errors = [ltext[match.offset:match.offset + match.errorLength]
119-
for match in matches]
120-
correct_offset = 0
121-
for n, match in enumerate(matches):
122-
frompos, topos = (correct_offset + match.offset,
123-
correct_offset + match.offset + match.errorLength)
124-
if ltext[frompos:topos] != errors[n]:
125-
continue
126-
repl = match.replacements[0]
127-
ltext[frompos:topos] = list(repl)
128-
correct_offset += len(repl) - len(errors[n])
129-
return ''.join(ltext)
130-
else: # no suggestions for given language, i.e. gibberish submit
131-
return str()
132-
else: # Correct string submit
133-
return None
141+
matches = [match for match in matches if match.replacements]
142+
errors = [ltext[match.offset:match.offset + match.errorLength]
143+
for match in matches]
144+
correct_offset = 0
145+
for n, match in enumerate(matches):
146+
frompos, topos = (correct_offset + match.offset,
147+
correct_offset + match.offset + match.errorLength)
148+
if ltext[frompos:topos] != errors[n]:
149+
continue
150+
repl = match.replacements[0]
151+
ltext[frompos:topos] = list(repl)
152+
correct_offset += len(repl) - len(errors[n])
153+
return ''.join(ltext)
134154

135155

136156
def get_language_tool_download_path() -> str:

0 commit comments

Comments
 (0)