[python]飞桨python小白逆袭课程day5——大作业来啦

 

第一步:爱奇艺《青春有你2》评论数据爬取(参考链接:https://www.iqiyi.com/v_19ryfkiv8w.html#curid=15068699100_9f9bab7e0d1e30c494622af777f4ba39)

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

第二步:词频统计并可视化展示

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

第三步:绘制词云

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

第四步:结合PaddleHub,对评论进行内容审核

 

需要的配置和准备

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

 

下面就是具体的实现过程啦

 

!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/

#设置当前字体为默认字体
!cp simhei.ttf /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf/

!fc-list :lang=zh | grep ".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

collections 模块是Python标准库,是数据结构常用模块

常用类型有:

  计数器(Counter)

  双向队列(deque)

  默认字典(defaultdict)

  有序字典(OrderedDict)

  可命名元组(namedtuple)

 

这一步是爬取评论

#请求爱奇艺评论接口,返回response信息
def getMovieinfo(url):
    '''
    请求爱奇艺评论接口,返回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(content):
    '''
    利用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('bar_result.jpg')
    plt.show()
    return


下面是加载抠图功能,利用一个图片最好是色差较大背景,具体的效果试过就知道了,输出是一个human_seg目录下的抠图图片

import sys
import os
import paddlehub as hub
 
# 加载模型
humanseg = hub.Module(name = "deeplabv3p_xception65_humanseg")
 
# 抠图
results = humanseg.segmentation(data = {"image":['bgbg.png']})
 
for result in results:
    print(result['origin'])
    print(result['processed'])

 

然后绘制词云图

def drawcloud(word_f):
    '''
    根据词频绘制词云图
    参数 word_f:统计出的词频结果
    return:none
    '''
    mask = np.array(Image.open('/home/aistudio/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/pic3.png') # 将图片输出为文件
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'./work/stopwords.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)
    # print("----")
    display(Image.open('pic3.png')) #显示生成的词云图像
    # text_detection()#显示色情评论及其概率

最后实现词频分析柱状图和词云图,收获很多,感谢飞桨深度学习学院,虽然时间很短,但的确在这很短时间内扩充了我们的视野,也让我从一开始停留在理论,转到动手实现深度学习的实验当中,虽然这些案例并不是很难,但每一步都需要认真明白,才能最终运行成功。总之,这次收获很多,再次感谢百度飞桨深度学习学院!我会利用更多时间去进一步消化课上的很多内容,加油!

python大作业 一、Turtle创意大PK 自拟题目,完成一个利用Python程序的创意绘图,采用turtle库绘图为主,不少于50行代码,可选采用其他库。 (滑稽绘制) 二、程序练习 2.1 问题描述(10分) 人们常常提到"一万小时定律",就是不管你做什么事情,只要坚持一万小时,应该都可以成为该领域的专家。那么,10000小时是多少年多少天呢? 2.2 问题描述(10分)0380031003800341590145037657 编写计算从n到m和的函数‬,函数名为sum(n,m)‬,函数返回值为n到m所有数据的和‬,使用该函数计算输入数据x,y之间所有数据的和。 2.3 问题描述(15分) 编写函数judgeTri(a,b,c),判断以参数a,b,c的值为边长能否构成三角形并判断三角形的形状;若是锐角三角形,返回R;若是直角三角形,返回Z;若是钝角三角形,返回D;若三边长不能构成三角形,返回ERROR。 2.4 问题描述(15分) 用户输入一个字符串,分别统计其中小写字母、大写字母、数字、空格和其他字符的个数,并在一行内输出小写字母、大写字母、数字、空格和其他字符的个数。 2.5 问题描述(20分) 程序的功能: (1) 使用随机库功能,生成一个包含10个不重复且小于200的正整数列表ls1,输出ls1。‬ (2) 使用列表排序方法,对ls1按奇数在前偶数在后,并且奇数之间的相对顺序不变,偶数之间的相对顺序也不变进行排序,再输出ls1。‬ (3) 使用列表排序方法,对ls1按元素字符长度降序进行排序,输出ls1。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值