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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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

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

  • 數據來源:香港金融新聞平臺
  • 處理工具:python3.5
  • 處理結果:分析語言的積極/消極意義
  • 領域:金融/炒股

請隨意觀看表演

  • 數據準備
  • 數據清洗
  • 情感分析
  • 報錯處理
  • 成果展示
  • 遺留問題

No1.數據準備

準備工作主要是對字典進行處理,將其按照類型分類寫入python文件中,方便其余腳本調用。并且,將詞典寫入到emotion_word.txt中,使用 jieba詞庫 重載

將字典寫入.py文件好處
  • 方便調用:from emotion_word import *
  • 按照類型分類,調用后,直接使用most_degree即可,避免打開txt文件的大量代碼
  • 可以使用python高級結構的方法
  • 附一張emotion_word.py的截圖
  • 寫入方法

    將txt字典中的每行的詞語讀出來,再寫入列表,再print(List)。當數據少的時候可以,但是當數據達到幾百以上,顯然不可行。
    若txt字典中的詞語都是按行分布的:

    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()

    寫入后,再全選復制,粘貼到對應.py文件就可以了
    附截圖

    No2.數據清洗

    拿到的數據是這樣的,附截圖

    主要就是:繁體去簡體,去掉html標簽和各種奇葩符號
    繁體和簡體的轉化,用到了國人的一個庫,請戳這里下載 :)

    使用方法很簡單:

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

    代碼會在之后用類一起封裝

    No3.情感分析

    分析title(新聞標題)和content(新聞主體)的成績(只看正負)和方差。對于成績,我們更重視新聞標題,因為關鍵詞明確,數量少,影響因素少;對于方差,我們更看重新聞主體,詞語多,從方差可以看出來這段新聞語氣程度(肯定/不確定...)。當然,當titile成績為0或者主體方差為0,我們會看主體的成績和title的方差。

  • 當前詞的正負性(褒義/貶義)
  • 檢索前一個詞是否是程度詞/反義詞
  • 后一個詞/標點是否能加深程度
  • 字典特征

    • 字典里面的否定詞:'不好',而不是'不','好'。所以否定詞是和別的詞連在一起的。但也有少數不是。
    • 字典包含標點符號
    • 字典有一些缺陷,并且不是針對金融領域的專門字典
    class EmotionAnalysis:def __init__(self,news=None):self.news = newsself.list = []def __repr__(self):return "News:"+self.news#新聞去標簽,繁->簡def delete_label(self):rule = r'(<.*?>)| |\t|\n|○|■|☉'self.news = re.sub(rule,'',self.news)self.news = cht_to_chs(self.news)#得到成績和方差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)#返回(成績,方差)return (score,np.var(mean_list))

    No4.報錯處理

    一共231506條新聞,為了方便回查,設置報錯處理(在數據庫操作的類里實現)

    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太小,所以擴大了1w倍,便于觀察相對大小和后期工作的進行。請觀察id,來觀看結果(為了方便顯示,導入到了兩個csv文件)

    No6.遺留問題

    • 在EmotionAnalysis類里的get_score函數里,對應的分值容易確定。(有空看一下機器學習,maybe能改進)。所以現在的分數只能看正負,來確定消極或積極。但對于這種金融新聞(特點:言簡意賅),效果還可以。
    • 字典問題,請看 No3里面的字典特征

    轉載于:https://www.cnblogs.com/AsuraDong/p/emotion_analysis.html

    總結

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

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。