人工智能情感分析案例实践

举报
tea_year 发表于 2025/04/16 17:16:48 2025/04/16
134 0 0
【摘要】 TextBlob是基于NLTK的轻量级NLP库,主要功能包括:分词分句:words/sentences属性(基于nltk.tokenize)。词性标注:tags属性(Penn Treebank标签集)。情感分析:sentiment返回极性(-1~1)和主观性(0~1)。过去式处理:Word("went").lemmatize("v")返回原形(如"go")。

案例一、textblob分局分词效果

textblob提供了sentences和words方法进行分词和分句效果。

import nltk
nltk.download('punkt_tab')
#分句分词
from textblob import TextBlob

testresult=TextBlob("Now I will introduce myself briefly.My name is zhangchenguang. I was born in xi'an city.\
                    Henan province. I am now a computer teacher in Zhegnzhou. and Test..adsfa")

print(testresult.sentences)
print(testresult.words)

案例二、对词性进行标注

import nltk
nltk.download('averaged_perceptron_tagger_eng')
#词性标注,给每个词语标注词类标签(形容词、动词、名词等)textblob提供tags方法进行词性标注。
text="python si a high-level,general-purpose programming language"
from textblob import TextBlob
blog=TextBlob(text)
blog.tags

案例三、情感分析
#textbolob提供了sentiment方法进行情感分析,返回元祖,Sentiment(polarity,subjectivity)
#其中:polarity得分为[-1,0,1.0]靠近-1.0表示消极,靠近1.0表示积极,subjectivity得分为[0.0,1.0]靠近
#0.0表示客观,靠近1.0表示主观。

text='I feel sad today'
from textblob import TextBlob
blob=TextBlob(text)
blob.sentences[0].sentiment

运行结果如下所示:

案例四、单复数

sentence=TextBlob('Use 4 spaces per indentation level.')
print(sentence.words[2].singularize())
print(sentence.words[-1].pluralize())

程序运行效果如下所示:

案例五、过去式

import nltk
nltk.download('wordnet')
from textblob import Word
w=Word('octopi')
print(w.lemmatize())
#对单词 'went' 进行词形还原,并指定词性为 动词('v')
w=Word('went')
print(w.lemmatize('v'))

案例六、拼写校正

textblob提供了correct()方法进行拼写校正。

#拼写校正
from textblob import TextBlob
b=TextBlob("I have good speling!")
print(b.correct())

程序运行结果如下:

I have good spelling!

案例七、词频统计

textblob提供word_counts()方法进行单词词频的统计。

#词频统计
from textblob import TextBlob
text=TextBlob("I am a boy,I am a student")
count1=text.word_counts['am']
count2=text.word_counts['boy']
print(count1)
print(count2)

总结

TextBlob是基于NLTK的轻量级NLP库,主要功能包括:

  1. 分词分句words/sentences属性(基于nltk.tokenize)。

  2. 词性标注tags属性(Penn Treebank标签集)。

  3. 情感分析sentiment返回极性(-1~1)和主观性(0~1)。

  4. 过去式处理Word("went").lemmatize("v")返回原形(如"go")。

  5. 拼写校正correct()基于概率模型修正拼写(如"speling"→"spelling")。

  6. 词频统计:需手动结合collections.Counter实现。

特点:简单易用,适合快速文本处理,但精度和泛化能力有限,依赖规则和统计方法,对复杂任务(如语法纠错)支持较弱。

【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。