Reputation: 1585
I get the following error when trying to execute the code below. How can I solve it?
Traceback (most recent call last): File "C:\Users\vaio\Desktop\coding\Twitter-Sentiment-Analysis-master\Twitter-Sentiment-Analysis-master\tweet_sentiment.py", line 64, in main() File "C:\Users\vaio\Desktop\coding\Twitter-Sentiment-Analysis-master\Twitter-Sentiment-Analysis-master\tweet_sentiment.py", line 53, in main sent_file = open(sys.argv[1]) IndexError: list index out of range [Finished in 0.1s with exit code 1]
import sys
import json
import ast
import re
def calcScoreFromTerm(termScoreFile): # returns a dictionary with term-score values
scores ={}
for line in termScoreFile:
term, score = line.split("\t")
scores[term] = float(score)
return scores
def getTweetText(tweet_file): #returns a list of all tweets
tweets = []
for line in tweet_file:
# print line
jsondata = json.loads(line)
if "text" in jsondata.keys():
tweets.append(jsondata["text"])
tweet_file.close()
return tweets
def filterTweet(et):
# Remove punctuations and non-alphanumeric chars from each tweet string
pattern = re.compile('[^A-Za-z0-9]+')
et = pattern.sub(' ', et)
#print encoded_tweet
words = et.split()
# Filter unnecessary words
for w in words:
if w.startswith("RT") or w.startswith("www") or w.startswith("http"):
words.remove(w)
return words
def getTweetSentiments(tweets, scores): #returns a list of sentiments
sentiments = []
for tweet in tweets:
sentiment = 0.0
tweet = tweet.encode('utf-8')
wordsInTweet = filterTweet(tweet) # re.split('\W+',tweet)
for eachWord in wordsInTweet:
if eachWord in scores:
sentiment += scores[eachWord]
sentiments.append(sentiment)
return sentiments
def main():
sent_file = open(sys.argv[1])
tweet_file = open(sys.argv[2])
scores = calcScoreFromTerm(sent_file)
tweets = getTweetText(tweet_file)
sentiments = getTweetSentiments(tweets, scores)
for sentiment in sentiments:
print sentiment
if __name__ == '__main__':
main()
Upvotes: 1
Views: 299
Reputation: 6594
It looks like you're supposed to provide the sent_file
and tweet_file
as arguments to the command. So you would invoke it like so:
python tweet_sentiment.py <sent_file_name> <tweet_file_name>
Upvotes: 1