Skip to content

Commit b8bde31

Browse files
Merge pull request #5 from Muawiya-contact/SE-UNDO
Good work. Try to find more room for improvements.
2 parents 0e54b6c + 65bf2f4 commit b8bde31

8 files changed

Lines changed: 43 additions & 12 deletions

File tree

SearchEngine.zip

7.23 KB
Binary file not shown.
1.15 KB
Binary file not shown.

SearchEngine/documents/doc1.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
Python is a powerful programming language. It is widely used in data science, artificial intelligence, and web development. Its simple and readable syntax makes it an excellent choice for beginners. Many open-source libraries are available to extend its functionality.
1+
Python is a powerful programming language.
2+
It is widely used in data science, artificial intelligence, and web development.
3+
Its simple and readable syntax makes it an excellent choice for beginners.
4+
Many open-source libraries are available to extend its functionality.

SearchEngine/documents/doc2.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Data structures are a fundamental concept in computer science.
1+
Data structures are a fundamental concept in computer science or CS.
22
They are used to organize and store data efficiently.
33
Examples include arrays, linked lists, stacks, and queues.
44
Understanding these structures is key to writing effective algorithms.

SearchEngine/documents/doc3.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
An algorithm is a step-by-step procedure or formula for solving a problem. Algorithms are crucial in computer programming and are independent of the programming language. Searching and sorting are classic examples of problems solved with various algorithms.
1+
An algorithm is a step-by-step procedure or formula for solving a problem.
2+
Algorithms are crucial in computer programming and are independent of the programming language.
3+
Searching and sorting are classic examples of problems solved with various algorithms.

SearchEngine/documents/doc4.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
A stack is a linear data structure that follows the Last-In, First-Out (LIFO) principle. Think of it like a stack of plates. The last plate you put on top is the first one you take off. Operations on a stack include push (to add an item) and pop (to remove an item).
1+
A stack is a linear data structure that follows the Last-In, First-Out (LIFO) principle.
2+
Think of it like a stack of plates. The last plate you put on top is the first one you take off.
3+
Operations on a stack include push (to add an item) and pop (to remove an item).

SearchEngine/documents/doc5.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
The field of artificial intelligence, or AI, involves the development of computer systems that can perform tasks that would normally require human intelligence. This includes things like visual perception, speech recognition, decision-making, and language translation. Machine learning is a subfield of AI.
1+
The field of artificial intelligence, or AI, involves the development of computer systems that can perform tasks that would normally require human intelligence.
2+
This includes things like visual perception, speech recognition, decision-making, and language translation.
3+
Machine learning is a subfield of AI.

SearchEngine/search.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
from index import InvertedIndex
55

66
# Path to the folder that has all text files
7-
PATH = "./documents" # First we go to base dir
7+
PATH = "./documents" # to base dir
88

99
class SearchSim:
1010
"""A simple search engine simulation."""
1111

1212
def __init__(self, path=PATH):
1313
self.index = InvertedIndex() # for word search
1414
self.history = Stack() # to store search history
15+
self.redo = Stack() # to redo
1516
self.path = path # folder path
1617
self.results = [] # last search results
1718
self._load() # load documents into index
@@ -35,13 +36,15 @@ def _load(self):
3536
def run(self):
3637
"""Main loop for user interaction."""
3738
while True:
38-
user_input = input("\nEnter search query, 'back', 'show', or 'quit': ").strip().lower()
39+
user_input = input("\nEnter search query, 'back', 'next', 'show', or 'quit': ").strip().lower()
3940

4041
if user_input == "quit":
4142
print("Goodbye!")
4243
break
4344
elif user_input == "back":
4445
self._back()
46+
elif user_input == "next":
47+
self._next()
4548
elif user_input == "show":
4649
self.history.show()
4750
elif user_input: # if user typed something
@@ -66,8 +69,8 @@ def _search(self, query):
6669
def _open_doc(self):
6770
"""Open and show the contents of a selected document."""
6871
while True:
69-
choice = input("\nEnter document number to open, or 'next': ").strip().lower()
70-
if choice == "next":
72+
choice = input("\nEnter document number to open, or 'continue': ").strip().lower()
73+
if choice == "continue":
7174
break
7275
try:
7376
num = int(choice)
@@ -78,7 +81,7 @@ def _open_doc(self):
7881
else:
7982
print("Invalid number.")
8083
except ValueError:
81-
print("Please enter a valid number or 'next'.")
84+
print("Please enter a valid number or 'continue'.")
8285
except FileNotFoundError:
8386
print("File not found.")
8487

@@ -88,8 +91,9 @@ def _back(self):
8891
print("No previous search available.")
8992
return
9093

91-
self.history.pop() # remove current search
92-
prev_query = self.history.peek() # last one left
94+
current = self.history.pop() # remove current
95+
self.redo.push(current) # store it for redo
96+
prev_query = self.history.peek() # now last one left
9397
print(f"\nBack to: '{prev_query}'")
9498

9599
self.results = self.index.search(prev_query)
@@ -99,3 +103,21 @@ def _back(self):
99103
for i, r in enumerate(self.results, start=1):
100104
print(f"{i}. {r['doc']}.txt | Score: {r['score']}")
101105
self._open_doc()
106+
107+
def _next(self):
108+
"""Redo the last undone search query."""
109+
if self.redo.empty():
110+
print("Nothing to Redo!")
111+
return
112+
113+
redo_query = self.redo.pop() # get last undone query
114+
self.history.push(redo_query) # put it back into history
115+
print(f"\nRedo: '{redo_query}'")
116+
117+
self.results = self.index.search(redo_query)
118+
if not self.results:
119+
print("No matches found.")
120+
else:
121+
for i, r in enumerate(self.results, start=1):
122+
print(f"{i}. {r['doc']}.txt | Score: {r['score']}")
123+
self._open_doc()

0 commit comments

Comments
 (0)