【飞桨深度学习 && 百度七日打卡 && Python小白逆袭大神】Day5:综合大作业

在这里插入图片描述

任务:

1、爱奇艺《青春有你2》评论数据爬取:

  • 评论条数不少于1000条
  • 爬取任意一期正片视频下评论

2、词频统计并可视化展示:

  • 数据预处理:清理清洗评论中特殊字符(如:@#¥%、emoji表情符),清洗后结果存储为txt文档
  • 中文分词:添加新增词(如:青你、奥利给、冲鸭),去除停用词(如:哦、因此、不然、也好、但是)
  • 统计top10高频词
  • 可视化展示高频词

3、绘制词云:

  • 根据词频生成词云
  • 可选项-添加背景图片,根据背景图片轮廓生成词云

4、结合PaddleHub,对评论进行内容审核

配置和准备工作:

1、中文分词需要jieba
2、词云绘制需要wordcloud
3、可视化展示中需要的中文字体
4、网上公开资源中找一个中文停用词表
5、根据分词结果自己制作新增词表
6、准备一张词云背景图(附加项,不做要求,可用hub抠图实现)
7、paddlehub配置

GO:

!pip install jieba
!pip install wordcloud
# Linux系统默认字体文件路径
!ls /usr/share/fonts/
# 查看系统可用的ttf格式中文字体
!fc-list :lang=zh | grep ".ttf"
# !wget https://mydueros.cdn.bcebos.com/font/simhei.ttf # 下载中文字体
# 所需字体可以官网下载然后上传
#创建字体目录fonts
# !mkdir .fonts
# 复制字体文件到该路径
!cp simhei.ttf .fonts/
#复制字体到当前使用的conda环境中的matplotlib下的指定路径
!cp simhei.ttf /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf/
#安装模型
!hub install porn_detection_lstm==1.1.0
!pip install --upgrade paddlehub
from __future__ import print_function
import requests
import json
import re #正则匹配
import time #时间处理模块
import jieba #中文分词
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager
from PIL import Image
from wordcloud import WordCloud  #绘制词云模块
import paddlehub as hub
import collections
#请求爱奇艺评论接口,返回response信息
def getMovieinfo(last_id):
    '''
    请求爱奇艺评论接口,返回response信息
    参数  url: 评论的url
    :return: response信息
    '''
    url = r"https://sns-comment.iqiyi.com/v3/comment/get_comments.action?agent_type=118&agent_version=9.11.5&authcookie=null&business_type=17&content_id=15068699100&hot_size=0&last_id=241062754621&page=&page_size=40"
    headers = { 
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
    }
    try:
        response = requests.get(url,headers=headers)
        # print(response.status_code)
        if response.status_code == 200:
        # 状态码为200时表示成功, 服务器已成功处理了请求
            json_text = json.loads(response.text)
            return json_text
        # print(json_text)
    except Exception as e:
        print(e)
    return None

#解析json数据,获取评论
def saveMovieInfoToFile(json_text):
    '''
    解析json数据,获取评论
    参数  lastId:最后一条评论ID  arr:存放文本的list
    :return: 新的lastId
    '''
    arr = []
    for i in range(40):
    # json_text.get('data').get('comments')得到的结果是列表
    # 由于page_size的值为40,因此需要循环40次
        comment = json_text.get('data').get('comments')[i].get('content')
        arr.append(comment)
    # lastId 的获取
    lastId = json_text.get('data').get('comments')[39].get('id')
    # print('comment获取成功,lastId:%s' % lastId)
    return arr,lastId  
#去除文本中特殊字符
def clear_special_char(content):
    '''
    正则处理特殊字符
    参数 content:原文本
    return: 清除后的文本
    '''
    s = r'[^\u4e00-\u9fa5a-zA-Z0-9]'
    # 用空格替换文本中特殊字符
    content= re.sub(s,'',content)
    return content
def fenci(text):
    '''
    利用jieba进行分词
    参数 text:需要分词的句子或文本
    return:分词结果
    '''
    words = [i for i in jieba.lcut(text)]
    return words
def stopwordslist(file_path):
    '''
    创建停用词表
    参数 file_path:停用词文本路径
    return:停用词list
    '''
    with open(file_path, encoding='UTF-8') as words:
        stopwords = [i.strip() for i in words.readlines()]
    return stopwords
def movestopwords(file_path):
    '''
    去除停用词,统计词频
    参数 file_path:停用词文本路径 stopwords:停用词list counts: 词频统计结果
    return:None
    '''
    clean_word_list = []
    # 使用set集合可以更快的查找某元素是否在这个集合中
    stopwords = set(stopwordslist(file_path))
    # 遍历获取到的分词结果,去除停用词
    for word in all_words:
        if word not in stopwords and len(word) > 1:
            clean_word_list.append(word)
    # 由于没有返回值的限制,所以此处现在main()中定义counts变量,再使用全局变量counts,此句是对counts为全局变量的声明
    global counts
    # collections.Counter(clean_word_list)就是前边多导入的一个package,返回的值是一个有序字典,并且带有词频
    counts = collections.Counter(clean_word_list)
    return None

def drawcounts(counts,topN):
    '''
    绘制词频统计表
    参数 counts: 词频统计结果 num:绘制topN
    return:none
    '''
    # counts.most_common(topN)返回的是一个列表,并且每个元素是一个元组,元组中的第一个元素是词,第二个元素是词频
    word_counts_topN = counts.most_common(topN) # 获取前topN最高频的词
    word_counts = []
    labels = []
    # 对列表进行遍历,获取词频word_counts 和该词的labels 
    for ele in word_counts_topN:
        labels.append(ele[0])
        word_counts.append(ele[1])
    plt.rcParams['font.sans-serif'] = ['SimHei'] # 指定默认字体

    plt.figure(figsize=(12,9))
    plt.bar(range(topN), word_counts,color='r',tick_label=labels,facecolor='#9999ff',edgecolor='white')

    # 这里是调节横坐标的倾斜度,rotation是度数,以及设置刻度字体大小
    plt.xticks(rotation=45,fontsize=20)
    plt.yticks(fontsize=20)

    plt.legend()
    plt.title('''前%d词频统计结果''' % topN,fontsize = 24)
    plt.savefig('/home/aistudio/work/result/bar.jpg')
    plt.show()
    return
import sys
import os
import paddlehub as hub
def drawcloud(word_f):
# 加载模型
    humanseg = hub.Module(name = "deeplabv3p_xception65_humanseg")

 
# 抠图
    results = humanseg.segmentation(data = {"image":['/home/aistudio/work/result/bg_horse.png']})
 
    for result in results:
        print(result['origin'])
        print(result['processed'])

    '''
    根据词频绘制词云图
    参数 word_f:统计出的词频结果
    return:none
    '''
    mask = np.array(Image.open('humanseg_output/bgbg.png')) # 定义词频背景
    wc = WordCloud(
        background_color='white', # 设置背景颜色
        font_path='/home/aistudio/simhei.ttf', # 设置字体格式
        mask=mask, # 设置背景图
        max_words=120, # 最多显示词数
        max_font_size=100 , # 字体最大值  
        min_font_size  = 10,   
        width = 400,
        height= 600,
        scale=2  # 调整图片清晰度,值越大越清楚
    )
    wc.generate_from_frequencies(word_f) # 从字典生成词云
    wc.to_file('/home/aistudio/bgbg.png') # 将图片输出为文件
# 导入模型
porn_detection_lstm = hub.Module(name="porn_detection_lstm")
def text_detection():
    '''
    使用hub对评论进行内容分析
    return:分析结果

    '''
    test_text = []
    # 配置hub模型
    porn_detection_lstm = hub.Module(name='porn_detection_lstm')
    # 读取评论,并存入test_text 下
    with open("./dataset/comments.txt", "r") as f:
        for line in f:
            if len(line) <= 1:
                continue
            else:
                test_text.append(line)

    input_dict = {'text':test_text}
    results = porn_detection_lstm.detection(data=input_dict,use_gpu=True,batch_size=1)
    for index,item in enumerate(results):
        if item['porn_detection_key'] == 'porn':
            print(item['text'], ':', item['porn_probs'])
#评论是多分页的,得多次请求爱奇艺的评论接口才能获取多页评论,有些评论含有表情、特殊字符之类的
#num 是页数,一页10条评论,假如爬取1000条评论,设置num=100
if __name__ == "__main__":
    text_list = []
    # 起始url
    url = r"https://sns-comment.iqiyi.com/v3/comment/get_comments.action?agent_type=118&agent_version=9.11.5&authcookie=null&business_type=17&content_id=15068699100&hot_size=0&last_id=241062754621&page=&page_size=40"
    # 停用词路径
    file_path = r'./dataset/mystopwords.txt'
    # 停用词list
    topN = 10
    counts = None
    stopwords = stopwordslist(file_path)
    # 评论获取
    for i in range(30):
        json_text = getMovieinfo(url)
        arr,lastId = saveMovieInfoToFile(json_text)
        text_list.extend(arr)
        time.sleep(0.5)
        # print('lastId:%s,评论抓取成功' %lastId)
        # 去除特殊字符
        for text in arr:
            # 去除文本中特殊字符
            if text and len(text) > 2:
                content = clear_special_char(text)
                text_list.append(content)
        # print('数据获取成功')
        url = r"https://sns-comment.iqiyi.com/v3/comment/get_comments.action?agent_type=118&agent_version=9.11.5&authcookie=null&business_type=17&content_id=15068699100&hot_size=0&last_id=" +lastId+ "&page=&page_size=40"
    with open("./dataset/comments.txt", "w") as f:
    # 评论写入txt文档
        for line in text_list:
            if line != None:
                f.writelines(line)
                # print(line)
                f.writelines("\n")
        print('*' * 50)
        print('写入完成')
    print('共爬取评论:%d' %len(text_list))
    # 评论分词
    all_words = []
    for text in text_list:
        if text:
            all_words.extend(fenci(text))
    # 分词结果 去除停用词
    movestopwords(file_path)
    # 绘制词频展示图
    drawcounts(counts,topN)
    # 绘制词云
    drawcloud(counts)
    display(Image.open('bgbg.png')) #显示生成的词云图像
    text_detection()#显示评论及其概率

效果展示:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

出错分析:

选择背景图片的时候最好不要选择黑色的图片,另外图片格式最好是 .png

♥ 喜 欢 请 点 赞 哟 ♥
(●ˇ∀ˇ●)

猜你喜欢

转载自blog.csdn.net/WU2629409421perfect/article/details/105822113