$ pip install -U textblob
$ python -m textblob.download_corpora
我在尝试运行情绪分析脚本时遇到此问题。我已经使用以下命令安装了textblob:
import json
import csv
from textblob import TextBlob
#set the input and outputing file
input_file= "tweets.json"
output_file= "results.csv"
#store all json data
tweets_novartis = []
with open (input_file) as input_novartis:
for line in input_novartis:
tweets_novartis.append(json.loads(line))
#open output file to store the results
with open(output_file, "w") as output_novartis:
writer = csv.writer(output_novartis)
#iterate through all the tweets
for tweets_novartis in tweets_novartis:
tweet = tweets_novartis["full_text"]
#TextBlob to calculate sentiment
tweet = Textblob(tweet)
tweet = tweet.replace("\n" , " ")
tweet = tweet.replace("\r" , " ")
sentiment = [[tweet.sentiment.polarity]]
writer.writerows(sentiment)
代码如下:
{{1}}
答案 0 :(得分:0)
您需要从TextBlob
模块
textblob
from textblob import TextBlob
tweet = TextBlob(tweet)
来自文档:
>>> from textblob import TextBlob
>>> wiki = TextBlob("Python is a high-level, general-purpose programming language.")
<强> More info 强>
答案 1 :(得分:0)
Python区分大小写。使用此:
from textblob import TextBlob
tweet = TextBlob(tweet)
python!= vba
答案 2 :(得分:0)
textblob
作为包不可调用,因此我们从TextBlob
包中导入textblob
对象。
from textblob import TextBlob
b = TextBlob('i hate to go to school')
print(b.sentiment)