Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

EVALUATION 1

Student Name: Aman Thakur UID:22BCS16401

Branch: CSE Section/Group:22BCS-901-A


Semester:4 Date of Performance:7-02-24
Subject Name: Python Subject Code: 22CSH-259

1: Aim: You are working on a project for the social media analytics company
define a function that analyses the user comments on the social
media platforms to identify the trend and topic of interest among
the people.

The function should take in a list of user comments as input and return
the following information:

1.The most mentioned topics or keywords in the comments.

2.The sentiment distribution of the comments (percentage of


positive, negative, and neutral comments).

CODE:

def analyze_comments(user_comments):

# Initialize variables

common_keywords = {}
sentiment_counts = {'positive': 0, 'negative': 0}

# Process each comment


for comment in user_comments:
words = comment.lower().split()

# Update common keywords


for word in words:
if word.isalnum():
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
common_keywords[word] = common_keywords.get(word, 0) + 1

# Update sentiment counts


if 'love' in words or 'great' in words or 'amazing' in words:
sentiment_counts['positive'] += 1

elif 'terrible' in words or 'disappointing' in words or ('not' in words and


'satisfied' in words):
sentiment_counts['negative'] += 1

# Identify common keywords/topics


common_keywords = sorted(common_keywords.items(), key=lambda x:
x[1], reverse=True)[:5]

# Calculate sentiment percentages


total_comments = len(user_comments)
positive_percentage = (sentiment_counts['positive'] / total_comments) *
100
negative_percentage = (sentiment_counts['negative'] / total_comments)
* 100

print("Common keywords/topics:")
for keyword, freq in common_keywords:
print(f"{keyword}: {freq} mentions")

print("\nSentiment Analysis:")
print(f"Positive comments: {positive_percentage:.2f}%")
print(f"Negative comments: {negative_percentage:.2f}%")
print(f"Neutral comments: {100 - positive_percentage -
negative_percentage:.2f}%")

# Test the function


user_comments = [
"I love this product, it's amazing!",
"The customer service is terrible, very disappointing experience.",
"Great job on the new update!",
"I'm not satisfied with the quality, it needs improvement."
]
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
analyze_comments(user_comments)

OUTPUT:

LEARNING OUTCOMES:

1. Understand how to initialize and manipulate dictionaries in Python for


counting occurrences and storing sentiment counts.

2. Learn how to process lists of strings, tokenize them into individual words,
and perform operations on them.

3. Gain familiarity with common string operations like converting to


lowercase and splitting.

4. Practice using control flow statements (if-elif-else) to perform sentiment


analysis based on predefined keywords.

You might also like