-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_processor.py
More file actions
64 lines (52 loc) · 2.51 KB
/
text_processor.py
File metadata and controls
64 lines (52 loc) · 2.51 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import difflib
class TextProcessor:
def __init__(self):
self.last_text = ""
self.similarity_threshold = 0.8 # If >80% similar, treat as same
def process_text(self, new_text: str) -> str:
"""
Takes raw OCR text and returns only the 'new' part to be appended.
Uses overlap detection to handle scrolling.
"""
if not new_text or not new_text.strip():
return ""
clean_new = new_text.strip()
clean_last = self.last_text.strip()
# If first run
if not clean_last:
self.last_text = clean_new
return clean_new
# 1. Exact Match Check (No change)
if clean_new == clean_last:
return ""
# 2. Similarity Check (Jitter/Flicker handling)
# If the text is 90% similar, we assume it's the "same" frame with minor OCR noise,
# unless it is clearly a scroll. But basic jitter shouldn't trigger new text.
matcher = difflib.SequenceMatcher(None, clean_last, clean_new)
if matcher.ratio() > 0.9:
# Too similar, probably just noise
return ""
# 3. Overlap Detection (Scrolling)
# We look for the longest match that connects the END of clean_last to the START of clean_new.
match = matcher.find_longest_match(0, len(clean_last), 0, len(clean_new))
# Condition:
# The match must touch the END of the old text (match.a + match.size == len)
# AND the match must touch the START of the new text (match.b == 0)
# AND the match must be of a reasonable size to be confident (e.g., > 10 chars or > 10%)
if (match.a + match.size == len(clean_last)) and (match.b == 0) and (match.size > 15):
# Found the seam!
# new_content is everything AFTER the match in clean_new
new_content = clean_new[match.size:].strip()
self.last_text = clean_new
return new_content
# 4. Fallback: Completely new text?
# If we didn't find a clean scroll seam, it might be a page jump or a very fast scroll.
# We'll return the whole thing but be careful.
# Ideally, we should check if new_text is just a SUBSET of old_text (user scrolled UP?)
if clean_new in clean_last:
# User scrolled up or text disappeared? Don't append.
return ""
self.last_text = clean_new
return clean_new
def reset(self):
self.last_text = ""