python 关于LHL Facebook帖子评论的情绪分析,请访问https://www.facebook.com/leehsienloong/posts/1505690826160285

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 关于LHL Facebook帖子评论的情绪分析,请访问https://www.facebook.com/leehsienloong/posts/1505690826160285相关的知识,希望对你有一定的参考价值。

import requests

graph_api_version = 'v2.9'
access_token = 'YOUR_FACEBOOK_ACCESS_TOKEN_HERE'

# LHL's Facebook user id
user_id = '125845680811480'

# the id of LHL's response post at https://www.facebook.com/leehsienloong/posts/1505690826160285
post_id = '1505690826160285'

# the graph API endpoint for comments on LHL's post
url = 'https://graph.facebook.com/{}/{}_{}/comments'.format(graph_api_version, user_id, post_id)

comments = []

r = requests.get(url, params={'access_token': access_token})
while True:
    data = r.json()

    # catch errors returned by the Graph API
    if 'error' in data:
        raise Exception(data['error']['message'])

    # append the text of each comment into the comments list
    for comment in data['data']:
        # remove line breaks in each comment
        text = comment['message'].replace('\n', ' ')
        comments.append(text)

    print('got {} comments'.format(len(data['data'])))

    # check if there are more comments
    if 'paging' in data and 'next' in data['paging']:
        r = requests.get(data['paging']['next'])
    else:
        break

# save the comments to a file
with open('comments.txt', 'w', encoding='utf-8') as f:
    for comment in comments:
        f.write(comment + '\n')
from google.cloud import language, exceptions

# create a Google Cloud Natural Languague API Python client
client = language.Client()


# a function which takes a block of text and returns its sentiment and magnitude
def detect_sentiment(text):
    """Detects sentiment in the text."""

    # Instantiates a plain text document.
    document = client.document_from_text(text)

    sentiment = document.analyze_sentiment().sentiment

    return sentiment.score, sentiment.magnitude


# keep track of count of total comments and comments with each sentiment
count = 0
positive_count = 0
neutral_count = 0
negative_count = 0

# read our comments.txt file
with open('comments.txt', encoding='utf-8') as f:
    for line in f:
        # use a try-except block since we occasionally get language not supported errors
        try:
            score, mag = detect_sentiment(line)
        except exceptions.BadRequest:
            # skip the comment if we get an error
            continue

        # increment the total count
        count += 1

        # depending on whether the sentiment is positve, negative or neutral, increment the corresponding count
        if score > 0:
            positive_count += 1
        elif score < 0:
            negative_count += 1
        else:
            neutral_count += 1

        # calculate the proportion of comments with each sentiment
        positive_proportion = positive_count / count
        neutral_proportion = neutral_count / count
        negative_proportion = negative_count / count

        print(
            'Count: {}, Positive: {:.3f}, Neutral: {:.3f}, Negative: {:.3f}'.format(
                count, positive_proportion, neutral_proportion, negative_proportion))

print('')
print('Total comments analysed: {}'.format(count))
print('Positive : {} ({:.2%})'.format(positive_count, positive_count / count))
print('Negative : {} ({:.2%})'.format(negative_count, negative_count / count))
print('Neutral  : {} ({:.2%})'.format(neutral_count, neutral_count / count))

以上是关于python 关于LHL Facebook帖子评论的情绪分析,请访问https://www.facebook.com/leehsienloong/posts/1505690826160285的主要内容,如果未能解决你的问题,请参考以下文章

获取公开 Facebook 帖子的所有评论

系统集成Facebook授权发布帖子以及获取帖子评论等功能

Mysql - 正确排序 Facebook 帖子、评论和回复

Facebook的数据库结构来存储评论和帖子? [关闭]

禁用对通过 Facebook API 发表的特定帖子的评论或点赞

移动应用帖子的 Facebook 洞察中的空故事点击、喜欢和评论