代码:https://github.com/sloria/TextBlob
本次任务的目的是,输入一段话,自动对这段话进行单词分割和句子分割,然后分别对单词进行词性分析,对句子进行情感倾向评分。
工程路径如图:所有文件都没有改动,在工程里面新增一个main.py,添加代码如下。
from textblob import TextBlob
text = "I love you. I hate you. This is a dog. The woman is running on the street. I am so sad. Today is a good day."
blob = TextBlob(text)
for i in range(len(blob.tags)):
print("{}: {}".format(blob.tags[i][0], blob.tags[i][1]))
for i in range(len(blob.sentences)):
print("{}:情感倾向得分 {}".format(blob.sentences[i].string, blob.sentences[i].polarity))
"""
输出结果
I: PRP
love: VBP
you: PRP
I: PRP
hate: VBP
you: PRP
This: DT
is: VBZ
a: DT
dog: NN
The: DT
woman: NN
is: VBZ
running: VBG
on: IN
the: DT
street: NN
I: PRP
am: VBP
so: RB
sad: JJ
Today: NN
is: VBZ
a: DT
good: JJ
day: NN
I love you.:情感倾向得分 0.5
I hate you.:情感倾向得分 -0.8
This is a dog.:情感倾向得分 0.0
The woman is running on the street.:情感倾向得分 0.0
I am so sad.:情感倾向得分 -0.5
Today is a good day.:情感倾向得分 0.7
"""
实验环境:python3.9.7
依赖包:手动装nose和nltk,其他都是附带的。
click 8.0.1
colorama 0.4.4
joblib 1.0.1
nltk 3.6.3
nose 1.3.7
pip 21.1.2
regex 2021.9.24
setuptools 57.0.0
tqdm 4.62.3
wheel 0.36.2
装nltk时遇到点问题,提示需要nltk.download('xxx')一些包,需要手动下载,下载地址:
nltk_data/packages at gh-pages · nltk/nltk_data · GitHub
如下图,将红色圈圈中的压缩包下载解压在这个路径即可。
===============================================================
词性总共有36种:
1. CC Coordinating conjunction 连接词
2. CD Cardinal number 基数词
3. DT Determiner 限定词(如this,that,these,those,such,不定限定词:no,some,any,each,every,enough,either,neither,all,both,half,several,many,much,(a) few,(a) little,other,another.
4. EX Existential there 存在句
5. FW Foreign word 外来词
6. IN Preposition or subordinating conjunction 介词或从属连词
7. JJ Adjective 形容词或序数词
8. JJR Adjective, comparative 形容词比较级
9. JJS Adjective, superlative 形容词最高级
10. LS List item marker 列表标示
11. MD Modal 情态助动词
12. NN Noun, singular or mass 常用名词 单数形式
13. NNS Noun, plural 常用名词 复数形式
14. NNP Proper noun, singular 专有名词,单数形式
15. NNPS Proper noun, plural 专有名词,复数形式
16. PDT Predeterminer 前位限定词
17. POS Possessive ending 所有格结束词
18. PRP Personal pronoun 人称代词
19. PRP$ Possessive pronoun 所有格代名词
20. RB Adverb 副词
21. RBR Adverb, comparative 副词比较级
22. RBS Adverb, superlative 副词最高级
23. RP Particle 小品词
24. SYM Symbol 符号
25. TO to 作为介词或不定式格式
26. UH Interjection 感叹词
27. VB Verb, base form 动词基本形式
28. VBD Verb, past tense 动词过去式
29. VBG Verb, gerund or present participle 动名词和现在分词
30. VBN Verb, past participle 过去分词
31. VBP Verb, non-3rd person singular present 动词非第三人称单数
32. VBZ Verb, 3rd person singular present 动词第三人称单数
33. WDT Wh-determiner 限定词(如关系限定词:whose,which.疑问限定词:what,which,whose.)
34. WP Wh-pronoun 代词(who whose which)
35. WP$ Possessive wh-pronoun 所有格代词
36. WRB Wh-adverb 疑问代词(how where when)