python 利用百度API進(jìn)行淘寶評論關(guān)鍵詞提取
利用百度API自然語言處理技術(shù)中的評論觀點(diǎn)抽取方面,對淘寶購物的評論進(jìn)行分析,把關(guān)鍵詞進(jìn)行提取,方便買家快速了解該商品的相關(guān)特點(diǎn),具體實(shí)現(xiàn)過程如下:
1、創(chuàng)建相關(guān)新應(yīng)用首先,需要登錄百度AI平臺,創(chuàng)建一個(gè)關(guān)于自然語言處理技術(shù)的應(yīng)用(領(lǐng)取免費(fèi)額度),獲得AppID、API Key和Secret Key如下:
然后下載Python的SDK,然后可以通過閱讀API的使用手冊和自然語言處理技術(shù)的觀點(diǎn)抽取相關(guān)案例:
使用百度API時(shí)需要先獲取Access Token,并且需要用到上一步獲取的參數(shù)API Key和Secret Key,在百度給出的使用說明中,建議用POST的方式對URL進(jìn)行請求來獲取access_token,同時(shí)可以加入Header,該方式支持UTF-8編碼,具體實(shí)現(xiàn)代碼如下:
import jsonimport timeimport requestsfrom urllib.request import urlopen#定義獲取token函數(shù)def get_token(): req = Request(Token_url) req.add_header(’Content-Type’, ’application/json; charset=UTF-8’) try: f = urlopen(req,timeout=5) result_str = f.read().decode(’utf-8’) except URLError as err: print(err) result = json.loads(result_str) #返回Access Token字符串 return result[’access_token’]3、分析評論并進(jìn)行觀點(diǎn)抽取
在獲取Access Token后就可以使用百度API對評論進(jìn)行分析并抽取關(guān)鍵詞。在使用說明中,調(diào)用API時(shí)需要輸入?yún)?shù)text和type,其中text為需要分析的文本,而type分成了13各類別,具體取值說明如下:
因?yàn)楸疚姆治龅氖翘詫氋徫镌u論文本數(shù)據(jù),所以該參數(shù)取值選擇了12。其次,返回格式需要指定輸入為UTF-8編碼,對于調(diào)用后返回的參數(shù),本文用到了prop、adj和sentiment三個(gè)參數(shù),解釋如下:
具體實(shí)現(xiàn)代碼如下:
def analysis_comment(host,comment): #定義分析類別(購物) data = json.dumps( { 'text':comment, 'type':12 }) request = Request(url=host,data=data.encode(’utf-8’)) request.add_header(’Content-Type’, ’application/json; charset=UTF-8’) response = urlopen(request) content = response.read().decode(’utf-8’) rdata = json.loads(content) print('--------------------------------------------------------------') print('評論:') print(' ' + comment) print('n評論關(guān)鍵字:') #把積極、中性、消極關(guān)鍵詞分類出來并打印 for item in rdata[’items’]: if item[’sentiment’] == 2: print(u' 積極的評論關(guān)鍵詞:' + item[’prop’] + item[’adj’]) if item[’sentiment’] == 1: print(u' 中性的評論關(guān)鍵詞:' + item[’prop’] + item[’adj’]) if item[’sentiment’] == 0: print(u' 消極的評論關(guān)鍵詞:' + item[’prop’] + item[’adj’])4、運(yùn)行結(jié)果
在對上述函數(shù)進(jìn)行定義后,運(yùn)行改代碼,調(diào)用函數(shù):
if __name__ == ’__main__’: #定義訪問url(API Key和Secret Key換成自己的) Comment_url = 'https://aip.baidubce.com/rpc/2.0/nlp/v2/comment_tag' Token_url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=your_API_Key&client_secret=your_Secret_Key' A_t = get_token() host = Comment_url + '?charset=UTF-8&access_token='+A_t comment1 = {'text':'版型不錯(cuò),顏色很好看,面料非常舒服而且厚度適中'} comment2 = {'text':'上身效果一般,做工也一般,會有點(diǎn)起球,沒有想象中好'} comment3 = {'text':'設(shè)計(jì)做工一點(diǎn)都不好,袖子特別長,衣服比例設(shè)計(jì)非常差,性價(jià)比不高'} comment1 = comment1['text'] comment2 = comment2['text'] comment3 = comment3['text'] analysis_comment(host,comment1) analysis_comment(host,comment2) analysis_comment(host,comment3)
運(yùn)行結(jié)果如下:
然而,從結(jié)果中可以看出,在第二句評論中,“一般”這個(gè)詞應(yīng)該定義為中性,而該模型將其定義為消極,說明該模型在一定程度上仍存在一些瑕疵,這也是后期需要改進(jìn)的地方。
以上就是python 利用百度API進(jìn)行淘寶評論關(guān)鍵詞提取的詳細(xì)內(nèi)容,更多關(guān)于python 淘寶評論關(guān)鍵詞提取的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 使用.net core 自帶DI框架實(shí)現(xiàn)延遲加載功能2. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究3. Angular獲取ngIf渲染的Dom元素示例4. php面向?qū)ο蟪绦蛟O(shè)計(jì)介紹5. ASP調(diào)用WebService轉(zhuǎn)化成JSON數(shù)據(jù),附j(luò)son.min.asp6. 無線標(biāo)記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁7. 三個(gè)不常見的 HTML5 實(shí)用新特性簡介8. php測試程序運(yùn)行速度和頁面執(zhí)行速度的代碼9. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報(bào)錯(cuò)問題分析10. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執(zhí)行過程解析
