Skip to content

Commit a5e0f01

Browse files
committed
Add searching by word and topic functionality to postsearch module
1 parent 79a277a commit a5e0f01

3 files changed

Lines changed: 186 additions & 4 deletions

File tree

samples/new.xml

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<users>
2+
<user>
3+
<id>1</id>
4+
<name>Ahmed Ali</name>
5+
<posts>
6+
<post>
7+
<body>How solar energy is changing the world.</body>
8+
<topics>
9+
<topic>solar_energy</topic>
10+
<topic>technology</topic>
11+
</topics>
12+
</post>
13+
<post>
14+
<body>The importance of financial literacy in today's economy.</body>
15+
<topics>
16+
<topic>economy</topic>
17+
</topics>
18+
</post>
19+
</posts>
20+
<followers>
21+
<follower>
22+
<id>2</id>
23+
</follower>
24+
<follower>
25+
<id>3</id>
26+
</follower>
27+
<follower>
28+
<id>4</id>
29+
</follower>
30+
</followers>
31+
</user>
32+
<user>
33+
<id>2</id>
34+
<name>Yasser Ahmed</name>
35+
<posts>
36+
<post>
37+
<body>Education reforms: What should come next?</body>
38+
<topics>
39+
<topic>education</topic>
40+
<topic>policy</topic>
41+
</topics>
42+
</post>
43+
</posts>
44+
<followers>
45+
<follower>
46+
<id>1</id>
47+
</follower>
48+
<follower>
49+
<id>5</id>
50+
</follower>
51+
</followers>
52+
</user>
53+
<user>
54+
<id>3</id>
55+
<name>Mohamed Sherif</name>
56+
<posts>
57+
<post>
58+
<body>The rise of AI in sports analytics.</body>
59+
<topics>
60+
<topic>sports</topic>
61+
<topic>technology</topic>
62+
</topics>
63+
</post>
64+
<post>
65+
<body>Top 10 moments in football history.</body>
66+
<topics>
67+
<topic>sports</topic>
68+
</topics>
69+
</post>
70+
</posts>
71+
<followers>
72+
<follower>
73+
<id>1</id>
74+
</follower>
75+
<follower>
76+
<id>2</id>
77+
</follower>
78+
</followers>
79+
</user>
80+
<user>
81+
<id>4</id>
82+
<name>Sara Mahmoud</name>
83+
<posts>
84+
<post>
85+
<body>The impact of clean water initiatives in rural areas.</body>
86+
<topics>
87+
<topic>environment</topic>
88+
<topic>policy</topic>
89+
</topics>
90+
</post>
91+
</posts>
92+
<followers>
93+
<follower>
94+
<id>1</id>
95+
</follower>
96+
<follower>
97+
<id>3</id>
98+
</follower>
99+
<follower>
100+
<id>5</id>
101+
</follower>
102+
</followers>
103+
</user>
104+
<user>
105+
<id>5</id>
106+
<name>Omar Khaled</name>
107+
<posts>
108+
<post>
109+
<body>Advancements in renewable energy technologies.</body>
110+
<topics>
111+
<topic>technology</topic>
112+
<topic>solar_energy</topic>
113+
</topics>
114+
</post>
115+
<post>
116+
<body>Breaking down the future of urban planning.</body>
117+
<topics>
118+
<topic>policy</topic>
119+
<topic>economy</topic>
120+
</topics>
121+
</post>
122+
</posts>
123+
<followers>
124+
<follower>
125+
<id>2</id>
126+
</follower>
127+
</followers>
128+
</user>
129+
<user>
130+
<id>6</id>
131+
<name>Fatima Saleh</name>
132+
<posts>
133+
<post>
134+
<body>How to balance work and personal life effectively.</body>
135+
<topics>
136+
<topic>self_help</topic>
137+
</topics>
138+
</post>
139+
<post>
140+
<body>Improving productivity with modern tools.</body>
141+
<topics>
142+
<topic>technology</topic>
143+
<topic>self_help</topic>
144+
</topics>
145+
</post>
146+
</posts>
147+
<followers>
148+
<follower>
149+
<id>3</id>
150+
</follower>
151+
<follower>
152+
<id>5</id>
153+
</follower>
154+
<follower>
155+
<id>4</id>
156+
</follower>
157+
</followers>
158+
</user>
159+
</users>

src/cli/cli_handler.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ def most_active_user(input_file):
287287
try:
288288
graph = GraphRepresentation.build_graph(input_file)
289289
user = NetworkAnalysis(graph).get_most_active_user()
290-
print(f"{Fore.GREEN}Most active user(s):",*user)
290+
print(f"{Fore.GREEN}Most active user(s) ID(s):",*user)
291291
except Exception as e:
292292
print(f"{Fore.RED}Error finding most active user: {e}")
293293

@@ -363,14 +363,18 @@ def search_posts(input_file, word=None, topic=None):
363363
print(f"{Fore.LIGHTYELLOW_EX}You forgot to add the extension to the input file :) \nAppending '.xml' to the input file name.")
364364
try:
365365
searcher = PostSearch(input_file)
366-
if word:
367-
results = searcher.search_word(word)
368-
print(f"{Fore.GREEN}Posts containing the word '{word}':")
366+
if word and topic:
367+
results = searcher.search_word_topic(word, topic)
368+
print(f"{Fore.GREEN}Posts containing the word '{word}' and related to the topic '{topic}':")
369369
print(*results, sep="\n")
370370
elif topic:
371371
results = searcher.search_topic(topic)
372372
print(f"{Fore.GREEN}Posts related to the topic '{topic}':")
373373
print(*results, sep="\n")
374+
elif word:
375+
results = searcher.search_word(word)
376+
print(f"{Fore.GREEN}Posts containing the word '{word}':")
377+
print(*results, sep="\n")
374378
except Exception as e:
375379
print(f"{Fore.RED}Error searching posts: {e}")
376380

src/postsearch/post_search.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,25 @@ def search_topic(self, topic):
7575
user_start = user_end + len("</user>")
7676
return posts
7777

78+
79+
def search_word_topic(self, word, topic):
80+
"""
81+
Search for posts containing a specific word and having a specific topic.
82+
"""
83+
word_posts = []
84+
topic_posts = []
85+
# Search for posts containing the word
86+
word_posts = self.search_word(word)
87+
88+
# Search for posts with the specific topic
89+
topic_posts = self.search_topic(topic)
90+
91+
# Find the intersection of both lists
92+
result_posts = [post for post in word_posts if post in topic_posts]
93+
94+
return result_posts
95+
96+
7897
def _extract_tag_value(self, content, tag):
7998
"""
8099
Extract the value of a given tag from the content.

0 commit comments

Comments
 (0)