简体   繁体   中英

sentiment analysis for 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']

I am not able to find the neutral tweets using loops, can anyone help me out?

The following code is straightforward for analyzing sentiment in each given text. It might provide a concept of the task.

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 analyze(text, pos_words, neg_words):
    score = 0
    score += sum(1 for word in pos_words if word in text)
    score += sum(-1 for word in neg_words if word in text)

    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: 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: positive

tweet: Spent my day with family!! #happy
sentiment: positive

tweet: Check out my blog post on common string operations in Python. #zerotopandas
sentiment: neutral

tweet: Freecodecamp has great coding tutorials. #skillup
sentiment: positive
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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM