繁体   English   中英

推文的情绪分析

[英]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']

我无法使用循环找到中性推文,有人可以帮我吗?

以下代码可直接用于分析每个给定文本中的情绪。 它可能提供任务的概念。

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)

您还可以更正悲伤推文和中立推文的代码吗?

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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