AI Engineer commited on
Commit
bf470df
·
1 Parent(s): 9515ba4

Add wordninja anomaly heuristic to fix mashed text

Browse files
Files changed (2) hide show
  1. pipeline/chunker.py +18 -4
  2. requirements.txt +1 -0
pipeline/chunker.py CHANGED
@@ -40,6 +40,20 @@ class ChunkMetadata:
40
 
41
  # ── Parsers ─────────────────────────────────────────────────────────
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  def parse_pdf(file_bytes: bytes) -> List[PageContent]:
44
  """Extract text from a PDF preserving page numbers and detecting section titles."""
45
  import fitz # PyMuPDF
@@ -57,7 +71,7 @@ def parse_pdf(file_bytes: bytes) -> List[PageContent]:
57
 
58
  pages.append(PageContent(
59
  page_num=page_idx,
60
- text=text.strip(),
61
  section_title=section_title,
62
  ))
63
 
@@ -110,7 +124,7 @@ def parse_docx(file_bytes: bytes) -> List[PageContent]:
110
  if current_text_lines:
111
  pages.append(PageContent(
112
  page_num=page_num,
113
- text="\n".join(current_text_lines).strip(),
114
  section_title=current_section,
115
  ))
116
  page_num += 1
@@ -124,7 +138,7 @@ def parse_docx(file_bytes: bytes) -> List[PageContent]:
124
  if current_text_lines:
125
  pages.append(PageContent(
126
  page_num=page_num,
127
- text="\n".join(current_text_lines).strip(),
128
  section_title=current_section,
129
  ))
130
 
@@ -150,7 +164,7 @@ def parse_txt(file_bytes: bytes) -> List[PageContent]:
150
 
151
  pages.append(PageContent(
152
  page_num=idx,
153
- text=section_text,
154
  section_title=title,
155
  ))
156
 
 
40
 
41
  # ── Parsers ─────────────────────────────────────────────────────────
42
 
43
+ def clean_mashed_text(text: str, threshold: int = 20) -> str:
44
+ """Detect and split anomalous, excessively long alphanumeric strings (missing spaces)."""
45
+ if not text:
46
+ return text
47
+
48
+ def replace_long_word(match):
49
+ word = match.group(0)
50
+ if len(word) >= threshold:
51
+ import wordninja
52
+ return " ".join(wordninja.split(word))
53
+ return word
54
+
55
+ return re.sub(r'[a-zA-Z0-9]{20,}', replace_long_word, text)
56
+
57
  def parse_pdf(file_bytes: bytes) -> List[PageContent]:
58
  """Extract text from a PDF preserving page numbers and detecting section titles."""
59
  import fitz # PyMuPDF
 
71
 
72
  pages.append(PageContent(
73
  page_num=page_idx,
74
+ text=clean_mashed_text(text.strip()),
75
  section_title=section_title,
76
  ))
77
 
 
124
  if current_text_lines:
125
  pages.append(PageContent(
126
  page_num=page_num,
127
+ text=clean_mashed_text("\n".join(current_text_lines).strip()),
128
  section_title=current_section,
129
  ))
130
  page_num += 1
 
138
  if current_text_lines:
139
  pages.append(PageContent(
140
  page_num=page_num,
141
+ text=clean_mashed_text("\n".join(current_text_lines).strip()),
142
  section_title=current_section,
143
  ))
144
 
 
164
 
165
  pages.append(PageContent(
166
  page_num=idx,
167
+ text=clean_mashed_text(section_text),
168
  section_title=title,
169
  ))
170
 
requirements.txt CHANGED
@@ -17,3 +17,4 @@ tiktoken==0.7.0
17
  numpy==1.26.4
18
  python-dotenv==1.0.1
19
  scipy==1.13.1
 
 
17
  numpy==1.26.4
18
  python-dotenv==1.0.1
19
  scipy==1.13.1
20
+ wordninja==2.0.0