-
Notifications
You must be signed in to change notification settings - Fork 0
/
boolean_model.py
666 lines (545 loc) · 22.8 KB
/
boolean_model.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
# Autor: Igor Augusto Reis Gomes [12011BSI290]
# Disclaimer: The following code is not logically as optimized as the next model, but it works.
# Also, the tagger and classifier were not per say necessary nor demanded by my professor, but I decided to use them anyway.
import pickle
import sys
import time
import subprocess
import nltk
from nltk.corpus import stopwords
from nltk.stem import RSLPStemmer
from nltk.tag import UnigramTagger
from nltk.tokenize import word_tokenize
from typing import Any, Callable
from importlib import import_module
def install_package(package_name: str):
"""Function to install a package using pip. If the package is already installed, it will be skipped."""
try:
import_module(package_name)
print(f"\033[1;32;40m✅ {package_name} is installed!\033[0m")
except ImportError:
print(f"\033[1;31m⚠ {package_name} not found. Installing it...\033[0m")
try:
subprocess.check_call(
[sys.executable, "-m", "pip", "install", package_name]
)
import_module(package_name) # Try to import again after installation
print(f"\033[1;32;40m✅ {package_name} is now installed!\033[0m")
except Exception as e:
print(f"\033[1;31m⚠ Failed to install {package_name}: {e}\033[0m")
if package_name == "nltk":
sys.exit(1)
def download_nltk_resources(resources: list[str]):
"""Download the necessary resources from NLTK."""
for resource in resources:
try:
nltk.data.find(resource)
except LookupError:
nltk.download(resource.split("/")[1])
def initialize_packages():
"""Initialize packages and download the necessary resources."""
packages = ["nltk", "prettytable"]
for package in packages:
install_package(package)
if "nltk" in packages:
resources = ["tokenizers/punkt", "corpora/stopwords", "stemmers/rslp"]
download_nltk_resources(resources)
def load_tagger() -> UnigramTagger:
"""
Load or create the NLTK tagger and save it to a binary file.
Returns:
UnigramTagger: The NLTK UnigramTagger.
"""
try:
with open("tagger.bin", "rb") as file:
tagger: UnigramTagger = pickle.load(file)
except FileNotFoundError:
tagged_sentences = nltk.corpus.mac_morpho.tagged_sents()
tagger = UnigramTagger(tagged_sentences)
with open("tagger.bin", "wb") as file:
pickle.dump(tagger, file)
return tagger
def load_documents(base_filename: str) -> tuple[list[str], dict[int, str]]:
"""
Load document filenames and contents from a base file.
Args:
base_filename (str): The filename of the base file.
Returns:
tuple[list[str], dict[int, str]]: A tuple containing a list of document texts and a dictionary mapping
document IDs to their filenames.
"""
with open(base_filename, "r") as base_file:
texts: list[str] = []
docs: dict[int, str] = {}
for i, line in enumerate(base_file):
fields: list[str] = line.split()
doc_filename: str = fields[0]
docs.update({i + 1: doc_filename})
with open(doc_filename, encoding="utf8", mode="r") as doc_file:
texts.append(doc_file.read())
return texts, docs
def preprocess_text(
text: str,
tagger: UnigramTagger,
stemmer: RSLPStemmer,
show_classifications: bool = False,
) -> dict[str, int]:
"""
Preprocess the given text.
Args:
text (str): The text to preprocess.
tagger (UnigramTagger): The NLTK UnigramTagger for part-of-speech tagging.
stemmer (RSLPStemmer): The NLTK RSLPStemmer for stemming.
show_classifications (bool, optional): Whether to display word classifications in the terminal. Defaults to False.
Returns:
dict[str, int]: A dictionary mapping stemmed words to their frequencies.
"""
global global_classification_list
global_classification_list = []
word_list_final: list[str] = []
sentence: str = text
word_list: list[str] = word_tokenize(sentence)
word_list = [val.lower() for val in word_list]
classification_list: list[list[tuple[str, str]]] = []
stop_words = stopwords.words("portuguese")
for word in word_list:
token = word_tokenize(word)
classification = tagger.tag(token)
classification_list.append(classification)
stopword_classification_list: list[str] = []
for tag in classification_list:
for word, classification in tag:
if (
classification == "ART"
or classification == "PREP"
or classification == "KC"
or classification == "KS"
):
stopword_classification_list.append(word)
removal_items = [".", "..", "...", "!", "?", ","]
i = 0
while i < len(word_list):
if (
word_list[i] in removal_items
or word_list[i] in stop_words
or word_list[i] in stopword_classification_list
):
word_list.pop(i)
else:
i += 1
for word in word_list:
token = nltk.word_tokenize(word)
classification = tagger.tag(token)
global_classification_list.append(classification)
word_list_final.append(stemmer.stem(word))
word_count: dict[str, int] = {}
for word in word_list_final:
if word not in word_count:
word_count[word] = 0
word_count[word] = word_count[word] + 1
# Display classifications in the terminal if show_classifications is True
if show_classifications:
print("\n\033[1;35m==== Complete Word Classification ====\033[0m")
print_classifications(global_classification_list)
# for classification in global_classification_list:
# print(f"word - classification: {classification}")
print()
return word_count
def print_classifications(global_classification_list):
"""Print the word-classification pairs in the given list."""
for pair in global_classification_list:
print(f"word - classification: {pair}")
def build_inverted_index(
texts: list[str], tagger: UnigramTagger, stemmer: RSLPStemmer
) -> dict[str, dict[int, int]]:
"""
Build an inverted index from the preprocessed documents.
Args:
texts (list[str]): A list of preprocessed document texts.
tagger (UnigramTagger): The NLTK UnigramTagger for part-of-speech tagging.
stemmer (RSLPStemmer): The NLTK RSLPStemmer for stemming.
Returns:
dict[str, dict[int, int]]: The inverted index mapping terms to document IDs and term frequencies.
"""
inverted_index: dict[str, dict[int, int]] = {}
for idx, text in enumerate(texts):
word_count: dict[str, int] = preprocess_text(
text, tagger, stemmer, show_classifications=False
)
doc_id: int = idx + 1
for word in word_count:
if word in inverted_index:
inverted_index[word].update({doc_id: word_count[word]})
else:
inverted_index[word] = {doc_id: word_count[word]}
sorted_inverted_index = dict(
sorted(inverted_index.items(), key=lambda item: item[0])
)
return sorted_inverted_index
def display_inverted_index(inverted_index: dict[str, dict[int, int]]) -> None:
"""
Display the inverted index in the terminal.
Args:
inverted_index (dict[str, dict[int, int]]): The inverted index mapping terms to document IDs and term frequencies.
Returns:
None
"""
try:
from prettytable import PrettyTable
table = PrettyTable()
table.field_names = ["Term", "Documents"]
for term in inverted_index:
docs = ""
for doc_id in inverted_index[term]:
docs += f"{doc_id},{inverted_index[term][doc_id]} "
table.add_row([term, docs])
print(table)
except ImportError:
print(
"\033[1;31m⚠ prettytable not found. Displaying inverted index without it.\033[0m"
)
for term in inverted_index:
print(f"{term}: ", end="")
for doc_id in inverted_index[term]:
print(f"{doc_id},{inverted_index[term][doc_id]} ", end="")
print()
return
def save_inverted_index(inverted_index: dict[str, dict[int, int]]) -> None:
"""
Save the inverted index to a file.
Args:
inverted_index (dict[str, dict[int, int]]): The inverted index mapping terms to document IDs and term frequencies.
Returns:
None
"""
with open("index.txt", encoding="utf8", mode="w") as file:
for term in sorted(inverted_index):
file.write(term + ": ")
file.write(
" ".join(
f"{doc_id},{inverted_index[term][doc_id]}"
for doc_id in sorted(inverted_index[term])
)
)
file.write("\n")
def load_query(query_file: str) -> list[str]:
"""
Load the query from the given file.
Args:
query_file (str): The filename of the query file.
Returns:
list[str]: A list of query terms.
"""
with open(query_file, "r") as file:
query: list[str] = file.readline().split()
for i, val in enumerate(query):
query[i] = val.lower()
return query
def evaluate_query(
query: str, document_sort: dict[str, dict[int, int]], documents: dict[int, str]
) -> dict[int, list[bool]]:
"""
Evaluate the given query using a boolean model.
Args:
query (str): The query string.
document_sort (dict[str, dict[int, int]]): The inverted index mapping terms to document IDs and term frequencies.
documents (dict[int, str]): A dictionary mapping document IDs to their filenames.
Returns:
dict[int, list[bool]]: A dictionary mapping document IDs to a list of Boolean results for the query.
"""
query_terms: list[str] = load_query(query)
def search_word(word: str, doc_id: int) -> bool:
"""Search for the given word in the given document."""
stemmer: RSLPStemmer = RSLPStemmer()
for term, doc_freqs in document_sort.items():
if stemmer.stem(word) == term:
for doc, _ in doc_freqs.items():
if doc == doc_id:
return True
return False
query_results: dict[int, list[bool]] = {}
single_term: bool = True
and_operator_count: int = 0
# Processing query terms
for i, term in enumerate(query_terms):
if term == "&":
and_operator_count += 1
# Evaluating conjunctions (AND)
for doc_id in range(1, len(documents) + 1):
if query_terms[i - 1][0] == "!":
term1_result: bool = not search_word(query_terms[i - 1][1:], doc_id)
else:
term1_result: bool = search_word(query_terms[i - 1], doc_id)
if query_terms[i + 1][0] == "!":
term2_result: bool = not search_word(query_terms[i + 1][1:], doc_id)
else:
term2_result: bool = search_word(query_terms[i + 1], doc_id)
term_final_result: bool = term1_result and term2_result
if doc_id in query_results:
query_results[doc_id].append(term_final_result)
else:
query_results[doc_id] = [term_final_result]
single_term = False
# Processing single query terms
for i, term in enumerate(query_terms):
if single_term:
for doc_id in range(1, len(documents) + 1):
if term[0] == "!":
term_result: bool = not search_word(term[1:], doc_id)
else:
term_result: bool = search_word(term, doc_id)
query_results.setdefault(doc_id, []).append(term_result)
# Processing disjunctions (OR) and more complex queries
for i, term in enumerate(query_terms):
for doc_id in range(1, len(documents) + 1):
if term == "|":
if len(query_results) != 0:
if query_terms[i - 2] != "&":
if query_terms[i - 1][0] == "!":
term1_result: bool = not search_word(
query_terms[i - 1][1:], doc_id
)
else:
term1_result: bool = search_word(query_terms[i - 1], doc_id)
term2_result: bool = query_results[doc_id][-1]
term_final_result: bool = term1_result or term2_result
query_results.setdefault(doc_id, []).append(term_final_result)
if len(query_terms) - 2 == i:
if len(query_terms) == 7:
term1_result: bool = query_results[doc_id][-2]
else:
term1_result: bool = query_results[doc_id][-1]
if query_terms[i + 1][0] == "!":
term2_result: bool = not search_word(
query_terms[i + 1][1:], doc_id
)
else:
term2_result: bool = search_word(query_terms[i + 1], doc_id)
term_final_result: bool = term1_result or term2_result
query_results.setdefault(doc_id, []).append(term_final_result)
# Final processing for complex queries
for doc_id in range(1, len(documents) + 1):
if len(query_terms) > 3:
term1_result: bool = query_results[doc_id][-1]
term2_result: bool = query_results[doc_id][-2]
term_final_result: bool = (
term1_result and term2_result
if and_operator_count > 1
else term1_result or term2_result
)
query_results.setdefault(doc_id, []).append(term_final_result)
return query_results # Return the results
def save_results(
query_results: dict[int, list[bool]], documents: dict[int, str]
) -> None:
"""
Save query results to a file and display them.
Args:
query_results (dict[int, list[bool]]): A dictionary mapping document IDs to a list of Boolean results for the query.
documents (dict[int, str]): A dictionary mapping document IDs to their filenames.
Returns:
None
"""
count: int = sum(1 for doc_id in query_results if query_results[doc_id][-1])
if count > 0:
with open("response.txt", encoding="utf8", mode="w") as file:
print(count, file=file)
for doc_id in query_results:
if query_results[doc_id][-1]:
file.write(documents[doc_id] + "\n")
else:
print("No results found.")
def display_query_results(
query_results: dict[int, list[bool]],
documents: dict[int, str],
query_file_path: str,
) -> None:
"""
Display the query results in the terminal.
Args:
query_results (dict[int, list[bool]]): A dictionary mapping document IDs to a list of Boolean results for the query.
documents (dict[int, str]): A dictionary mapping document IDs to their filenames.
query_file_path (str): The path to the query file.
Returns:
None
"""
count: int = sum(1 for doc_id in query_results if query_results[doc_id][-1])
num_docs: int = len(query_results)
if count > 0:
with open(query_file_path, encoding="utf8", mode="r") as query_file:
query = query_file.read().strip()
print(f"\nQuery: {query}")
print(f"{count} out of {num_docs} documents matched the query.")
print(f"Check the file 'resposta.txt' for the results.\n")
try:
from prettytable import PrettyTable
table = PrettyTable()
table.field_names = ["Document Name", "Result"]
for doc_id in query_results:
if query_results[doc_id][-1]:
table.add_row([documents[doc_id], "Yes"])
else:
table.add_row([documents[doc_id], "No"])
print(table)
except ImportError:
print(
"\033[1;31m⚠ prettytable not found. Displaying query results without it.\033[0m"
)
for doc_id in query_results:
if query_results[doc_id][-1]:
print(f"{documents[doc_id]}: Yes")
else:
print(f"{documents[doc_id]}: No")
return
def display_detailed_results(query_results: dict[int, list[bool]]) -> None:
"""
Display the detailed query results in a table.
Args:
query_results (dict[int, list[bool]]): A dictionary mapping document IDs to a list of Boolean results for the query.
Returns:
None
"""
try:
from prettytable import PrettyTable
table = PrettyTable()
table.field_names = ["Document", "Result"]
for doc_id, result in query_results.items():
table.add_row([doc_id, result[-1]])
print(table)
except ImportError:
print(
"\033[1;31m⚠ prettytable not found. Displaying detailed results without it.\033[0m"
)
for doc_id, result in query_results.items():
print(f"Document {doc_id}: {result}")
return
def display_output(
document_sort: dict[str, dict[int, int]],
query_results: dict[int, list[bool]],
documents: dict[int, str],
base_filename: str,
query_filename: str,
) -> None:
"""
Display the output in the terminal.
Args:
document_sort (dict[str, dict[int, int]]): The inverted index mapping terms to document IDs and term frequencies.
query_results (dict[int, list[bool]]): A dictionary mapping document IDs to a list of Boolean results for the query.
documents (dict[int, str]): A dictionary mapping document IDs to their filenames.
base_filename (str): The filename of the base file.
query_filename (str): The filename of the query file.
Returns:
None
"""
# Display the word classifications in the terminal
# To display the complete list of word classifications, set show_classifications to True in preprocess_text(), line 199
# print("\n\033[1;34m==== Word Classification ====\033[0m")
# print_classifications(global_classification_list)
print()
# Display the inverted index in the terminal
print("\033[1;34m==== Inverted Index ====\033[0m")
display_inverted_index(document_sort)
print()
# Display the query results in the terminal
print("\033[1;34m==== Query Results ====\033[0m")
display_query_results(query_results, documents, query_file_path=query_filename)
print()
# Display the detailed query results in the terminal
print("\033[1;34m==== Detailed Results ====\033[0m")
display_detailed_results(query_results)
print()
# Display the execution information in the terminal
print("\033[1;34m==== Execution Information ====\033[0m")
print(f"Base File: {base_filename}")
print(f"Query File: {query_filename}")
def measure_execution_time(func: Callable[..., Any]) -> Callable[..., Any]:
"""Decorator function to measure the execution time of a function."""
def wrapper(*args: Any, **kwargs: Any) -> Any:
start_time: float = time.time()
result: Any = func(*args, **kwargs)
end_time: float = time.time()
total_time: float = end_time - start_time
print(f"Total execution time: {total_time:.2f} seconds\n")
return result
return wrapper
@measure_execution_time
def main(base_filename: str, query_filename: str) -> None:
"""
Main function.
Args:
base_filename (str): The filename of the base file.
query_filename (str): The filename of the query file.
Returns:
None
"""
initialize_packages() # Initialize packages and download the necessary resources
# --- Load the documents ---
print("\033[1;37m📚 Loading documents...\033[0m")
time.sleep(1)
texts, documents = load_documents(base_filename)
print(f"\033[1;32;40m✅ Loaded {len(documents)} documents!\033[0m")
# --- Build the inverted index from the documents ---
print("\033[1;36m🔍 Building inverted index...\n\033[0m")
time.sleep(1)
# --- Declare the variables for the inverted index ---
tagger = load_tagger()
stemmer = RSLPStemmer()
# --- Build the inverted index and save it to a file ---
document_sort = build_inverted_index(texts, tagger, stemmer)
save_inverted_index(document_sort)
print(f"\033[1;32;40m✅ Built inverted index!\033[0m")
# --- Load the query ---
print("\033[1;34m📝 Loading query...\033[0m")
time.sleep(1)
query = query_filename
# query = load_query(query_filename)
# --- Evaluate the query ---
print("\033[1;35m🔎 Evaluating query...\033[0m")
time.sleep(1)
results_all = {}
# --- Evaluate the query and save the results to a file ---
try:
from tqdm import trange
total_iterations = int(len(documents) / 2)
for _ in trange(
total_iterations,
desc="\033[1;31m🚀 Query progress\033[0m",
bar_format="{l_bar}\033[1;31m{bar:50}\033[0m{r_bar}",
):
query_results = evaluate_query(query, document_sort, documents)
results_all.update(query_results)
save_results(results_all, documents)
time.sleep(0.1)
except ImportError:
print("\033[1;31m⚠ tqdm not found. Running query without progress bar.\033[0m")
for _, _ in enumerate(documents):
query_results = evaluate_query(query, document_sort, documents)
results_all.update(query_results)
save_results(results_all, documents)
print(f"\033[1;32;40m✅ Evaluated query!\033[0m")
print("\033[1;33m✨ Done!\033[0m")
# --- Display the output ---
display_output(
document_sort,
results_all,
documents,
base_filename,
query_filename,
)
if __name__ == "__main__":
if len(sys.argv) != 3:
print(
"\033[91m\n❌ ERROR: Invalid arguments! Please use the following format:\n"
)
print("\033[93mUsage: python program_name.py base_file query_file\n")
print(
"\033[96mAlternatively, you can run the task script by using VSCode's shortcut Ctrl+Shift+B.\n"
)
print(
"\033[92mTip: you can use the command './delete.bat' (on Windows) or './delete.sh' (on Linux) to delete the output files,"
)
print("generating new output files instead of overwriting the old ones.")
print("\033[0m")
sys.exit(1)
# base_filename, query_filename = sys.argv[1], sys.argv[2]
main(base_filename=sys.argv[1], query_filename=sys.argv[2])