简体   繁体   中英

Error with “ blob = TextBlob(tweet[text])” in sentiment analysis using Python and Textblob

I'm working on a project in which I extract tweets from Twitter and run a sentiment analysis on specific keywords to draw conclusions. Unfortunately, I have come to a point where I am stumped. I have a sentiment analysis code:

When I use this: blob = TextBlob(tweet[text]) I get the following error:

Traceback (most recent call last): File "C:/Users/Michael/python/Sentiment2.py", line 65, in 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

Change this

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

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM