​cntext更新 | 新增效價情感分析函式

語言: CN / TW / HK

cntext更新至1.7.0

更新安裝

將自己電腦中的cntext更新至最新

pip install cntext==1.7.0

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_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

精選文章