DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
WORKSHEET 1
Student Name: Devansh Raturi UID:22BCS16169
Branch: CSE Section/Group: TPP-802 B
Semester: 4th Date of Performance: 08.02.2024
Subject Name: Numerical Methods and Optimization Using Python
Subject Code: 22CSH-259
1. Aim:
You are working on a project for a social media analytics company. Your task is to develop a
Python function that analyzes user comments on a social media platform to identify trends and
topics of interest among users.
The function should take in a list of user comments as input and return the following
information:
1. The most commonly mentioned topics or keywords in the comments.
2. The sentiment distribution of the comments (percentage of positive, negative, and
neutral comments).
Outline how you would design and implement this function in Python. Describe the input
parameters the function would accept, the steps involved in analyzing the comments, and any
external libraries.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
2. Source Code:
def analyze_comments(comments):
word_counts = {} for
comment in comments:
words = comment.split()
for word in words:
word = word.lower()
if word not in word_counts:
word_counts[word] = 1 else:
word_counts[word] += 1
most_common_word = max(word_counts, key=word_counts.get)
positive_keywords = ['love', 'like', 'awesome', 'great',
'amazing']
negative_keywords = ['hate', 'dislike', 'terrible', 'awful',
'horrible'] positive_count = 0
negative_count = 0 neutral_count = 0
for comment in comments:
lowercase_comment = comment.lower()
if any(keyword in lowercase_comment for keyword in
positive_keywords): positive_count += 1
elif any(keyword in lowercase_comment for keyword in
negative_keywords): negative_count += 1
else:
neutral_count += 1
total_comments = len(comments)
positive_percentage = (positive_count / total_comments) * 100
negative_percentage = (negative_count / total_comments) * 100
neutral_percentage = (neutral_count / total_comments) * 100
results = {
"most_common_topic": most_common_word,
"sentiment_distribution": {
"positive": positive_percentage,
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
"negative": negative_percentage,
"neutral": neutral_percentage
}
}
return results
comments = [
"Just finished an intense gaming session with friends, what a
blast! #gaming",
"Can't wait for release of new gaming console, it's going
to be epic! #excited",
"Spent all day playing my favorite game, totally worth it!
#gamerlife",
"Graphics on this new game are insane, feels like I'm in
another world! #gamingcommunity",
"Started streaming gameplay on Twitch, come join , have
fun! #streamer",
"Joined a gaming tournament and made it to finals, wish me
luck! #competitivegaming",
"Got a new gaming mouse and it's a game-changer! #gadget",
"I hate this new update, totally awful! #awful", "
storyline in this RPG game is so immersive, can't stop
playing! #roleplaying",
"Just got latest gaming headset, sound quality is
amazing! #audiophile",
"Binge-playing this new game all weekend, goodbye social
life! #gamingaddict",
" gaming industry keeps surprising us with innovative
technology, can't wait to see what's next! #futureofgaming",
"Finally beat final boss in my favorite game, such a feeling
of accomplishment! #victory",
"Had a frustrating gaming experience today, lost all my
progress! #gamingwoes",
]
analysis_results = analyze_comments(comments)
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
print("Most Common Topic:",
analysis_results["most_common_topic"])
print("Sentiment Distribution:",
analysis_results["sentiment_distribution"])
3. Screenshot of Outputs: