Skip to content

Commit 13fff08

Browse files
committed
perf: optimize LanguageTool API calls using persistent session
- Implement requests.Session() to enable TCP connection reuse - Reduce latency for sequential text processing by avoiding repeated handshakes - Switch localhost to 127.0.0.1 to prevent IPv6 resolution delays on Windows - Improve real-time responsiveness during text correction
1 parent 3e6c2ff commit 13fff08

File tree

4 files changed

+54
-29
lines changed

4 files changed

+54
-29
lines changed

aura_engine.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import psutil
1212
import time, re
1313

14+
#from config.settings import LANGUAGETOOL_CHECK_URL
1415

1516
# Python path to ensure reliable imports on all platforms
1617
# This solves potential issues when running from a batch script on Windows
@@ -1015,8 +1016,8 @@ def system_memory_watchdog(logging):
10151016
# atexit.register(lambda: stop_languagetool_server(logger, languagetool_process))
10161017

10171018
# aura_engine.py:760
1018-
active_lt_url = f"http://localhost:{settings.LANGUAGETOOL_PORT}/v2/check"
1019-
1019+
# active_lt_url = f"http://localhost:{settings.LANGUAGETOOL_PORT}/v2/check"
1020+
active_lt_url = f"{settings.LANGUAGETOOL_CHECK_URL}"
10201021

10211022
if not languagetool_process:
10221023
notify("Vosk Startup Error", "LanguageTool Server failed to start.", "critical")

config/maps/plugins/git/de-DE/FUZZY_MAP_pre.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
# EXAMPLE: git
1616
gitGit = r'(git|Geht|Sie geht|git|get|gitter|glitch|Gliedstaat|kids|kate|geht[^\s]*|geh|gitter|Gitta|kate|käthe|kitte|fiat|mit|kit|peach|quitt)'
1717

18+
# ein kit mit text in english
19+
20+
1821
# EXAMPLE: Commit
1922
commitGit = r'(Commit|Komet|Komik|Comics|Gummi|gummis|kommt|kommend|mit|hitch|komm|Kometen|kubicki|komisch|gewinnen|gromit|komme|kubis|cobit|cubic|beach|gemütlich|quitt|google)'
2023

@@ -47,6 +50,11 @@
4750
# EXAMPLE: git commit
4851
('git commit ', rf'\bgittern komet\b\s*', 80, {'flags': re.IGNORECASE}),
4952

53+
# EXAMPLE: git commit Text in english
54+
('git commit Text in english', rf'\bein {gitGit}\b\s*\b{commitGit} text in english\b', 80, {'flags': re.IGNORECASE}),
55+
56+
57+
5058

5159
# EXAMPLE: git clone
5260
('git clone ', rf'^\s*{gitGit}\s+(klar|klon|clone)\s*$', 80, {'flags': re.IGNORECASE}),

config/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
r"0 a\.d\.": ("SL5net >> Aura", 1), # 14400 = 4 h autocivp/moddata/autocivP_IconNames_README.txt
6666
r"Matrix|Discord": ("🗣SL5net ⟫ Aura", 3600), # 1 Stunde für Chat
6767
r"Outlook|Mail": ("-- Sent via Aura --", 86400), # 1 Tag für E-Mails
68-
r"Konsole|kate": ("", 86400),
68+
r"Konsole|kate|Google AI Studio|google ai studio": ("", 86400),
6969
r".*": ("🗣[ SL5net Aura ]", 1800) # 30 Min Fallback
7070
}
7171

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,66 @@
1-
# file script/py/func/correct_text_by_languagetool
1+
# scripts/py/func/correct_text_by_languagetool.py:1
22
import 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

614
def 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

Comments
 (0)