2016-02-01 6 views
1

Я занимаюсь анализом настроений в python. После очистки твитов, которые я использую, я застрял в получении окончательного счета за каждый твит. Я получаю значения, но не могу объединить их как один балл за твит. Вот кодСовокупные результаты from for loop in python

scores = {} # initialize an empty dictionary 
for line in sent_file: 
    term, score = line.split("\t") 
    scores[term] = int(score) # Convert the score to an integer. 



for line in tweet_file: 
     #convert the line from file into a json object 
    mystr = json.loads(line) 
    #check the language is english, if "lang" is among the keys 
    if 'lang' in mystr.keys() and mystr["lang"]=='en': 
      #if "text" is not among the keys, there's no tweet to read, skip it 
     if 'text' in mystr.keys(): 
      print mystr['text'] 
      resscore=[] 
      result = 0 
      #split the tweet into a list of words 
      words = mystr["text"].split() 
      #print type(words) 
      for word in words: 

       if word in scores: 
        result = scores[word] 
        resscore.append(result) 

        print str(sum(resscore)) 


       else: 
        result+=0 

выход я получаю, как

If nothing is as you'd imagine it to be then you may as well start imaging some mad stuff like dragons playing chess on a… 
-3 
-1 

Но я хочу, чтобы эти значения в этом случае -3, -1, чтобы агрегировать, чтобы дать окончательный счет для этого твит т.е. -4 , Благодаря

ответ

2

Накопить эти значения & напечатать их после завершения цикла:

# ... 

finalScore = 0 # final score 
for word in words: 

    if word in scores: 
     result = scores[word] 
     resscore.append(result) 

     finalScore += sum(resscore) 

print(str(finalScore)) 

# ... 
+0

Благодаря Mohammed – suri

+0

@suri вы радушны –