Python数据分析:情感分析

该博客聚焦Python数据分析中的情感分析。介绍了自然语言处理(NLP),可将文本转化为计算机易理解形式,对预处理字符串进行向量化,其经典应用包括情感分析等。还提及简单情感分析,如用情感字典匹配关键词,也可使用机器学习模型。

Python数据分析:情感分析

自然语言处理(NLP)
  • 将自然语言(文本)转化为计算机程序更容易理解的形式
  • 预处理得到的字符串进行向量化
  • 经典应用:
    1. 情感分析
    2. 文本相似度
    3. 文本分类
简单情感分析:
  • 情感字典(sentiment dictionary)

    • 人工构造一个字典
    • 根据关键词匹配
  • 优点:简单实用

  • 缺点:遇到新词,特殊词等,扩展性较差

  • 实用机器学习模型,nltk.classify

# 简单的例子

import nltk
from nltk.stem import WordNetLemmatizer
from nltk.corpus import stopwords
from nltk.classify import NaiveBayesClassifier

text1 = 'I like the movie so much!'
text2 = 'That is a good movie.'
text3 = 'This is a great one.'
text4 = 'That is a really bad movie.'
text5 = 'This is a terrible movie.'

def proc_text(text):
    """
        预处处理文本
    """
    # 分词
    raw_words = nltk.word_tokenize(text)
    
    # 词形归一化
    wordnet_lematizer = WordNetLemmatizer()    
    words = [wordnet_lematizer.lemmatize(raw_word) for raw_word in raw_words]
    
    # 去除停用词
    filtered_words = [word for word in words if word not in stopwords.words('english')]
    
    # True 表示该词在文本中,为了使用nltk中的分类器
    return {word: True for word in filtered_words}

# 构造训练样本
train_data = [[proc_text(text1), 1],
              [proc_text(text2), 1],
              [proc_text(text3), 1],
              [proc_text(text4), 0],
              [proc_text(text5), 0]]

# 训练模型
nb_model = NaiveBayesClassifier.train(train_data)

# 测试模型
text6 = 'Everything is good.'
print(nb_model.classify(proc_text(text6)))

运行:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sweeney Chen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值