-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
459 lines (392 loc) · 16.6 KB
/
app.py
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
from flask import Flask, render_template, request, jsonify, flash
from werkzeug.utils import secure_filename
import os
import subprocess
import time
import random
import requests
from bs4 import BeautifulSoup
from typing import Dict, List, Optional
from dataclasses import dataclass
import logging
from typing import List
import math
@dataclass
class PatternMetrics:
length: int
occurrences: int
avg_time: float
min_time: float
max_time: float
std_dev: float
@dataclass
class AlgorithmPerformance:
name: str
display_name: str
pattern_metrics: Dict[int, PatternMetrics]
overall_avg_time: float
total_patterns_tested: int
total_matches: int
# Algorithm name mappings
ALGORITHM_EXECUTABLES = {
'Naive': 'naive_search',
'Rabin-Karp': 'rabin_karp_search',
'KMP': 'kmp_search',
'Boyer-Moore': 'boyer_moore_search',
'DFA': 'dfa_search',
'Z': 'z_search',
'Aho-Corasick': 'aho_corasick_search'
}
app = Flask(__name__)
app.secret_key = os.environ.get('FLASK_SECRET_KEY', 'key8888') # Better to use environment variable
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['BUILD_FOLDER'] = 'bin'
app.config['MAX_CONTENT_LENGTH'] = 50 * 1024 * 1024 # 50MB limit
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Create necessary directories
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
os.makedirs(app.config['BUILD_FOLDER'], exist_ok=True)
def fetch_url_content(url: str) -> Optional[str]:
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'lxml')
for element in soup(['script', 'style', 'header', 'footer', 'nav']):
element.decompose()
text = soup.get_text()
return ' '.join(text.split())
except requests.RequestException as e:
logger.error(f"Error fetching URL {url}: {str(e)}")
flash(f"Error fetching URL: {str(e)}")
return None
def save_text_to_temp_file(text: str) -> str:
temp_filename = os.path.join(app.config['UPLOAD_FOLDER'], f'temp_text_{int(time.time())}.txt')
try:
with open(temp_filename, 'w', encoding='utf-8') as temp_file:
temp_file.write(text)
return temp_filename
except IOError as e:
logger.error(f"Error saving temporary file: {str(e)}")
raise
def analyze_search_method(text: str, patterns: List[str]) -> str:
if not text or not patterns:
return "Naive"
# Calculate text statistics
text_length = len(text)
text_unique_chars = len(set(text))
# Calculate pattern statistics
total_pattern_length = sum(len(p) for p in patterns)
avg_pattern_length = total_pattern_length / len(patterns)
max_pattern_length = max(len(p) for p in patterns)
min_pattern_length = min(len(p) for p in patterns)
all_pattern_chars = set(''.join(patterns))
pattern_alphabet_size = len(all_pattern_chars)
# Multiple patterns case
if len(patterns) > 1:
return "Aho-Corasick"
# Single pattern analysis
pattern = patterns[0]
pattern_length = len(pattern)
# Calculate character frequencies and entropy
char_freq = {}
for c in pattern:
char_freq[c] = char_freq.get(c, 0) + 1
entropy = 0
for count in char_freq.values():
prob = count / pattern_length
entropy -= prob * math.log2(prob)
# Calculate pattern characteristics
first_char_freq = text.count(pattern[0]) / text_length if text_length > 0 else 0
pattern_unique_chars = len(set(pattern))
pattern_occurrences = text.count(pattern)
# Check for pattern periodicity
def is_periodic(p: str) -> bool:
if len(p) <= 1:
return False
for i in range(1, len(p) // 2 + 1):
if p[:i] * (len(p) // i) == p[:len(p) // i * i]:
return True
return False
# Very short patterns or text - use naive approach
if pattern_length <= 5 or text_length <= 100:
return "Naive"
# DFA is excellent for:
# - Short to medium patterns that appear frequently
# - Small alphabet size
# - When the text will be searched multiple times
if (pattern_length <= 30 and
pattern_alphabet_size <= 26 and
pattern_occurrences > text_length / 1000):
return "DFA"
# KMP is good for:
# - Periodic patterns
# - Patterns with repeating prefixes
# - When the pattern appears frequently
if (is_periodic(pattern) or
pattern_occurrences > text_length / 100 or
(pattern_length > 10 and pattern_unique_chars < pattern_length * 0.6)):
return "KMP"
# Boyer-Moore is excellent for:
# - Long patterns
# - Large alphabet size
# - When the pattern is rare in the text
if ((pattern_length > 15 and first_char_freq < 0.1) or
(pattern_length > 20 and pattern_alphabet_size > 20) or
(pattern_length > text_length * 0.01 and entropy > 3.0)):
return "Boyer-Moore"
# Rabin-Karp is good for:
# - Short patterns with low entropy
# - When looking for multiple patterns of the same length
# - Patterns with many repeated characters
if (entropy < 2.5 and pattern_length < 20) or pattern_unique_chars < pattern_length * 0.4:
return "Rabin-Karp"
# Z algorithm is good for:
# - Medium length patterns
# - When preprocessing the pattern is beneficial
# - General-purpose cases where other algorithms don't have clear advantages
if pattern_length > 10 and pattern_length <= 100:
return "Z"
# Default to Boyer-Moore for very long patterns
if pattern_length > 110:
return "Boyer-Moore"
# For everything else, use KMP as a reliable default and fall-back option
return "KMP"
def generate_smart_patterns(text: str) -> List[str]:
num_patterns = 5
patterns = set()
# Common words strategy
words = [word for word in text.split() if len(word) >= 4]
word_freq = {}
for word in words:
word_freq[word] = word_freq.get(word, 0) + 1
common_words = sorted(word_freq.items(), key=lambda x: x[1], reverse=True)[:2]
patterns.update(word for word, _ in common_words)
# Random substrings strategy
lengths = [5, 10, 15]
for length in lengths:
if len(text) >= length:
start = random.randint(0, len(text) - length)
patterns.add(text[start:start + length])
return list(patterns)[:num_patterns]
def run_search_algorithm(executable_path: str, input_file: str, pattern: str) -> tuple[float, List[int]]:
try:
start_time = time.time()
proc = subprocess.run(
[executable_path, input_file, pattern],
capture_output=True,
text=True,
check=True,
timeout=30 # Add timeout
)
execution_time = time.time() - start_time
output = proc.stdout.strip()
positions = []
if output:
try:
positions = list(map(int, output.split(":")[1].split()))
except (IndexError, ValueError) as e:
logger.error(f"Error parsing algorithm output: {str(e)}")
return execution_time, positions
except subprocess.TimeoutExpired:
logger.error(f"Algorithm execution timed out for pattern: {pattern}")
return 0.0, []
except subprocess.CalledProcessError as e:
logger.error(f"Algorithm execution failed: {str(e)}")
return 0.0, []
def run_search_algorithm_aho_corasick(executable_path: str, input_file: str, patterns: List[str]) -> tuple[float, Dict[str, List[int]]]:
try:
start_time = time.time()
proc = subprocess.run(
[executable_path, input_file] + patterns,
capture_output=True,
text=True,
check=True,
timeout=30 # Add timeout
)
execution_time = time.time() - start_time
output = proc.stdout.strip()
positions_dict = {}
# Parse output for each pattern
if output:
try:
for line in output.split('\n'):
parts = line.split(":")
if len(parts) == 2:
pattern = parts[0].strip()
positions = list(map(int, parts[1].strip().split()))
positions_dict[pattern] = positions
except (IndexError, ValueError) as e:
logger.error(f"Error parsing Aho-Corasick algorithm output: {str(e)}")
return execution_time, positions_dict
except subprocess.TimeoutExpired:
logger.error(f"Aho-Corasick algorithm execution timed out for patterns: {', '.join(patterns)}")
return 0.0, {}
except subprocess.CalledProcessError as e:
logger.error(f"Aho-Corasick algorithm execution failed: {str(e)}")
return 0.0, {}
@app.route('/')
def index():
return render_template('index.html')
def run_search_algorithm_aho_corasick(executable_path: str, input_file: str, patterns: List[str]) -> tuple[float, Dict[str, List[int]]]:
try:
start_time = time.time()
proc = subprocess.run(
[executable_path, input_file] + patterns,
capture_output=True,
text=True,
check=True,
timeout=30 # Add timeout
)
execution_time = time.time() - start_time
output = proc.stdout.strip()
positions_dict = {}
# Parse output for each pattern
if output:
try:
for line in output.split('\n'):
parts = line.split(":")
if len(parts) == 2:
pattern = parts[0].strip()
positions = list(map(int, parts[1].strip().split()))
positions_dict[pattern] = positions
except (IndexError, ValueError) as e:
logger.error(f"Error parsing Aho-Corasick algorithm output: {str(e)}")
return execution_time, positions_dict
except subprocess.TimeoutExpired:
logger.error(f"Aho-Corasick algorithm execution timed out for patterns: {', '.join(patterns)}")
return 0.0, {}
except subprocess.CalledProcessError as e:
logger.error(f"Aho-Corasick algorithm execution failed: {str(e)}")
return 0.0, {}
# Adjustments in the /search endpoint
@app.route('/search', methods=['POST'])
def search():
try:
# Input handling
if 'url' in request.form and request.form['url']:
text_content = fetch_url_content(request.form['url'])
if text_content is None:
return jsonify({'error': 'Failed to fetch URL content'}), 400
filepath = save_text_to_temp_file(text_content)
elif 'textfile' in request.files and request.files['textfile'].filename:
file = request.files['textfile']
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
with open(filepath, 'r', encoding='utf-8') as f:
text_content = f.read()
else:
return jsonify({'error': 'No input provided'}), 400
patterns = [p.strip() for p in request.form.get('patterns', '').split(',') if p.strip()]
if not patterns:
return jsonify({'error': 'No patterns provided'}), 400
algorithm = request.form.get('algorithm', 'none')
if algorithm not in ALGORITHM_EXECUTABLES:
return jsonify({'error': 'Invalid algorithm specified'}), 400
results = {}
calc_time = 0
if algorithm != 'none':
executable = os.path.join(app.config['BUILD_FOLDER'], ALGORITHM_EXECUTABLES[algorithm])
if algorithm == 'Aho-Corasick':
execution_time, results = run_search_algorithm_aho_corasick(executable, filepath, patterns)
calc_time = execution_time
else:
for pattern in patterns:
execution_time, positions = run_search_algorithm(executable, filepath, pattern)
calc_time += execution_time
results[pattern] = positions
return jsonify({
'results': results,
'times': round(calc_time, 7),
'text_content': text_content[:1000],
'algorithm_used': algorithm
})
except Exception as e:
logger.error(f"Error in search endpoint: {str(e)}")
return jsonify({'error': 'Internal server error'}), 500
@app.route('/analyze', methods=['POST'])
def analyze():
try:
# Input handling
if 'url' in request.form and request.form['url']:
text_content = fetch_url_content(request.form['url'])
if text_content is None:
return jsonify({'error': 'Failed to fetch URL content'}), 400
filepath = save_text_to_temp_file(text_content)
elif 'textfile' in request.files and request.files['textfile'].filename:
file = request.files['textfile']
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
with open(filepath, 'r', encoding='utf-8') as f:
text_content = f.read()
else:
return jsonify({'error': 'No input provided'}), 400
patterns = [p.strip() for p in request.form.get('patterns', '').split(',') if p.strip()]
if not patterns:
return jsonify({'error': 'No patterns provided'}), 400
best_algorithm = analyze_search_method(text_content, patterns)
results = {}
performance_data = {}
for algo in ALGORITHM_EXECUTABLES:
executable = os.path.join(app.config['BUILD_FOLDER'], ALGORITHM_EXECUTABLES[algo])
algo_time = 0
if algo == "Aho-Corasick":
execution_time, positions = run_search_algorithm_aho_corasick(executable, filepath, patterns)
algo_time = execution_time
if algo == best_algorithm:
results.update(positions)
else:
for pattern in patterns:
execution_time, positions = run_search_algorithm(executable, filepath, pattern)
algo_time += execution_time
if algo == best_algorithm:
results[pattern] = positions
performance_data[algo] = algo_time
return jsonify({
'results': results,
'times': round(performance_data[best_algorithm], 7),
'text_content': text_content[:1000],
'algorithm_used': best_algorithm,
'performance': performance_data
})
except Exception as e:
logger.error(f"Error in analyze endpoint: {str(e)}")
return jsonify({'error': 'Internal server error'}), 500
@app.route('/random_analysis', methods=['POST'])
def random_analysis():
try:
if 'url' in request.form and request.form['url']:
text_content = fetch_url_content(request.form['url'])
if text_content is None:
return jsonify({'error': 'Failed to fetch URL content'}), 400
elif 'textfile' in request.files and request.files['textfile'].filename:
file = request.files['textfile']
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
with open(filepath, 'r', encoding='utf-8') as f:
text_content = f.read()
else:
return jsonify({'error': 'No input provided'}), 400
patterns = generate_smart_patterns(text_content)
filepath = save_text_to_temp_file(text_content)
performance_data = {}
for algo in ALGORITHM_EXECUTABLES:
executable = os.path.join(app.config['BUILD_FOLDER'], ALGORITHM_EXECUTABLES[algo])
algo_time = 0
for pattern in patterns:
execution_time, _ = run_search_algorithm(executable, filepath, pattern)
algo_time += execution_time
performance_data[algo] = algo_time
return jsonify({
'performance': performance_data,
'patterns_tested': patterns
})
except Exception as e:
logger.error(f"Error in random_analysis endpoint: {str(e)}")
return jsonify({'error': 'Internal server error'}), 500
if __name__ == "__main__":
app.run(debug=False) # Set debug=False for production