-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_topic.py
More file actions
45 lines (43 loc) · 2.11 KB
/
input_topic.py
File metadata and controls
45 lines (43 loc) · 2.11 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
# input_topic.py
# This script is designed to take user input for a topic or a keyword and validate it before using it in a news summarization application.
def get_topic():
topic = input("Enter a topic to search for news articles: ")
if not topic:
print("No topic provided. Please enter a valid topic.")
return None
if len(topic) > 100: # Arbitrary limit for topic length
print("Topic is too long. Please enter a shorter topic.")
return None
if not topic.isascii():
print("Topic contains non-ASCII characters. Please use only ASCII characters.")
return None
if not topic.isprintable():
print("Topic contains non-printable characters. Please use only printable characters.")
return None
if topic[0].isdigit():
print("Topic should not start with a digit. Please enter a valid topic.")
return None
if topic[0] == ' ':
print("Topic should not start with a space. Please enter a valid topic.")
return None
# Normalize the input to lowercase and strip any leading/trailing whitespace.
topic = topic.lower().strip()
# Check for special characters and replace them with spaces.
special_chars = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '{', '}', '[', ']', '|', ':', ';', '"', "'", '<', '>', ',', '.', '?', '/', '\\']
for char in special_chars:
topic = topic.replace(char, ' ')
# Remove extra spaces
topic = ' '.join(topic.split())
# Check if the topic is empty after normalization
if not topic:
print("Topic is empty after normalization. Please enter a valid topic.")
return None
# Check for common stop words and remove them
stop_words = ['the', 'is', 'in', 'and', 'to', 'a', 'of', 'for', 'on', 'with', 'as', 'by', 'this', 'that']
topic_words = topic.split()
topic = ' '.join([word for word in topic_words if word not in stop_words])
# Check if the topic is empty after removing stop words
if not topic:
print("Topic is empty after removing stop words. Please enter a valid topic.")
return None
return topic