File size: 5,912 Bytes
39028c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
"""
Text preprocessing and tokenization module for technical documents
"""

import re
import logging
from typing import List, Tuple
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import sent_tokenize, word_tokenize


# Download required NLTK resources
try:
    nltk.data.find('tokenizers/punkt')
except LookupError:
    nltk.download('punkt', quiet=True)

try:
    nltk.data.find('corpora/stopwords')
except LookupError:
    nltk.download('stopwords', quiet=True)

logger = logging.getLogger(__name__)


class TextPreprocessor:
    """Comprehensive text preprocessing for technical documents."""
    
    def __init__(self, remove_stopwords: bool = False):
        """
        Initialize preprocessor.
        
        Args:
            remove_stopwords: Whether to remove English stopwords
        """
        self.remove_stopwords = remove_stopwords
        self.stop_words = set(stopwords.words('english')) if remove_stopwords else set()
    
    def clean_text(self, text: str) -> str:
        """
        Clean technical document text.
        
        Args:
            text: Raw text to clean
            
        Returns:
            Cleaned text
        """
        # Remove extra whitespace
        text = re.sub(r'\s+', ' ', text)
        
        # Remove URLs
        text = re.sub(r'http\S+|www\S+', '', text)
        
        # Remove email addresses
        text = re.sub(r'\S+@\S+', '', text)
        
        # Remove special characters but keep punctuation for sentences
        text = re.sub(r'[^\w\s.!?,;:\-()]', '', text)
        
        # Remove extra spaces created by above operations
        text = re.sub(r'\s+', ' ', text).strip()
        
        return text
    
    def remove_citations(self, text: str) -> str:
        """
        Remove citation references from text.
        
        Args:
            text: Text with citations
            
        Returns:
            Text without citations
        """
        # Remove [Author et al., Year] style citations
        text = re.sub(r'\[\d+\]|\[[\w\s\.]+,\s*\d{4}\]', '', text)
        
        # Remove (Author Year) style citations
        text = re.sub(r'\([\w\s\.]+,?\s*\d{4}\)', '', text)
        
        return text
    
    def remove_equations(self, text: str) -> str:
        """
        Remove mathematical equations and formulas.
        
        Args:
            text: Text with equations
            
        Returns:
            Text without equations
        """
        # Remove LaTeX equations
        text = re.sub(r'\$\$.*?\$\$', '', text, flags=re.DOTALL)
        text = re.sub(r'\$.*?\$', '', text)
        
        return text
    
    def sent_tokenize(self, text: str) -> List[str]:
        """
        Tokenize text into sentences.
        
        Args:
            text: Input text
            
        Returns:
            List of sentences
        """
        sentences = sent_tokenize(text)
        return [sent.strip() for sent in sentences if sent.strip()]
    
    def word_tokenize(self, text: str) -> List[str]:
        """
        Tokenize text into words.
        
        Args:
            text: Input text
            
        Returns:
            List of words
        """
        tokens = word_tokenize(text.lower())
        
        if self.remove_stopwords:
            tokens = [t for t in tokens if t not in self.stop_words and t.isalnum()]
        
        return tokens
    
    def preprocess_document(self, text: str, remove_citations: bool = True,
                           remove_equations: bool = False) -> str:
        """
        Complete preprocessing pipeline.
        
        Args:
            text: Raw document text
            remove_citations: Whether to remove citations
            remove_equations: Whether to remove equations
            
        Returns:
            Preprocessed text
        """
        # Clean text
        text = self.clean_text(text)
        
        # Remove citations if requested
        if remove_citations:
            text = self.remove_citations(text)
        
        # Remove equations if requested
        if remove_equations:
            text = self.remove_equations(text)
        
        logger.info("Document preprocessing completed")
        return text


class TechnicalDocumentParser:
    """Parse technical document structure (sections, abstracts, etc.)."""
    
    @staticmethod
    def extract_abstract(text: str) -> Tuple[str, str]:
        """
        Extract abstract from document.
        
        Args:
            text: Full document text
            
        Returns:
            Tuple of (abstract, remaining_text)
        """
        abstract_match = re.search(
            r'(?:^|\n)(abstract|summary)(.*?)(?:\n(?:introduction|1\.|contents))',
            text, re.IGNORECASE | re.DOTALL
        )
        
        if abstract_match:
            abstract = abstract_match.group(2).strip()
            remaining = text[:abstract_match.start()] + text[abstract_match.end():]
            return abstract, remaining
        
        return "", text
    
    @staticmethod
    def extract_sections(text: str) -> List[Tuple[str, str]]:
        """
        Extract document sections.
        
        Args:
            text: Document text
            
        Returns:
            List of (section_title, section_content) tuples
        """
        # Match common section patterns
        section_pattern = r'(?:^|\n)((?:\d+\.\s+)?(?:introduction|methodology|results|discussion|conclusion|references|abstract).*?)(?:\n(?:\d+\.\s+)?(?:[A-Z][^.]*?)(?=\n|$))'
        
        sections = []
        matches = re.finditer(section_pattern, text, re.IGNORECASE)
        
        for match in matches:
            title = match.group(1).strip()
            content = match.group(2).strip() if match.lastindex >= 2 else ""
            sections.append((title, content))
        
        return sections