情感分析:谁在评价哪个方面?
Sentiment analysis
区分评价对象与冲突意见,检查明确的组合规则,保留讽刺与范围判断的失败。
sentiment-analysis菜好、服务差,不是一条中性意见
虚构评价food good but service bad包含针对两个对象的意见。把正向词分数与负向词分数相加可能得到0,却丢掉两项有用结论。情感分析首先需要确定预测单位:全文、句子、明确出现的方面词,还是预先定义的方面类别。
Pang、Lee和Vaithyanathan在2002年研究影评整体情感,比较词表基线与学习式文本分类器;SemEval-2014 Task4则分开方面词抽取、给定方面词的极性、类别检测及给定类别的极性。不能把这些任务混在一个没有说明口径的准确率里。影评整体任务 四种方面任务
极性标签也不是情绪诊断或产品质量的客观测量,它描述标注政策下的一种解释。下方程序采用给定方面词与明确局部规则,让每项贡献和失败都可以检查。
保留对象,也保留相反证据
定义标签含义
对一个方面,使用positive、negative、neutral、conflict四类。SemEval的conflict表示对同一方面同时表达正负意见,其方面词极性任务给定方面词;推断隐含类别是另一项任务。SB1—SB4定义
本文把正负贡献分别累积。意见词的基础值为v_i,紧邻程度词乘以m_i;窗口内识别到奇数个前置否定词时翻转符号:
这些是虚构教学权重与规则,不是校准概率,也不复现某个现成情感库。默认否定窗口至多包含同一小句内前三个词元;标点和but结束小句。紧邻的very乘1.5,slightly乘0.5。
把有分数的词分给同一小句内最近的给定方面提及,距离不超过六个词元;并列时优先较早提及。找不到合适对象时,保留为未分配意见,不凭空推断隐含方面。同一方面的重复提及合并累积。
决策表区分没有方向证据与两个方向都有证据:
因此,food good but food bad的正向强度为2、负向强度为2、净分为0,却是conflict;food arrived没有识别到情感证据,按此基线判neutral。这不证明读者在更丰富语境下也一定看不出意见。
局部规则暴露组合错误
not good会翻转词的贡献;not very good得到2×1.5×(−1)=−3。但not only good通常不是负面评价,同一窗口仍会把它翻成负向。双重否定的奇偶性也只是声明的算术规则,不是人类解释否定的一般理论。
学习式文档分类器可以替换固定词权重或编码更大语境,仍需目标定义、合适标签和留出评测。Pang等人的词袋设定是一个历史上的学习式选择;其第三个探索性词表基线使用过测试数据信息,这种开发尝试不能冒充封存测试成绩。基线与特征方法
运行规则,把反例留下来
完整代码只用Python3.12标准库,方面表由小写单词明确给定。分词保留原文区间及其他非空白字符,但不实现中文分词或多语言模型。
"""Original transparent aspect-polarity rules, with deliberate failure examples."""
import json,re
LEXICON={'good':2.,'great':3.,'excellent':3.,'bad':-2.,'terrible':-3.,'slow':-1.5,'boring':-2.}
NEGATIONS={'not','never','no'};BOOSTERS={'very':1.5,'slightly':.5};LABELS=('positive','negative','neutral','conflict')
def polarity(positive,negative):
if positive and negative:return 'conflict'
if positive:return 'positive'
if negative:return 'negative'
return 'neutral'
def analyze(text,aspects=('food','service','battery','screen'),negation_window=3):
if not isinstance(text,str) or len(text)>10000:raise ValueError('text must have at most10000 characters')
if type(negation_window) is not int or not 0<=negation_window<=5:raise ValueError('negation window must be0..5')
if not aspects or any(not isinstance(a,str) or not re.fullmatch('[a-z]+',a) for a in aspects) or len(set(aspects))!=len(aspects):
raise ValueError('unique lowercase single-word aspect terms required')
tokens=[];clause=0
for match in re.finditer(r"[A-Za-z]+(?:'[A-Za-z]+)?|[^\s]",text):
word=match[0].casefold();tokens.append({'word':word,'span':list(match.span()),'clause':clause})
if word in {'.',',','!','?',';','but'}:clause+=1
mentions=[(i,t['word'],t['clause']) for i,t in enumerate(tokens) if t['word'] in aspects]
totals={a:[0.,0.] for _,a,_ in mentions};evidence=[]
for i,token in enumerate(tokens):
word=token['word']
if word not in LEXICON:continue
previous=[t['word'] for t in tokens[max(0,i-negation_window):i] if t['clause']==token['clause']]
negated=sum(w in NEGATIONS for w in previous)%2==1
modifier=BOOSTERS.get(tokens[i-1]['word'],1.) if i and tokens[i-1]['clause']==token['clause'] else 1.
value=LEXICON[word]*modifier*(-1 if negated else 1)
candidates=[(abs(i-j),0 if j<i else 1,j,a) for j,a,c in mentions if c==token['clause'] and abs(i-j)<=6]
owner=min(candidates)[3] if candidates else None
if owner is not None:totals[owner][0 if value>0 else 1]+=abs(value)
evidence.append({'word':word,'span':token['span'],'base':LEXICON[word],'modifier':modifier,'negated':negated,'value':value,'aspect':owner})
result={a:{'positive':p,'negative':n,'net':p-n,'label':polarity(p,n)} for a,(p,n) in sorted(totals.items())}
return {'tokens':tokens,'aspects':result,'evidence':evidence,'unassigned_opinions':sum(e['aspect'] is None for e in evidence)}
def metrics(gold,predicted):
if len(gold)!=len(predicted) or any(v not in LABELS for v in list(gold)+list(predicted)):raise ValueError('aligned supported labels required')
classes={}
for label in LABELS:
g=gold.count(label);p=predicted.count(label);tp=sum(a==b==label for a,b in zip(gold,predicted))
classes[label]={'tp':tp,'fp':p-tp,'fn':g-tp,'f1':2*tp/(g+p) if g+p else 0.}
return {'accuracy':sum(a==b for a,b in zip(gold,predicted))/len(gold) if gold else 0.,
'macro_f1':sum(c['f1'] for c in classes.values())/len(LABELS),'classes':classes}
def demo():
# Gold intent is explicitly chosen for these invented examples; no corpus claim.
cases=[('food is good','food','positive'),('service is not good','service','negative'),
('battery lasts hours','battery','neutral'),('food good but food bad','food','conflict'),
('service is not only good','service','positive'),('service great another delay','service','negative')]
results=[];raw=[];gold=[];pred=[]
for text,target,label in cases:
full=analyze(text);baseline=analyze(text,negation_window=0);gold.append(label);pred.append(full['aspects'][target]['label']);raw.append(baseline['aspects'][target]['label'])
results.append({'text':text,'target':target,'gold_intent':label,'prediction':pred[-1],'without_negation':raw[-1],'analysis':full})
return {'cases':results,'with_negation':metrics(gold,pred),'without_negation':metrics(gold,raw),
'mixed_aspects':analyze('food good but service bad'),'intensifier':analyze('service is not very good'),
'scope':'Given aspect-term inventory, local English lexicon/window rules. No target extraction benchmark, learned classifier, sarcasm detector or VADER reproduction.'}
if __name__=='__main__':print(json.dumps(demo(),indent=2))
六个虚构例子明确规定了预期含义,四个被按意图分类;另外两个错误原样保留:not only good被误判负向,service great another delay这个预期为负面讽刺的片段被误判正向。缺少完整意图时,真实标注者也可能对这种短文本有分歧。
输出记录每个情感词、原文区间、基础分、程度因子、否定决定及方面归属,还提供否定窗口设为0的消融对照。两个版本的accuracy都是4/6,却错在不同地方:关闭否定会弄错not good,同时修好not only good。只看总分会隐藏这种交换。
开启窗口时,四个声明标签上的macroF1为0.75;关闭时为2/3。这些分数只属于此开发算例,不证明这个窗口能普遍改善真实评论。
情感分数需要解释口径
最近对象规则可能把情感词分给错误方面,小句边界也可能阻断对前文对象的继承。delicious but expensive没有给定方面词,程序因此不输出方面预测;它没有实现SemEval另设任务中的隐含类别检测。方面类别
讽刺、引述、比较、领域特定词义和说话者实际对象都可能让局部词典失效。一段话可能引用别人的赞美,同时否定那份赞美。因此,极性分数应附带证据与范围,不能当成人格测量或对象的已证实事实。
评测分类器时,说明标注单位和各类错误。Accuracy是标签正确的比例;本文额外的macroF1平等对待每个声明类别,某类F1分母为0时记0:
SemEval给定方面词及类别的极性子任务使用accuracy;本文四类宏平均是额外教学诊断,不是其官方主指标。不能把方面抽取F1与极性accuracy当成同一任务比较。任务评测片段
文本上限为10,000字符。n个词元、m个方面提及时,这个直接实现可能对每个意见词扫描m个候选,连同词元扫描,最坏需要O(n(m+1))时间;固定局部窗口对每个意见词增加有界工作。词元和证据占O(n+m)空间。这里选择易检查的实现,没有采用优化的最近对象数据结构。
问清改了什么,不只看分数有没有上涨
- 净分0为什么不能决定一个方面是中性还是冲突?
答案:正向2、负向2的净分为0,却包含相反意见;正向0、负向0的净分也为0,但没有识别到方向证据。分别保留两项总量,才能区分。
- 计算service is not very good的规则贡献,再解释同一个否定窗口为何弄错not only good。
答案:贡献为2×1.5×(−1)=−3。窗口只数已识别否定词,不理解not only结构。算术可以完全正确,语言解释却仍然错误。
- 两种窗口设置都答对六例中的四例,能否视为等价?
答案:不能。它们交换了not good与not only good上的错误,各类错误计数和macroF1也不同。应比较混淆结构及受影响语言形式,不能只看accuracy。
- 评论写delicious but expensive,却没有直接提到food或price。方面输出为空,能证明没有情感吗?
答案:不能。这暴露了给定方面词表的局限;推断食物与价格类别是另一项需要标签和评测的任务。为这两个已知词加规则后,它们变成开发例,不能继续当作新测试证据。
文档标签与方面标签回答不同问题
2002年的影评研究把整体情感作为文本分类问题;2014年的方面任务把对象检测与对象极性分开约定。它们提供了有用参照,却没有把某套数据集变成情感的通用定义。本文局部规则例子单独展示组合与对象归属,不复现两篇论文的系统。文档情感 方面定义
继续阅读文本分类中的训练/验证/测试流程、分词中的输入单位,以及精确率和召回率中的错误计数。计算语用学讨论字面表达为何可能不同于说话者想传达的意思。
先判断,再看解释
同一个方面获得正向强度2和负向强度2。声明的政策选哪个标签?
学习目标
- 评分前定义意见对象、粒度与标注政策。
- 区分中性与同时存在正负意见的冲突。
- 检查局部否定、程度修饰和方面归属规则。
- 保留讽刺与组合语义失败,并比较关注类别的指标。
先修与延伸
查看知识关联图 →参考文献
- Thumbs up? Sentiment Classification using Machine Learning Techniques
Bo Pang, Lillian Lee and Shivakumar Vaithyanathan · 2002 - SemEval-2014 Task4: Aspect Based Sentiment Analysis
Maria Pontiki, Dimitris Galanis, John Pavlopoulos, Harris Papageorgiou, Ion Androutsopoulos and Suresh Manandhar · 2014
版本1 · 编写状态: 已完成记录中的技术复核
查看技术检查记录(AI)
Codex · 2026-09-12 · Single AI author checked four paired equations and four explained exercises against scoped Pang2002 and SemEval2014. Given aspects, local English lexicon, clause/window negation, modifier factors and separate positive/negative evidence are explicit. Conflict versus neutral at net zero, not-only failure, assigned sarcastic intent and six-case ablation are retained. Both accuracies4/6 with different errors; macro F1 .75 versus2/3; intensified negative score-3 checked. Unpublished prerequisite links point to the catalogue. No VADER, trained sarcasm detector or ABSA benchmark reproduction.
- 单AI作者核四组公式及四解释练习,引用限定Pang2002和SemEval2014。明确给定方面、英语词典、分句窗口否定、强度因子与正负证据分存;保留零净分的冲突/中性、not-only错误、设定讽刺意图和六例消融。准确率同为4/6,宏F1为.75与2/3,负向增强为-3。未出版先修跳目录;不是VADER、训练讽刺检测器或ABSA基准复现。
- 四张原创计算SVG绑定双语MDX;十一项图检查、36组独立语言/宽度组合和四备用字体通过;本AI作者实际查看六张桌面/手机截图。完整页面验收另记,不称人工同行审校。
核对的来源位置:
- Thumbs up? Sentiment Classification using Machine Learning Techniques · Scoped text116–149,154–217: movie-review labels, word-list baselines, test-data use in third list, bag features and NB add-one estimation. Not full ME/SVM methods or complete comparative experiments.
- SemEval-2014 Task4: Aspect Based Sentiment Analysis · Text68–120: SB1–SB4, supplied aspect terms/categories and positive/negative/conflict/neutral; scoring excerpt291–294 confirms polarity accuracy. Not complete corpus, results or participant systems.
相关主题