【问题标题】:Error with " blob = TextBlob(tweet[text])" in sentiment analysis using Python and Textblob使用 Python 和 Textblob 进行情感分析时出现“blob = TextBlob(tweet[text])”错误
【发布时间】:2018-03-09 07:10:18
【问题描述】:

我正在开展一个项目,在该项目中我从 Twitter 中提取推文,并对特定关键字进行情绪分析以得出结论。不幸的是,我已经到了让我难过的地步。我有一个情绪分析代码:

当我使用这个时:blob = TextBlob(tweet[text]) 我收到以下错误:

Traceback(最近一次调用最后一次):文件 “C:/Users/Michael/python/Sentiment2.py”,第 65 行,在 blob = TextBlob(tweet[text]) NameError: name 'text' is not defined

import json
import re
import operator 
from textblob import TextBlob
from collections import Counter
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
import string
import os, sys, codecs
import csv
import sys
from nltk import bigrams

emoticons_str = r"""
    (?:
        [:=;] # Eyes
        [oO\-]? # Nose (optional)
        [D\)\]\(\]/\\OpP] # Mouth
    )"""

regex_str = [
    emoticons_str,
    r'<[^>]+>', # HTML tags
    r'(?:@[\w_]+)', # @-mentions
    r"(?:\#+[\w_]+[\w\'_\-]*[\w_]+)", # hash-tags
    r'http[s]?://(?:[a-z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-f][0-9a-f]))+', # URLs

    r'(?:(?:\d+,?)+(?:\.?\d+)?)', # numbers
    r"(?:[a-z][a-z'\-_]+[a-z])", # words with - and '
    r'(?:[\w_]+)', # other words
    r'(?:\S)' # anything else
]

tokens_re = re.compile(r'('+'|'.join(regex_str)+')', re.VERBOSE | re.IGNORECASE)
emoticon_re = re.compile(r'^'+emoticons_str+'$', re.VERBOSE | re.IGNORECASE)

def tokenize(s):
    return tokens_re.findall(s)

def preprocess(s, lowercase=False):
    tokens = tokenize(s)
    if lowercase:
        tokens = [token if emoticon_re.search(token) else token.lower() for token in tokens]
    return tokens
punctuation = list(string.punctuation)
stop = stopwords.words('english') + punctuation + ['rt', 'via'] 
fname = 'python.json'
with open(fname, 'r') as f:
    lis=[]
    neg=0.0
    n=0.0
    net=0.0
    pos=0.0
    p=0.0
    count_all = Counter()
    cout=0
    for line in f:
        try:
            tweet = json.loads(line)
        except:
            continue
        # Create a list with all the terms
        blob = TextBlob(tweet[text])
        cout+=1
        lis.append(blob.sentiment.polarity)
        #print blob.sentiment.subjectivity
        #print (os.listdir(tweet["text"]))
        if blob.sentiment.polarity < 0:
            sentiment = "negative"
            neg+=blob.sentiment.polarity
            n+=1
        elif blob.sentiment.polarity == 0:
            sentiment = "neutral"
            net+=1
        else:
            sentiment = "positive"
            pos+=blob.sentiment.polarity
            p+=1

        # output sentiment

    print("Total tweets"),len(lis)
    print("Positive"),float(p/cout)*100,"%"
    print("Negative"),float(n/cout)*100,"%"
    print("Neutral"),float(net/len(lis))*100,"%"
    #print lis
        # determine if sentiment is positive, negative, or neutral

        # output sentiment
        #print sentiment

【问题讨论】:

    标签: python sentiment-analysis


    【解决方案1】:

    改变这个

    # Create a list with all the terms
    blob = TextBlob(tweet[text])
    

    # Create a list with all the terms
    blob = TextBlob(tweet['text'])
    

    【讨论】:

    • 可以显示推文的内容吗?它应该有一个名为“text”的键
    • 这不仅仅是一条单一的推文。我正在使用包含大约 650 000 条推文的 .json 文件来执行此操作
    猜你喜欢
    • 2020-03-26
    • 2020-08-14
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 2022-06-22
    相关资源
    最近更新 更多