基於Python的情感分析案例

2021-09-14 01:44:44 字數 1601 閱讀 8360

情感極性分析的目的是對文字進行褒義、貶義、中性的判斷。在大多應用場景下,只分為兩類。例如對於「喜愛」和「厭惡」這兩個詞,就屬於不同的情感傾向。

示例1(好評) 

示例2(差評) 

讀取文字檔案

def text():

f1 = open('e:/工作檔案/情感分析案例1/good.txt','r',encoding='utf-8')

f2 = open('e:/工作檔案/情感分析案例1/bad.txt','r',encoding='utf-8')

line1 = f1.readline()

line2 = f2.readline()

str = ''

while line1:

str += line1

line1 = f1.readline()

while line2:

str += line2

line2 = f2.readline()

f1.close()

f2.close()

return str

把單個詞作為特徵

把雙個詞作為特徵,並使用卡方統計的方法,選擇排名前1000的雙詞

def  bigram(words,score_fn=bigramassocmeasures.chi_sq,n=1000):

bigram_finder=bigramcollocationfinder.from_words(words) #把文字變成雙詞搭配的形式

bigrams = bigram_finder.nbest(score_fn,n) #使用卡方統計的方法,選擇排名前1000的雙詞

newbigrams = [u+v for (u,v) in bigrams]

return bag_of_words(newbigrams)

print(bigram(text(),score_fn=bigramassocmeasures.chi_sq,n=1000))

基於Python3的情感分析案例

import scrapy import json import codecs import re class commentspider scrapy.spider name comment start urls def parse self,response filename firstpage...

Python 情感分析

今天修改了情感分析的程式發現之前有一些不足。這個最簡單的實現乙個string情感分析的小函式,載入了積極詞典,消極詞典,程度詞典,以及一些反轉詞等的詞典。這裡我沒有做符號的分析和判斷,因為的東西暫時用不到,需要的童鞋可以自己新增。import jieba import cpickle as pick...

python 情感分析

使用庫 用python 進行機器學習及情感分析,需要用到兩個主要的程式包 nltk 和 scikit learn nltk 主要負責處理特徵提取 雙詞或多詞搭配需要使用nltk 來做 和特徵選擇 需要nltk 提供的統計方法 scikit learn 主要負責分類演算法,評價分類效果,進行分類等任務...