TextBlob Quick Start

摘要:
极性在[-1.0,1.0]范围内浮动。情绪text=“我今天很难过。”fromtextblobimportTextBlobblob=文本Blobblob。events#情绪拼写纠正˃˃˃b=TextBlob(“Iavvgoodspeling!

安装

pip install textblob

import nltk
pip install nltk
nltk.download('punkt')   # 安装一些语料库, 国内安装有问题
nltk.download('averaged_perceptron_tagger')

基本操作

情感分析

该sentiment属性返回形式的namedtuple 。极性在[-1.0,1.0]范围内浮动。主观性是在[0.0,1.0]范围内的浮动,其中0.0是非常客观的,而1.0是非常主观的。Sentiment(polarity, subjectivity)

text = "I am happy today. I feel sad today."

from textblob importTextBlob
blob =TextBlob(text)
blob.sentences   #能够分句
print(blob.sentences[0].sentiment)
print(blob.sentences[1].sentiment)
print(blob.sentiment)
#Sentiment(polarity=0.15000000000000002, subjectivity=1.0)

拼写纠正

>>> b = TextBlob("I havv goood speling!")
>>> print(b.correct())
I have good spelling!

返回的貌似是字符数组,最好用str转成字符串.

原理见How to Write a Spelling Corrector

例子:amazon评论情感分析

1. 获取原始评论数据

importpandas as pd

df=pd.read_csv('hair_dryer.tsv', sep='', usecols=['review_body','review_date'])

df.head()

TextBlob Quick Start第1张

2. 由于评论中有大量的拼写错误,加个单词纠正

defcorrect(text):
  b =TextBlob(text)
  returnstr(b.correct())


df['corrected_review_body'] = df['review_body'].map(correct)
df.head()

TextBlob Quick Start第2张

3. 得到情感极性和主观性

defget_polarity(text):
  testimonial =TextBlob(text)
  returntestimonial.sentiment.polarity

defget_subjectivity(text):
  testimonial =TextBlob(text)
  returntestimonial.sentiment.subjectivity


df['polarity'] = df['review_body'].map(get_polarity)
df['subjectivity'] = df['review_body'].map(get_subjectivity)

print(df)

TextBlob Quick Start第3张

4. 统计词频

统计词频主要是依靠 collections.Counter(word_list).most_common() 方法来自动统计并排序,如下:

row =df.shape[0]
words = ""
for i inrange(0, row):
  words = words + " " +p[i]

print(words)   #汇总每个评论

importcollections
collections.Counter(words.split()).most_common(50)  #显示top50

具体得到某个单词的频数:

coll =collections.Counter(words.split())
coll["good"]

5. 保存结果到CSV

df.to_csv("情感分析.csv")

参考链接:

1.https://textblob.readthedocs.io/en/dev/quickstart.html#get-word-and-noun-phrase-frequencies

2.https://blog.csdn.net/heykid/article/details/62424513#%E7%BB%9F%E8%AE%A1%E8%AF%8D%E9%A2%91

3.https://blog.csdn.net/sinat_29957455/article/details/79059436

免责声明:文章转载自《TextBlob Quick Start》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Adroid 总结--android ListView美化,个性化更改的属性ATM境内外取款测试点下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

js 获取图片url的Blob值并预览

1)使用 XMLHttpRequest 对象获取图片url的Blob值 //获取图片的Blob值 functiongetImageBlob(url, cb) { var xhr = newXMLHttpRequest(); xhr.open("get", url, true); xhr.responseTy...

JAVA 实现CLOB转String

CLOB 定义   数据库中的一种保存文件所使用的类型。   Character Large Object   SQL 类型 CLOB 在 JavaTM 编程语言中的映射关系。SQL CLOB 是内置类型,它将字符大对象 (Character Large Object) 存储为数据库表某一行中的一个列值。默认情况下,驱动程序使用 SQL locator(C...

使用 JavaScript File API 实现文件上传

概述 以往对于基于浏览器的应用而言,访问本地文件都是一件头疼的事情。虽然伴随着 Web 2.0 应用技术的不断发展,JavaScript 正在扮演越来越重要的角色,但是出于安全性的考虑,JavaScript 一直是无法访问本地文件的。于是,为了在浏览器中能够实现诸如拖拽并上传本地文件这样的功能,我们就不得不求助于特定浏览器所提供的各种技术了。比如对于 IE...

.net webapi后台返回pdf文件流,前端ajax请求下载,空白pdf排错经历

.net webapi后台返回pdf文件流,前端ajax请求下载,空白pdf排错经历 先上代码: 后台webapi代码: [HttpGet] [Route("{manifestId}")] public IHttpActionResult FindManifestPdfById([FromUri]string manifestId) {     byte...

web worker在react项目中的使用

新建一个worker.js文件,编写worker子线程脚本,代码如下: const workercode = () => { self.onmessage = function(e) { console.log('Message received from main script'); var workerResult = 'R...

angularJS实现无刷新文件下载

1 $scope.getExcel = function() { 2 $http.post("/production/statistics/export", { 3 storeId: $scope.$parent.currStore.storeId, 4 date: $scope.$parent.ledgerDat...