综述
本文主要介绍TextBlob基本使用,侧重点在于情感分析。
TextBlob基本介绍
TextBlob是一个用Python编写的开源的文本处理库。它可以用来执行很多自然语言处理的任务,比如,词性标注,名词性成分提取,情感分析,文本翻译,等等。你可以在官方文档阅读TextBlog的所有特性。
基本功能
- Noun phrase extraction
- Part-of-speech tagging
- Sentiment analysis
- Classification (Naive Bayes, Decision Tree)
- Language translation and detection powered by Google Translate
- Tokenization (splitting text into words and sentences)
- Word and phrase frequencies
- Parsing
- n-grams
- Word inflection (pluralization and singularization) and lemmatization
- Spelling correction
- Add new models or languages through extensions
- WordNet integration
TextBlob安装
$ pip install -U textblob
$ python -m textblob.download_corpora
情感分析">情感分析
情感分析">什么是文本情感分析?
来自wiki百科:文本情感分析
文本情感分析(也称为意见挖掘)是指用自然语言处理、文本挖掘以及计算机语言学等方法来识别和提取原素材中的主观信息。
通常来说,情感分析的目的是为了找出说话者/作者在某些话题上或者针对一个文本两极的观点的态度。这个态度或许是他或她的个人判断或是评估,也许是他当时的情感状态(就是说,作者在做出这个言论时的情绪状态),或是作者有意向的情感交流(就是作者想要读者所体验的情绪)。
文本情感分析的一个基本步骤就是将文本中的某段已知文字的两极性进行分类,这个分类可能是在句子级、功能级。分类的作用就是判断出此文字中表述的观点是积极的、消极的、还是中性的情绪。更高级的“超出两极性”的情感分析还会寻找更复杂的情绪状态,比如“生气”、“悲伤”、“快乐” 等等。
情感分析">使用TextBlob进行情感分析
使用TextBlob情感分析的结果,以元组的方式进行返回,形式如(polarity, subjectivity). 其中polarity的分数是一个范围为 [-1.0 , 1.0 ] 浮点数, 正数表示积极,负数表示消极。subjectivity 是一个 范围为 [0.0 , 1.0 ] 的浮点数,其中 0.0 表示 客观,1.0表示主观的。
情感分析实例:
python hljs ">from textblob import TextBlob
testimonial = TextBlob("Textblob is amazingly simple to use. What great fun!")
print testimonial.sentiment
更多细节:
TextBlob Sentiment: Calculating Polarity and Subjectivity
参考:
【TextBlob】Tutorial: Quickstart
【TextBlob】API Reference