1- # file script /py/func/correct_text_by_languagetool
1+ # scripts /py/func/correct_text_by_languagetool.py:1
22import requests
3+ from requests .adapters import HTTPAdapter
4+ from urllib3 .util .retry import Retry
5+
6+ # scripts/py/func/correct_text_by_languagetool.py:6
7+ lt_session = requests .Session ()
8+
9+
10+ retries = Retry (total = 2 , backoff_factor = 0.1 )
11+ lt_session .mount ('http://' , HTTPAdapter (max_retries = retries ))
312
4- # from config.settings import LANGUAGETOOL_BASE_URL
513
614def correct_text_by_languagetool (logger , active_lt_url , LT_LANGUAGE , text : str ) -> str :
7- # scripts/py/func/correct_text_by_languagetool.py:7
8- # LANGUAGETOOL_URL = f"{LANGUAGETOOL_BASE_URL}/v2/check"
9- # LANGUAGETOOL_URL active_lt_url
15+ if not text or not text .strip ():
16+ return text
1017
1118 log_all_changes = True
12- # log_all_changes = False
1319
14- if not text .strip (): return text
20+ # 1. Daten-Payload optimieren
21+ # Tipp: Deaktivieren Sie "picky" Regeln oder schränken Sie Kategorien ein
22+ data = {
23+ 'language' : LT_LANGUAGE ,
24+ 'text' : text ,
25+ 'maxSuggestions' : 1 ,
26+ # 'enabledCategories': 'PUNCTUATION,GRAMMAR', # Nur das Nötigste
27+ # 'disabledRules': 'WHITESPACE_RULE', # Beispiel für langsame Regeln
28+ 'level' : 'default' # 'picky' wäre deutlich langsamer
29+ }
1530
16- if log_all_changes :
17- logger .info (f"-----> rawInput to LT: '{ text } '" )
18- # data = {'language': LT_LANGUAGE, 'text': text, 'maxSuggestions': 1, 'enabledCategories': 'PUNCTUATION,GRAMMAR',
19- # 'Categories': 'PUNCTUATION,GRAMMAR' }
20- data = {'language' : LT_LANGUAGE , 'text' : text , 'maxSuggestions' : 1 }
2131 try :
22- # scripts/py/func/correct_text_by_languagetool.py:19
23- response = requests .post (active_lt_url , data , timeout = 20 ) # timeout was 10 but Windows OS seems need much more at the moment 18.1.'26 21:28 Sun
32+ # 2. Timeout senken (z.B. 5 Sekunden)
33+ # Wenn der lokale Server länger braucht, ist er überlastet
34+ response = lt_session .post (active_lt_url , data = data , timeout = 5 )
2435 response .raise_for_status ()
36+
2537 matches = response .json ().get ('matches' , [])
2638 if not matches :
27- if log_all_changes :
28- logger .info (" <- Output from LT: (No changes)" )
2939 return text
40+
41+ # Korrektur-Logik (unverändert, aber effizienter)
3042 sorted_matches = sorted (matches , key = lambda m : m ['offset' ])
3143 new_text_parts , last_index = [], 0
44+
3245 for match in sorted_matches :
33- new_text_parts .append (text [last_index :match ['offset' ]])
34- if match ['replacements' ]:
35- new_text_parts .append (match ['replacements' ][0 ]['value' ])
36- else :
37- # FIX: Keep original text if there is no replacement
38- original_slice = text [match ['offset' ] : match ['offset' ] + match ['length' ]]
39- new_text_parts .append (original_slice )
46+ # Überspringe Korrektur, wenn keine Replacements vorhanden sind
47+ if not match .get ('replacements' ):
48+ continue
4049
50+ new_text_parts .append (text [last_index :match ['offset' ]])
51+ new_text_parts .append (match ['replacements' ][0 ]['value' ])
4152 last_index = match ['offset' ] + match ['length' ]
53+
4254 new_text_parts .append (text [last_index :])
4355 corrected_text = "" .join (new_text_parts )
56+
4457 if log_all_changes :
45- logger .info (f"🔁 📚 { text } 📚 -> LT-> 📚 { corrected_text } 📚 " )
58+ logger .info (f"🔁 LT-Korrektur durchgeführt. " )
4659 return corrected_text
60+
61+ except requests .exceptions .Timeout :
62+ logger .error (f" <- TIMEOUT: LT Server war zu langsam." )
63+ return text
4764 except requests .exceptions .RequestException as e :
4865 logger .error (f" <- ERROR: LanguageTool request failed: { e } " )
49- return text
50-
66+ return text
0 commit comments