我无法针对单词列表(文件 2,制表符分隔,两列)对推文(文件 1,标准 twitter json 响应)进行情绪分析,并将其情绪分配给它们(正面或负面)。
问题是:顶部循环只运行一次,然后脚本在我循环文件 1 时结束,然后嵌套在其中,我循环文件 2 并尝试比较并保持每条推文的综合情绪的运行总和。
所以我有:
def get_sentiments(tweet_file, sentiment_file):
sent_score = 0
for line in tweet_file:
document = json.loads(line)
tweets = document.get('text')
if tweets != None:
tweet = str(tweets.encode('utf-8'))
#print tweet
for z in sentiment_file:
line = z.split('\t')
word = line[0].strip()
score = int(line[1].rstrip('\n').strip())
#print score
if word in tweet:
print "+++++++++++++++++++++++++++++++++++++++"
print word, tweet
sent_score += score
print "====", sent_score, "====="
#PROBLEM, IT'S ONLY DOING THIS FOR THE FIRST TWEET
file1 = open(tweetsfile.txt)
file2 = open(sentimentfile.txt)
get_sentiments(file1, file2)
我花了大半天的时间试图弄清楚为什么它打印出所有的推文,而没有用于 file2 的嵌套 for 循环,但是有了它,它只处理第一条推文然后退出。