通過百度云api接口抽取得到產品評論的觀點,也掠去了很多評論中無用的內容以及符號,為后續進行文本主題挖掘或者規則的提取提供基礎。
工具
1、百度云賬號,申請應用接口(自然語言處理)
2、python3.5
以下是百度接口提供的說明:
我們使用到的可選值是13,kindle屬于3c產品。
下面是代碼示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
from aip import aipnlp import csv import pandas as pd from pandas.core.frame import dataframe """ 你的 appid ak sk """ app_id = '********' api_key = '********' secret_key = '********' client = aipnlp(app_id, api_key, secret_key) # 導入評論數據文件,并找到第13列(12行)的評論內容提取出來 def output(): urls = [] with open ( 'e:\\tb_iphone8.csv' , "r" ) as f: reader = csv.reader(f) for row in reader: urls.append(row[ 12 ]) return urls # 通過百度云提供的api對評論觀點進行提取 def commenttag(): x = output() all = {} abst = '' for i in range ( 10560 ): text = x[i] """ 調用評論觀點抽取 """ """ 如果有可選參數 """ # 可選參數為13表示利用了3c產品的語料庫 options = {} options[ "type" ] = 13 """ 帶參數調用評論觀點抽取 """ result = client.commenttag(text, options) print (result) if "error_code" in result.keys(): abst + = '' all [ 'abstract' ] = abst else : data = result[ 'items' ] items = data[ 0 ] abst + = items[ 'abstract' ] all [ 'abstract' ] = abst return abst if __name__ = = '__main__' : all = commenttag() print ( all ) |
得到的結果如下:
可以看到,現在抽取出來的評論部分內容都是具有一定觀點傾向的,大部分沒有什么含義的評論內容已經被除去,這對后面的分析有一定的幫助。
以上這篇對python借助百度云api對評論進行觀點抽取的方法詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/AnthonyHDM/article/details/78957315