@@ -212,25 +212,42 @@ def verify_fact(
212212 # Sentence Segmentation
213213 # =========================================================================
214214
215- def _segment_sentences (self , text : str ) -> List [Tuple [str , int , int ]]:
216- """
217- Segment text into sentences with their positions.
218-
219- Returns:
220- List of (sentence, start_index, end_index)
221- """
222- # Simple sentence boundary detection
223- # Handles: period, question mark, exclamation, followed by space and capital
224- sentence_pattern = r'(?<=[.!?])\s+(?=[A-Z])'
215+ if len (text ) > 100000 :
216+ text = text [:100000 ] # Hard cap to prevent ReDoS on massive inputs
217+
218+ # Optimized sentence boundary: avoid lookbehind/ahead complexity if possible
219+ # Standard ReDoS-safe pattern for English sentences
220+ # Limit the whitespace match to avoid catastrophic backtracking
221+ sentence_pattern = r'[.!?]\s{1,50}(?=[A-Z])'
225222
226223 sentences = []
227224 current_pos = 0
228- parts = re .split (sentence_pattern , text )
225+
226+ # Using split with a simpler pattern is safer
227+ # Find all delimiters first
228+ matches = list (re .finditer (sentence_pattern , text ))
229+
230+ last_end = 0
231+ for match in matches :
232+ end = match .end () - 1 # Keep the delimiter with the sentence usually, or adjust
233+ # Actually, re.split behavior is tricky to replicate exactly safely
234+ # Let's use the pattern but with a limit
235+ pass
236+
237+ # Reverting to re.split but with SAFE regex
238+ # The issue was \s+ which is unbounded. \s{1,50} limits backtracking.
239+ safe_pattern = r'(?<=[.!?])\s{1,50}(?=[A-Z])'
240+
241+ parts = re .split (safe_pattern , text )
229242
230243 for part in parts :
231244 part = part .strip ()
232245 if part :
246+ # Find the part in text efficiently
233247 start = text .find (part , current_pos )
248+ if start == - 1 :
249+ # Fallback if strip() messed up strict finding
250+ start = current_pos
234251 end = start + len (part )
235252 sentences .append ((part , start , end ))
236253 current_pos = end
0 commit comments