日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

自然语言之情感分析(中文)

發(fā)布時(shí)間:2023/12/10 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自然语言之情感分析(中文) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

自然語(yǔ)言之情感分析(中文)

  • 數(shù)據(jù)來(lái)源:香港金融新聞平臺(tái)
  • 處理工具:python3.5
  • 處理結(jié)果:分析語(yǔ)言的積極/消極意義
  • 領(lǐng)域:金融/炒股

請(qǐng)隨意觀看表演

  • 數(shù)據(jù)準(zhǔn)備
  • 數(shù)據(jù)清洗
  • 情感分析
  • 報(bào)錯(cuò)處理
  • 成果展示
  • 遺留問(wèn)題

No1.數(shù)據(jù)準(zhǔn)備

準(zhǔn)備工作主要是對(duì)字典進(jìn)行處理,將其按照類型分類寫(xiě)入python文件中,方便其余腳本調(diào)用。并且,將詞典寫(xiě)入到emotion_word.txt中,使用 jieba詞庫(kù) 重載

將字典寫(xiě)入.py文件好處
  • 方便調(diào)用:from emotion_word import *
  • 按照類型分類,調(diào)用后,直接使用most_degree即可,避免打開(kāi)txt文件的大量代碼
  • 可以使用python高級(jí)結(jié)構(gòu)的方法
  • 附一張emotion_word.py的截圖
  • 寫(xiě)入方法

    將txt字典中的每行的詞語(yǔ)讀出來(lái),再寫(xiě)入列表,再print(List)。當(dāng)數(shù)據(jù)少的時(shí)候可以,但是當(dāng)數(shù)據(jù)達(dá)到幾百以上,顯然不可行。
    若txt字典中的詞語(yǔ)都是按行分布的:

    word_list = [] def main():with open('emotion_word.txt','r',encoding="utf-8") as f:global word_listfor line in f.readlines():word_list.append(line.strip('\n'))with open('tem.txt','a',encoding="utf-8") as f:writted = 'word_list = '+str(word_list)+'\n'f.write(writted)if __name__=='__main__':main()

    寫(xiě)入后,再全選復(fù)制,粘貼到對(duì)應(yīng).py文件就可以了
    附截圖

    No2.數(shù)據(jù)清洗

    拿到的數(shù)據(jù)是這樣的,附截圖

    主要就是:繁體去簡(jiǎn)體,去掉html標(biāo)簽和各種奇葩符號(hào)
    繁體和簡(jiǎn)體的轉(zhuǎn)化,用到了國(guó)人的一個(gè)庫(kù),請(qǐng)戳這里下載 :)

    使用方法很簡(jiǎn)單:

    from langconv import * #轉(zhuǎn)換繁體到簡(jiǎn)體 def cht_to_chs(line):line = Converter('zh-hans').convert(line)line.encode('utf-8')return line#轉(zhuǎn)換簡(jiǎn)體到繁體 def chs_to_cht(line):line = Converter('zh-hant').convert(line)line.encode('utf-8')return line

    代碼會(huì)在之后用類一起封裝

    No3.情感分析

    分析title(新聞標(biāo)題)和content(新聞主體)的成績(jī)(只看正負(fù))和方差。對(duì)于成績(jī),我們更重視新聞標(biāo)題,因?yàn)殛P(guān)鍵詞明確,數(shù)量少,影響因素少;對(duì)于方差,我們更看重新聞主體,詞語(yǔ)多,從方差可以看出來(lái)這段新聞?wù)Z氣程度(肯定/不確定...)。當(dāng)然,當(dāng)titile成績(jī)?yōu)?或者主體方差為0,我們會(huì)看主體的成績(jī)和title的方差。

  • 當(dāng)前詞的正負(fù)性(褒義/貶義)
  • 檢索前一個(gè)詞是否是程度詞/反義詞
  • 后一個(gè)詞/標(biāo)點(diǎn)是否能加深程度
  • 字典特征

    • 字典里面的否定詞:'不好',而不是'不','好'。所以否定詞是和別的詞連在一起的。但也有少數(shù)不是。
    • 字典包含標(biāo)點(diǎn)符號(hào)
    • 字典有一些缺陷,并且不是針對(duì)金融領(lǐng)域的專門字典
    class EmotionAnalysis:def __init__(self,news=None):self.news = newsself.list = []def __repr__(self):return "News:"+self.news#新聞去標(biāo)簽,繁->簡(jiǎn)def delete_label(self):rule = r'(<.*?>)| |\t|\n|○|■|☉'self.news = re.sub(rule,'',self.news)self.news = cht_to_chs(self.news)#得到成績(jī)和方差def get_score(self):self.list = list(jieba.cut(self.news))index_list = zip(range(len(self.list)),self.list)score = 0mean_list = []#tem_list= []for (index,word) in index_list:#tem_list.append(word)tem_score = 0#print("NO:",index,'WORD:',word)if (word in pos_emotion) or (word in pos_envalute):tem_score = 0.1#搜索程度詞if self.list[index-1] in most_degree and (index-1):tem_score = tem_score*3elif self.list[index-1] in very_degree and (index-1):tem_score = tem_score*2.5elif self.list[index-1] in more_degree and (index-1):tem_score = tem_score*2elif self.list[index-1] in ish_degree and (index-1):tem_score = tem_score*1.5elif self.list[index-1] in least_degree and (index-1):tem_score = tem_score*1else:pass#搜索否定詞/反意詞if (self.list[index-1] in neg_degree and index!=0) or (index<len(self.list)-1 and self.list[index+1] in neg_degree):tem_score = -tem_score#print("| tem_score:",tem_score)elif (word in neg_emotion) or (word in neg_envalute):tem_score = -0.3if self.list[index-1] in most_degree and (index-1):tem_score = tem_score*3elif self.list[index-1] in very_degree and (index-1):tem_score = tem_score*2.5elif self.list[index-1] in more_degree and (index-1):tem_score = tem_score*2elif self.list[index-1] in ish_degree and (index-1):tem_score = tem_score*1.5elif self.list[index-1] in least_degree and (index-1):tem_score = tem_score*1else:pass#print("| tem_score:",tem_score)mean_list.append(tem_score)score+=tem_score#print(tem_list)#返回(成績(jī),方差)return (score,np.var(mean_list))

    No4.報(bào)錯(cuò)處理

    一共231506條新聞,為了方便回查,設(shè)置報(bào)錯(cuò)處理(在數(shù)據(jù)庫(kù)操作的類里實(shí)現(xiàn))

    log_file = 'error.log' class SQL(object):......def run(self,cmd,index):try:self.read_SQL(cmd,index)self.operate()self.write_SQL(index)self.w_conn.commit()except Exception as r:self.r_conn.rollback()self.w_conn.rollback()error = "ID "+str(self.r_dict['id'])+str(r)global log_filelog_error(log_file = log_file,error=error)

    No5.成果展示

    由于var太小,所以擴(kuò)大了1w倍,便于觀察相對(duì)大小和后期工作的進(jìn)行。請(qǐng)觀察id,來(lái)觀看結(jié)果(為了方便顯示,導(dǎo)入到了兩個(gè)csv文件)

    No6.遺留問(wèn)題

    • 在EmotionAnalysis類里的get_score函數(shù)里,對(duì)應(yīng)的分值容易確定。(有空看一下機(jī)器學(xué)習(xí),maybe能改進(jìn))。所以現(xiàn)在的分?jǐn)?shù)只能看正負(fù),來(lái)確定消極或積極。但對(duì)于這種金融新聞(特點(diǎn):言簡(jiǎn)意賅),效果還可以。
    • 字典問(wèn)題,請(qǐng)看 No3里面的字典特征

    轉(zhuǎn)載于:https://www.cnblogs.com/AsuraDong/p/emotion_analysis.html

    總結(jié)

    以上是生活随笔為你收集整理的自然语言之情感分析(中文)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。