tweets = [
"Wow, what a great day today!! #sunshine",
"I feel sad about the things going on around us. #covid19",
"I'm really excited to learn Python with @JovianML #zerotopandas",
"This is a really nice song. #linkinpark",
"The python programming language is useful for data science",
"Why do bad things happen to me?",
"Apple announces the release of the new iPhone 12. Fans are excited.",
"Spent my day with family!! #happy",
"Check out my blog post on common string operations in Python. #zerotopandas",
"Freecodecamp has great coding tutorials. #skillup"
]
happy_words = ['great', 'excited', 'happy', 'nice', 'wonderful', 'amazing', 'good', 'best']
sad_words = ['sad', 'bad', 'tragic', 'unhappy', 'worst']
I am not able to find the neutral tweets using loops, can anyone help me out?
CodePudding user response:
The following code is straightforward for analyzing sentiment in each given text. It might provide a concept of the task.
Edit: Add text.split()
to prevent comparing whole words, not parts of words. (example: unhappy
and happy
).
tweets = ["I'am unhappy", "Wow, what a great day today!! #sunshine", "I feel sad about the things going on around us. #covid19", "I'm really excited to learn Python with @JovianML #zerotopandas", "This is a really nice song. #linkinpark", "The python programming language is useful for data science", "Why do bad things happen to me?", "Apple announces the release of the new iPhone 12. Fans are excited.", "Spent my day with family!! #happy", "Check out my blog post on common string operations in Python. #zerotopandas", "Freecodecamp has great coding tutorials. #skillup" ]
happy_words = ['great', 'excited', 'happy', 'nice', 'wonderful', 'amazing', 'good', 'best']
sad_words = ['sad', 'bad', 'tragic', 'unhappy', 'worst']
def analyze(text, pos_words, neg_words):
score = 0
tokens = text.split(" ")
score = sum(1 for word in tokens if word in pos_words)
score = sum(-1 for word in tokens if word in neg_words)
if score > 0:
sentiment = "positive"
elif score < 0:
sentiment = "negative"
else:
sentiment = "neutral"
return sentiment
for text in tweets:
sentiment = analyze(text, happy_words, sad_words)
print(f"tweet: {text}")
print(f"sentiment: {sentiment}")
print()
Output:
tweet: I'am unhappy
sentiment: negative
tweet: Wow, what a great day today!! #sunshine
sentiment: positive
tweet: I feel sad about the things going on around us. #covid19
sentiment: negative
tweet: I'm really excited to learn Python with @JovianML #zerotopandas
sentiment: positive
tweet: This is a really nice song. #linkinpark
sentiment: positive
tweet: The python programming language is useful for data science
sentiment: neutral
tweet: Why do bad things happen to me?
sentiment: negative
tweet: Apple announces the release of the new iPhone 12. Fans are excited.
sentiment: neutral
tweet: Spent my day with family!! #happy
sentiment: neutral
tweet: Check out my blog post on common string operations in Python. #zerotopandas
sentiment: neutral
tweet: Freecodecamp has great coding tutorials. #skillup
sentiment: positive
CodePudding user response:
tweets = [
"Wow, what a great day today!! #sunshine",
"I feel sad about the things going on around us. #covid19",
"I'm really excited to learn Python with @JovianML
#zerotopandas",
"This is a really nice song. #linkinpark",
"The python programming language is useful for data science",
"Why do bad things happen to me?",
"Apple announces the release of the new iPhone 12. Fans are
excited.",
"Spent my day with family!! #happy",
"Check out my blog post on common string operations in Python.
#zerotopandas",
"Freecodecamp has great coding tutorials. #skillup"
]
happy_words = ['great', 'excited', 'happy', 'nice', 'wonderful',
'amazing', 'good', 'best']
sad_words = ['sad', 'bad', 'tragic', 'unhappy', 'worst']
def analyse(items):
number_of_happy_tweets = 0
for tweet in tweets:
for words in happy_words:
if words in tweet:
number_of_happy_tweets = 1
number_of_sad_tweets = 0
for tweet in tweets:
for words in sad_words:
if words in tweet:
number_of_sad_tweets = 1
return 'number of happy tweets are {} and number of sad
tweets are {}'.format(number_of_happy_tweets,number_of_sad_tweets)
analyse(tweets)
CodePudding user response:
Can you also correct the code for sad tweets and neutral tweets?
tweets = [
"Wow, what a great day today!! #sunshine",
"I feel sad about the things going on around us. #covid19",
"I'm really excited to learn Python with @JovianML #zerotopandas",
"This is a really nice song. #linkinpark",
"The python programming language is useful for data science",
"Why do bad things happen to me?",
"Apple announces the release of the new iPhone 12. Fans are excited.",
"Spent my day with family!! #happy",
"Check out my blog post on common string operations in Python.
#zerotopandas",
"Freecodecamp has great coding tutorials. #skillup"
]
happy_words = ['great', 'excited', 'happy', 'nice', 'wonderful',
'amazing', 'good', 'best']
sad_words = ['sad', 'bad', 'tragic', 'unhappy', 'worst']
def analyse(items, good, bad):
number_of_happy_tweets = 0
number_of_sad_tweets = 0
for item in items:
for words in good or bad:
if words in item:
number_of_happy_tweets = 1
else:
number_of_sad_tweets = 1
return 'number of happy tweets are {} and number of sad tweets are
{}'.format(number_of_happy_tweets,number_of_sad_tweets)
analyse(tweets, happy_words, sad_words)