【飛槳深度學習 && 百度七日打卡 && 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

♥ 喜 歡 請 點 贊 喲 ♥
(●ˇ∀ˇ●)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章