-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservices.py
More file actions
29 lines (23 loc) · 909 Bytes
/
services.py
File metadata and controls
29 lines (23 loc) · 909 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import re
def is_stammering(text: str) -> bool:
"""
Detects non-natural repetitions in translated text.
Meets Advanced Functional Specification[cite: 34, 35].
"""
if not text:
return False
# 1. Detect Character Elongation (e.g., "soooooooooo...")
# Matches any character repeated 10 or more times [cite: 35]
if re.search(r'(.)\1{9,}', text):
return True
# 2. Detect Word/Phrase Repetition (e.g., "station station")
# Standard NLP approach for repetition [cite: 35]
words = re.findall(r'\w+', text.lower())
# Check sequences of 1 to 4 words repeating consecutively
for n in range(1, 5):
for i in range(len(words) - 2 * n + 1):
segment1 = words[i : i + n]
segment2 = words[i + n : i + 2 * n]
if segment1 == segment2:
return True
return False