88import os
99import subprocess
1010import urllib .parse
11- import urllib . request
11+ from enum import Enum
1212import psutil
1313
1414from .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+
102128def 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
136156def get_language_tool_download_path () -> str :
0 commit comments