-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_engine.py
More file actions
375 lines (299 loc) · 12.9 KB
/
Copy pathsearch_engine.py
File metadata and controls
375 lines (299 loc) · 12.9 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
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
# Search Engine Implementation - CS600 Project
# Based on textbook Section 23.6
import os
import re
import sys
import math
from bs4 import BeautifulSoup
from collections import defaultdict, Counter
from urllib.parse import urljoin
# Import NLTK for stopwords
try:
import nltk
from nltk.corpus import stopwords
try:
nltk.data.find('corpora/stopwords')
except LookupError:
nltk.download('stopwords', quiet=True)
STOPWORDS = set(stopwords.words('english'))
except ImportError:
# Fallback stopwords if NLTK not available
STOPWORDS = {
"a", "an", "the", "and", "or", "but", "is", "are", "was", "were",
"in", "on", "at", "to", "for", "with", "by", "about", "like",
"from", "of", "as", "this", "that", "these", "those", "it", "its",
"be", "been", "being", "have", "has", "had", "do", "does", "did",
"will", "would", "should", "could", "may", "might", "must", "can"
}
class SearchEngine:
"""
Search engine using inverted index and TF-IDF ranking.
Uses NLTK stopwords and extracts hyperlinks from pages.
"""
def __init__(self, webpages_dir="webpages"):
self.webpages_dir = webpages_dir
# Inverted index: maps terms to documents
self.inverted_index = defaultdict(set)
self.term_frequency = defaultdict(Counter)
self.document_frequency = Counter()
self.document_lengths = {}
# Hyperlinks for web crawler
self.hyperlinks = defaultdict(set)
self.stopwords = STOPWORDS
self.documents = []
self.document_urls = {}
self._load_url_mapping()
def _load_url_mapping(self):
# Load URL mappings from input.txt
input_file = os.path.join(self.webpages_dir, "input.txt")
try:
with open(input_file, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
# Skip empty lines and comments
if line and not line.startswith("//"):
parts = line.split(" ", 1) # Split at first space
if len(parts) == 2:
filename, url = parts
self.document_urls[filename] = url
print(f"✓ Loaded {len(self.document_urls)} URL mappings from input.txt")
except FileNotFoundError:
print(f"⚠ Warning: {input_file} not found. URLs will not be displayed.")
except Exception as e:
print(f"⚠ Error loading URL mapping: {e}")
def _extract_hyperlinks(self, soup, base_url=""):
# Extract all hyperlinks from HTML
links = set()
for a_tag in soup.find_all('a', href=True):
href = a_tag['href']
if base_url:
full_url = urljoin(base_url, href)
else:
full_url = href
links.add(full_url)
return links
def parse_document(self, filename):
# Parse HTML file and update indexes
filepath = os.path.join(self.webpages_dir, filename)
try:
with open(filepath, "r", encoding="utf-8") as file:
content = file.read()
soup = BeautifulSoup(content, "html.parser")
# Extract hyperlinks
base_url = self.document_urls.get(filename, "")
links = self._extract_hyperlinks(soup, base_url)
self.hyperlinks[filename] = links
# Extract and tokenize text
text = soup.get_text()
tokens = re.findall(r'\b\w+\b', text.lower())
# Filter stopwords and single chars
filtered_tokens = [
token for token in tokens
if token not in self.stopwords and len(token) > 1
]
term_counts = Counter(filtered_tokens)
self.document_lengths[filename] = len(filtered_tokens)
# Update indexes
for term, count in term_counts.items():
self.inverted_index[term].add(filename)
self.term_frequency[term][filename] = count
self.document_frequency[term] += 1
print(f"✓ Indexed: {filename} ({len(term_counts)} unique terms, {len(links)} links)")
return True
except FileNotFoundError:
print(f"✗ Error: File not found - {filepath}")
return False
except Exception as e:
print(f"✗ Error parsing {filename}: {e}")
return False
def build_index(self):
# Build inverted index from all HTML files
print(f"\n{'='*60}")
print(f"Building Inverted Index from '{self.webpages_dir}'...")
print(f"{'='*60}\n")
self.documents = []
if not os.path.exists(self.webpages_dir):
print(f"✗ Error: Directory '{self.webpages_dir}' not found!")
return
html_files = [
f for f in os.listdir(self.webpages_dir)
if f.endswith((".html", ".htm"))
]
if not html_files:
print(f"✗ Warning: No HTML files found in '{self.webpages_dir}'")
return
for filename in sorted(html_files):
if self.parse_document(filename):
self.documents.append(filename)
print(f"\n{'='*60}")
print(f"Indexing Complete!")
print(f"{'='*60}")
print(f"Documents indexed: {len(self.documents)}")
print(f"Unique terms: {len(self.inverted_index)}")
print(f"Total hyperlinks: {sum(len(links) for links in self.hyperlinks.values())}")
print(f"{'='*60}\n")
def calculate_tf_idf(self, term, document):
# Calculate TF-IDF score
# TF = term_count / total_terms, IDF = log(total_docs / docs_with_term)
term_count = self.term_frequency[term][document]
doc_length = self.document_lengths[document]
tf = term_count / doc_length if doc_length > 0 else 0
total_docs = len(self.documents)
docs_with_term = self.document_frequency[term]
idf = math.log(total_docs / docs_with_term) if docs_with_term > 0 else 0
return tf * idf
def search(self, query):
# Search for documents with ALL query terms (AND logic)
if not query.strip():
print("⚠ Empty query. Please enter some search terms.")
return []
query_terms = re.findall(r'\b\w+\b', query.lower())
filtered_terms = [
term for term in query_terms
if term not in self.stopwords and len(term) > 1
]
if not filtered_terms:
print("⚠ Query contains only stopwords. Please use more specific terms.")
return []
print(f"\nSearch terms (after filtering): {filtered_terms}")
# Find docs with ALL terms using set intersection
matching_docs = None
for term in filtered_terms:
if term in self.inverted_index:
if matching_docs is None:
matching_docs = self.inverted_index[term].copy()
else:
matching_docs &= self.inverted_index[term]
print(f" '{term}' → found in {len(self.inverted_index[term])} documents")
else:
print(f" '{term}' → NOT FOUND in any document")
return []
if not matching_docs:
print("\n✗ No documents found matching all query terms.")
return []
# Rank by TF-IDF scores
ranked_results = []
for doc in matching_docs:
score = sum(
self.calculate_tf_idf(term, doc)
for term in filtered_terms
if term in self.inverted_index
)
ranked_results.append((doc, score))
ranked_results.sort(key=lambda x: x[1], reverse=True)
return ranked_results
def display_results(self, results):
# Display search results
if not results:
print("\nNo results to display.")
return
print(f"\n{'='*60}")
print(f"✓ Found {len(results)} matching document(s)")
print(f"{'='*60}\n")
for i, (doc, score) in enumerate(results, 1):
# Get URL from mapping or use a default message
url = self.document_urls.get(doc, "URL not available")
print(f"{i}. {doc}")
print(f" Relevance Score (TF-IDF): {score:.6f}")
print(f" URL: {url}")
# Show linked pages if available
if doc in self.hyperlinks and self.hyperlinks[doc]:
num_links = len(self.hyperlinks[doc])
print(f" Links: {num_links} outgoing hyperlink(s)")
print() # Blank line between results
print(f"{'='*60}\n")
def display_statistics(self):
"""Display search engine statistics."""
print(f"\n{'='*60}")
print("Search Engine Statistics")
print(f"{'='*60}")
print(f"Total Documents: {len(self.documents)}")
print(f"Vocabulary Size: {len(self.inverted_index)} unique terms")
print(f"Total Hyperlinks: {sum(len(links) for links in self.hyperlinks.values())}")
# Find most common terms
term_doc_counts = [(term, len(docs)) for term, docs in self.inverted_index.items()]
term_doc_counts.sort(key=lambda x: x[1], reverse=True)
print(f"\nTop 10 Most Common Terms:")
for i, (term, count) in enumerate(term_doc_counts[:10], 1):
print(f" {i}. '{term}' appears in {count} document(s)")
print(f"{'='*60}\n")
def run_interactive(self):
# Interactive mode - run queries
self.build_index()
self.display_statistics()
print("🔍 Mini Search Engine - Interactive Mode")
print("Type 'exit' or 'quit' to quit")
print("Type 'stats' to see statistics\n")
while True:
try:
query = input("Enter search query: ").strip()
if query.lower() in ['exit', 'quit']:
print("\n👋 Exiting search engine. Goodbye!")
break
if query.lower() == 'stats':
self.display_statistics()
continue
# Perform search and display results
results = self.search(query)
self.display_results(results)
except KeyboardInterrupt:
print("\n\n👋 Exiting search engine. Goodbye!")
break
except Exception as e:
print(f"\n✗ Error: {e}\n")
def run_tests(engine):
# Run test queries
test_queries = [
"", # empty
"the and is are", # stopwords only
"xyzabc123notfound", # non-existent
"security",
"encryption",
"malware",
"cloud",
"cloud security",
"network attack",
"encryption cryptography",
"malware detection",
"incident response",
"security threats protection",
"firewall intrusion detection",
"cybersecurity best practices"
]
print("\n" + "="*60)
print("Running Automated Test Suite")
print("="*60 + "\n")
for i, query in enumerate(test_queries, 1):
print(f"\n{'─'*60}")
print(f"TEST CASE {i}: Query = '{query}'")
print(f"{'─'*60}")
results = engine.search(query)
engine.display_results(results)
def main():
# Main function - run in test or interactive mode
if len(sys.argv) > 1 and sys.argv[1] == '--test':
print("\n🧪 Running in TEST mode...")
print("Output will be saved to output.txt\n")
engine = SearchEngine()
engine.build_index()
output_file = "output.txt"
original_stdout = sys.stdout
with open(output_file, 'w', encoding='utf-8') as f:
sys.stdout = f
print("="*60)
print("SEARCH ENGINE - TEST OUTPUT")
print("="*60)
print(f"Total Documents Indexed: {len(engine.documents)}")
print(f"Vocabulary Size: {len(engine.inverted_index)} unique terms")
print("="*60 + "\n")
run_tests(engine)
print("\n" + "="*60)
print("END OF TEST OUTPUT")
print("="*60)
sys.stdout = original_stdout
print(f"✓ Test output saved to {output_file}")
else:
engine = SearchEngine()
engine.run_interactive()
if __name__ == "__main__":
main()