其他
cntext更新 | 新增效价情感分析函数
cntext更新至1.7.0
更新安装
将自己电脑中的cntext更新至最新
pip install cntext==1.7.7
sentiment_by_valence函数
之前cntext只有一个情感计算函数ct.sentiment(),该函数默认所有情感词权重均为1,只需要统计文本中情感词的个数,即可得到文本情感得分。
sentiment_by_valence函数考虑了词语的效价(valence)
sentiment_by_valence(text, diction, lang='english')
text 待输入文本 diction 带效价的词典,DataFrame格式。 lang 语言类型'chinese' 或 'english',默认'english'
这里我们以 ,通过cntext可以让代码更简化。 Concreteness.pkl 整理自 Brysbaert2014的文章。
Brysbaert, M., Warriner, A. B., & Kuperman, V. (2014). Concreteness ratings for 40 thousand generally known English word lemmas. Behavior Research Methods, 46, 904–911
import cntext as ct
# load the concreteness.pkl dictionary file
concreteness_df = ct.load_pkl_dict('Concreteness.pkl')['Concreteness']
concreteness_df.head()
Run
word | valence | |
---|---|---|
0 | roadsweeper | 4.85 |
1 | traindriver | 4.54 |
2 | tush | 4.45 |
3 | hairdress | 3.93 |
4 | pharmaceutics | 3.77 |
之前分享过 计算文本的语言具体性 | 以JCR2021论文为例
先看一条文本的具体性度量
reply = "I'll go look for that"
score=ct.sentiment_by_valence(text=reply,
diction=concreteness_df,
lang='english')
score
Run
1.85
很多个文本的具体性度量
employee_replys = ["I'll go look for that",
"I'll go search for that",
"I'll go search for that top",
"I'll go search for that t-shirt",
"I'll go look for that t-shirt in grey",
"I'll go search for that t-shirt in grey"]
for idx, reply in enumerate(employee_replys):
score=ct.sentiment_by_valence(text=reply,
diction=concreteness_df,
lang='english')
template = "Concreteness Score: {score:.2f} | Example-{idx}: {exmaple}"
print(template.format(score=score,
idx=idx,
exmaple=reply))
ct.sentiment_by_valence(text=text, diction=concreteness_df, lang='english')
Run
Concreteness Score: 1.55 | Example-0: I'll go look for that
Concreteness Score: 1.55 | Example-1: I'll go search for that
Concreteness Score: 1.89 | Example-2: I'll go search for that top
Concreteness Score: 2.04 | Example-3: I'll go search for that t-shirt
Concreteness Score: 2.37 | Example-4: I'll go look for that t-shirt in grey
Concreteness Score: 2.37 | Example-5: I'll go search for that t-shirt in grey