情感词生成

        我们知道,在情感挖掘中,主要有情感分类(Sentiment Classification)和情感抽取(Opinion Extraction)。对于文档情感分类,一般是构造特征向量来进行分类或是聚类。也有通过计算文档中词的情感分数来获取文档的情感极性,然而这种方法用的不多,效果也不佳。但是对于句子级别的情感挖掘,由于特征少,情感分类效果没有那么理想,而句子的情感往往是由句子中的几个情感词决定。因而获取情感 词在情感挖掘中很有用。而构造情感词典(sentiment lexicon)往往也是许多无监督学习方法中的首要一步。

 

       通常从情感角度来说,一个词可以分为三类:褒义词、中性词、贬义词。然而,由于语境的不同,领域的不同,同样一个词,其情感极性可能就不同。例如:  (http://zhidao.baidu.com/question/159946153)           

          “灯红酒绿”有两个意思,它既可形容寻欢作乐的腐化生活,是贬义词;也可形容都市或娱乐场所的繁华景象,是褒义词

           有时甚至在同一领域由于语境同一词的情感极性也不同。例如照相机的评论:

          电池寿命很长。(正)

          需要很长时间聚焦。(负)

 

        以下介绍几种判断词情感的早期的比较经典的方法,主要有基于词典的方法和基于语料的方法。后发现刘兵编的《web数据挖掘》中文版320页的倒数第二段有总结的更典型的参考文献。




利用词典:

1.极词性扩展(近义词、反义词)

(Hu & Liu, 2004)形容词:对评论进行POS标记,利用关联挖掘获取频繁产品特征(如颜色、质量等),在含有特征的句子中抽取形容词作为情感词。根据同义词情感极性相同,反义词情感极性相反,给定几个有极性的种子形容词如美丽等,利用wordnet不断查找它们的同义词和反义词以扩大情感词词典直到其数量不再变化为止。

优点:简单。缺点:只考虑形容词,只针对产品评论,词的极性无法与语境、领域关联。

(Ding, Liu, & Yu,2008)利用wordnet同样方法生成动词、名词、副词。此外还人工标记成语(idioms)。对于难以确定情感的词,利用语境来判断。如“照相机照相很好并且(and)电池寿命长”,长和好有相同的情感极性。but则相反。可以在同一句中判断,也可以根据上下句来判断。还有一些词如太(too)有否定效果,如“这相机太小了”。根据语境获取的词极性再用到同义词和反义词规则中得到其他词的极性。

优点:情感词找的比较全。缺点:需要考虑语义并加入很多规则,增加判别复杂度。

Hu, M., Liu, B.: Mining and summarizing customerreviews.  Proceedings of the tenth ACM SIGKDD international conference onKnowledge discovery and data mining, pp. 168-177. ACM, Seattle, WA, USA (2004)

Ding, X., Liu, B., et al.: A holistic lexicon-basedapproach to opinion mining. In: Proceedings of the international conference onWeb search and web data mining (WSDM '08), pp. 231-240, ACM, Palo Alto,California, USA (2008)


2.情感词典——字的情感——词的情感

(Ku et al. 2006,2007) 情感词典:英文情感词典(General Inquirer)的翻译、中文词典(Chinese NetworkSentiment Dictionary)<pos, neg>=<2764,7778>。从情感词词典中得到字的情感分数,再计算词的情感分数,从而得到情感词。假定已经有中文情感词词典,词典中的每个字在词典中组成的正情感词频率与负情感词频率,正频率与总频率比减去负频率与总频率比为该字的情感分数。然后对于每个词,则其情感分数为字的情感分数平均值。选取某个值来获得情感词。

优点:能发现未知的情感词,缺点:需要情感词词典,词极性固定无法与语境、领域关联。

Ku, L.W., Liang, Y.T., et al.: Opinion extraction,summarization and tracking in news and blog corpora. In: Proceedings ofAAAI-2006 Spring Symposium on Computational Approaches to Analyzing Weblogs,pp. 100-107 (2006)

Ku, L.W., Lo, Y.S., et al.: Using polarity scores ofwords for sentence-level opinion extraction. In: Proceedings of NTCIR-6Workshop Meeting, pp. 316-322, Citeseer, Tokyo, Japan (2007)


利用语料:

n  基于语料的方法主要是利用词之间的共现模式来确定他们的情感。(Turney, 2002)

1.  抽取形容词和副词的短语。

2.  采用点对互信息(MI)估计所抽取短语的语义倾向。PMI(t1,t2)= log2(p(t1*t2)/(p(t1)*p(t2))) 两个词同时出现与他们单独出现的比的对数值。

3.  给定一个评审,算法计算评审中所有短语的评价语义倾向(SO)。SO(t)=PMI(t,"excellent")-PMI(t,"poor") 利用搜索引擎搜索来估计。

Turney, P.D.: Thumbs up or thumbs down?: semanticorientation applied to unsupervised classification of reviews. Proceedings of the 40th Annual Meeting on Association for ComputationalLinguistics, pp. 417-424. Association for Computational Linguistics,Philadelphia, Pennsylvania (2002)

n  形容词的共现及与名词的修饰关系(词性、依赖关系)

1.  针对产品评论的情感词扩展方法,利用词性标注及句法依赖关系发现词与词间的关系再扩展。例如一个与形容词为并列关系的形容词也可能是情感词,修饰产品属性的形容词也会是情感词。根据情感对象和情感词的不断迭代来找全形容词情感词。

2.  关于极性则根据一些观察结果的启发式规则来确定,如并列的词有共同的极性,一对象的已发现的极性来确定修饰它的形容词的极性,同一词在一领域极性统一。

3.  问题:领域方法,针对的只是形容词。

G. Qiu, B. Liu, J. Bu, and C. Chen, "Opinion WordExpansion and Target Extraction through Double Propagation,"ComputationalLinguistics, vol. 37, pp. 9-27, 2011.


附开源的中文情感词典版本:

1.        台湾交通大学NTU简体版

下载地址:http://www.datatang.com/data/11837

2.        HowNet版

下载地址:http://www.keenage.com/html/c_index.html

http://www.datatang.com/data/11851



文章来源:twenz for higher

好的,下面是一个简单的情感生成和朴素贝叶斯分类的Python代码示例: ```python import os import re from collections import Counter import math # 情感典文件路径 SENTIMENT_DICT_FILE = 'sentiment_dict.txt' # 训练集文件路径 TRAIN_DATA_FILE = 'train_data.txt' # 测试集文件路径 TEST_DATA_FILE = 'test_data.txt' # 情感生成函数 def generate_sentiment_dict(train_data_file, output_file): sentiment_words = Counter() with open(train_data_file, 'r', encoding='utf-8') as f: for line in f: line = line.strip() if not line: continue words, label = line.split('\t') if label == '1': sentiment_words.update(words.split(' ')) with open(output_file, 'w', encoding='utf-8') as f: for word, freq in sentiment_words.items(): f.write('{}\t{}\n'.format(word, freq)) # 朴素贝叶斯分类器 class NaiveBayesClassifier: def __init__(self): self.total_docs = 0 # 总文档数 self.class_docs = {} # 每个类别的文档数 self.word_freq = {} # 每个在每个类别中的出现次数 self.classes = set() # 类别集合 self.vocab = set() # 汇表 # 训练函数 def train(self, train_data_file): with open(train_data_file, 'r', encoding='utf-8') as f: for line in f: line = line.strip() if not line: continue words, label = line.split('\t') self.total_docs += 1 self.class_docs[label] = self.class_docs.get(label, 0) + 1 for word in words.split(' '): self.word_freq[label] = self.word_freq.get(label, Counter()) self.word_freq[label][word] += 1 self.vocab.add(word) self.classes.add(label) # 预测函数 def predict(self, text): words = re.findall(r'\w+', text) scores = {c: math.log(self.class_docs[c] / self.total_docs) for c in self.classes} for word in words: if word not in self.vocab: continue for c in self.classes: freq = self.word_freq[c].get(word, 0) scores[c] += math.log((freq + 1) / (sum(self.word_freq[c].values()) + len(self.vocab))) return max(scores, key=scores.get) # 生成情感典 generate_sentiment_dict(TRAIN_DATA_FILE, SENTIMENT_DICT_FILE) # 加载情感sentiment_dict = set() with open(SENTIMENT_DICT_FILE, 'r', encoding='utf-8') as f: for line in f: word, freq = line.strip().split('\t') if int(freq) > 10: # 过滤掉出现次数过少的 sentiment_dict.add(word) # 训练朴素贝叶斯分类器 classifier = NaiveBayesClassifier() classifier.train(TRAIN_DATA_FILE) # 测试朴素贝叶斯分类器 with open(TEST_DATA_FILE, 'r', encoding='utf-8') as f: num_correct = 0 num_total = 0 for line in f: line = line.strip() if not line: continue text, label = line.split('\t') pred_label = classifier.predict(text) num_correct += int(pred_label == label) num_total += 1 accuracy = num_correct / num_total print('Accuracy: {:.2%}'.format(accuracy)) ``` 上面的代码实现了以下功能: 1. 从训练集文件中生成情感典文件。 2. 加载情感典和训练集文件,训练朴素贝叶斯分类器。 3. 加载测试集文件,测试朴素贝叶斯分类器的准确率。 请注意,这只是一个简单的示例代码,实际上情感分析需要更多的预处理和特征提取步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值