-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode_preprocessor.py
More file actions
314 lines (247 loc) · 9.24 KB
/
Copy pathcode_preprocessor.py
File metadata and controls
314 lines (247 loc) · 9.24 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
"""
Code Preprocessor Module
Handles code cleaning, standardization, and normalization.
"""
import re
from typing import List, Dict, Optional
class CodePreprocessor:
"""Preprocesses code for evaluation."""
def __init__(self):
"""Initialize Code Preprocessor."""
pass
def remove_comments(self, code: str, language: str = 'python') -> str:
"""
Remove comments from code.
Args:
code: Source code string
language: Programming language (python, java, javascript, cpp)
Returns:
Code without comments
"""
if language in ['python']:
# Remove single-line comments
code = re.sub(r'#.*$', '', code, flags=re.MULTILINE)
# Remove multi-line comments (docstrings)
code = re.sub(r'""".*?"""', '', code, flags=re.DOTALL)
code = re.sub(r"'''.*?'''", '', code, flags=re.DOTALL)
elif language in ['java', 'javascript', 'cpp', 'c', 'typescript']:
# Remove single-line comments
code = re.sub(r'//.*$', '', code, flags=re.MULTILINE)
# Remove multi-line comments
code = re.sub(r'/\*.*?\*/', '', code, flags=re.DOTALL)
return code
def remove_blank_lines(self, code: str) -> str:
"""
Remove blank lines and excessive whitespace.
Args:
code: Source code string
Returns:
Code without blank lines
"""
# Remove lines that are only whitespace
lines = [line for line in code.split('\n') if line.strip()]
return '\n'.join(lines)
def normalize_whitespace(self, code: str) -> str:
"""
Normalize whitespace in code.
Args:
code: Source code string
Returns:
Code with normalized whitespace
"""
# Replace multiple spaces with single space
code = re.sub(r' +', ' ', code)
# Replace multiple newlines with single newline
code = re.sub(r'\n+', '\n', code)
# Remove trailing whitespace
code = '\n'.join(line.rstrip() for line in code.split('\n'))
return code.strip()
def remove_imports(self, code: str, language: str = 'python') -> str:
"""
Remove import statements from code.
Args:
code: Source code string
language: Programming language
Returns:
Code without import statements
"""
if language == 'python':
# Remove import and from...import statements
code = re.sub(r'^import .*$', '', code, flags=re.MULTILINE)
code = re.sub(r'^from .* import .*$', '', code, flags=re.MULTILINE)
elif language in ['java']:
code = re.sub(r'^import .*$', '', code, flags=re.MULTILINE)
elif language in ['javascript', 'typescript']:
code = re.sub(r'^import .*$', '', code, flags=re.MULTILINE)
code = re.sub(r"^import .*from .*$", '', code, flags=re.MULTILINE)
return code
def extract_functions(self, code: str, language: str = 'python') -> List[str]:
"""
Extract function definitions from code.
Args:
code: Source code string
language: Programming language
Returns:
List of function code blocks
"""
functions = []
if language == 'python':
# Match function definitions with their bodies (basic implementation)
pattern = r'def\s+\w+\s*\([^)]*\):\s*\n(?:(?: |\t).*\n)*'
functions = re.findall(pattern, code)
elif language == 'java':
# Match method definitions
pattern = r'(?:public|private|protected)?\s*(?:static)?\s*\w+\s+\w+\s*\([^)]*\)\s*\{[^}]*\}'
functions = re.findall(pattern, code, re.DOTALL)
elif language in ['javascript', 'typescript']:
# Match function declarations
pattern = r'function\s+\w+\s*\([^)]*\)\s*\{[^}]*\}'
functions.extend(re.findall(pattern, code, re.DOTALL))
# Match arrow functions
pattern = r'const\s+\w+\s*=\s*\([^)]*\)\s*=>\s*\{[^}]*\}'
functions.extend(re.findall(pattern, code, re.DOTALL))
return functions
def standardize_code(
self,
code: str,
language: str = 'python',
remove_comments: bool = True,
remove_imports: bool = False,
normalize_ws: bool = True,
remove_blank: bool = True
) -> str:
"""
Standardize code by applying multiple preprocessing steps.
Args:
code: Source code string
language: Programming language
remove_comments: Whether to remove comments
remove_imports: Whether to remove import statements
normalize_ws: Whether to normalize whitespace
remove_blank: Whether to remove blank lines
Returns:
Standardized code
"""
processed_code = code
if remove_comments:
processed_code = self.remove_comments(processed_code, language)
if remove_imports:
processed_code = self.remove_imports(processed_code, language)
if remove_blank:
processed_code = self.remove_blank_lines(processed_code)
if normalize_ws:
processed_code = self.normalize_whitespace(processed_code)
return processed_code
def preprocess_for_codebert(self, code: str, language: str = 'python') -> str:
"""
Preprocess code specifically for CodeBERT input.
Args:
code: Source code string
language: Programming language
Returns:
Preprocessed code ready for CodeBERT
"""
# For CodeBERT, we typically keep the structure but normalize formatting
processed = self.standardize_code(
code,
language=language,
remove_comments=False, # Keep comments as they may contain semantic info
remove_imports=False, # Keep imports
normalize_ws=True,
remove_blank=True
)
# Truncate if too long (CodeBERT has token limits)
max_length = 10000 # characters
if len(processed) > max_length:
processed = processed[:max_length]
return processed
def preprocess_for_token_similarity(self, code: str, language: str = 'python') -> str:
"""
Preprocess code for token-based similarity comparison.
Args:
code: Source code string
language: Programming language
Returns:
Preprocessed code ready for tokenization
"""
# For token similarity, we remove noise but keep the core structure
processed = self.standardize_code(
code,
language=language,
remove_comments=True, # Remove comments (noise for token matching)
remove_imports=True, # Remove imports (usually similar)
normalize_ws=True,
remove_blank=True
)
return processed
def get_language_from_extension(self, extension: str) -> str:
"""
Get language name from file extension.
Args:
extension: File extension (e.g., '.py', '.java')
Returns:
Language name
"""
language_map = {
'.py': 'python',
'.java': 'java',
'.js': 'javascript',
'.jsx': 'javascript',
'.ts': 'typescript',
'.tsx': 'typescript',
'.cpp': 'cpp',
'.c': 'c',
'.go': 'go',
'.rs': 'rust',
'.rb': 'ruby',
'.php': 'php'
}
return language_map.get(extension.lower(), 'unknown')
def merge_code_files(self, code_files: List[Dict[str, str]]) -> str:
"""
Merge multiple code files into a single string.
Args:
code_files: List of dictionaries with 'path' and 'content' keys
Returns:
Merged code string
"""
merged = []
for file_info in code_files:
path = file_info.get('path', 'unknown')
content = file_info.get('content', '')
merged.append(f"# File: {path}")
merged.append(content)
merged.append("") # Add blank line between files
return '\n'.join(merged)
# Example usage
if __name__ == "__main__":
# Example code
sample_code = """
import os
import sys
# This is a comment
def calculate_sum(a, b):
'''This function calculates sum'''
# Add the numbers
return a + b
def main():
result = calculate_sum(5, 3)
print(result)
if __name__ == "__main__":
main()
"""
preprocessor = CodePreprocessor()
print("Original code:")
print(sample_code)
print("\n" + "="*50 + "\n")
print("Standardized code:")
standardized = preprocessor.standardize_code(sample_code, language='python')
print(standardized)
print("\n" + "="*50 + "\n")
print("Preprocessed for CodeBERT:")
codebert_ready = preprocessor.preprocess_for_codebert(sample_code)
print(codebert_ready)
print("\n" + "="*50 + "\n")
print("Preprocessed for Token Similarity:")
token_ready = preprocessor.preprocess_for_token_similarity(sample_code)
print(token_ready)