- Python - 文本处理
- Python - 文本处理简介
- Python - 文本处理环境
- Python - 字符串不可变性
- Python - 排序行
- Python - 重新格式化段落
- Python - 计算段落中的标记
- Python - 二进制 ASCII 转换
- Python - 字符串作为文件
- Python - 向后文件读取
- Python - 筛选重复的单词
- Python - 从文本中提取电子邮件
- Python - 从文本中提取 URL
- Python - 美观打印
- Python - 文本处理状态机
- Python - 大写和小写转换
- Python - 分词
- Python - 删除停用词
- Python - 同义词和反义词
- Python - 文本翻译
- Python - 单词替换
- Python - 拼写检查
- Python - WordNet 接口
- Python - 语料库访问
- Python - 标记单词
- Python - 块和缺口
- Python - 块分类
- Python - 文本分类
- Python - 二元词组
- Python - 处理 PDF
- Python - 处理 Word 文档
- Python - 阅读 RSS 源
- Python - 情感分析
- Python - 查找和匹配
- Python - 文本整理
- Python - 文本换行
- Python - 频率分布
- Python - 文本摘要
- Python - 词干算法
- Python - 受限搜索
Python - 情感分析
语义分析是为了分析受众的总体意见。它可能是对新闻、电影或关于某个讨论问题的推文的反应。通常,此类反应取自社交媒体,并汇总到一个文件中,以通过 NLP 进行分析。我们将首先定义正面和负面单词的简单案例。然后采用一种方法,将这些单词作为包含这些单词的句子的一部分进行分析。我们使用 nltk 中的 sentiment_analyzer 模块。我们首先对一个单词进行分析,然后对成对的单词(也称为二元词组)进行分析。最后,我们将标记为负面情绪的单词放入 mark_negation 函数中。
import nltk
import nltk.sentiment.sentiment_analyzer
# Analysing for single words
def OneWord():
positive_words = ['good', 'progress', 'luck']
text = 'Hard Work brings progress and good luck.'.split()
analysis = nltk.sentiment.util.extract_unigram_feats(text, positive_words)
print(' ** Sentiment with one word **\n')
print(analysis)
# Analysing for a pair of words
def WithBigrams():
word_sets = [('Regular', 'fit'), ('fit', 'fine')]
text = 'Regular excercise makes you fit and fine'.split()
analysis = nltk.sentiment.util.extract_bigram_feats(text, word_sets)
print('\n*** Sentiment with bigrams ***\n')
print analysis
# Analysing the negation words.
def NegativeWord():
text = 'Lack of good health can not bring success to students'.split()
analysis = nltk.sentiment.util.mark_negation(text)
print('\n**Sentiment with Negative words**\n')
print(analysis)
OneWord()
WithBigrams()
NegativeWord()
运行上述程序后,将得到以下输出 -
** Sentiment with one word **
{'contains(luck)': False, 'contains(good)': True, 'contains(progress)': True}
*** Sentiment with bigrams ***
{'contains(fit - fine)': False, 'contains(Regular - fit)': False}
**Sentiment with Negative words**
['Lack', 'of', 'good', 'health', 'can', 'not', 'bring_NEG', 'success_NEG', 'to_NEG', 'students_NEG']
广告