File size: 1,068 Bytes
734b5b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Dict, List, Any


class FormatCorrectionAgent:
    """Corrects text format - spacing, alignment, fonts."""

    def process(self, extracted_text: str, layout_info: Dict = None) -> Dict[str, Any]:
        """Correct text formatting."""
        corrected = self._fix_spacing(extracted_text)
        corrected = self._fix_alignment(corrected)
        return {
            "corrected_text": corrected,
            "fixes_applied": ["spacing", "alignment"],
            "paragraphs_fixed": self._count_paragraphs(corrected),
        }

    def _fix_spacing(self, text: str) -> str:
        """Fix spacing issues."""
        import re

        text = re.sub(r" +", " ", text)
        text = re.sub(r"\n+", "\n", text)
        return text

    def _fix_alignment(self, text: str) -> str:
        """Fix alignment."""
        lines = text.split("\n")
        return "\n".join(line.strip() for line in lines)

    def _count_paragraphs(self, text: str) -> int:
        """Count paragraphs."""
        return len([p for p in text.split("\n\n") if p.strip()])