2016-11-11 24 views
-3

我的if/else語句直接去別的,我似乎無法弄清楚爲什麼。這裏是我的代碼:如果/ else聲明直接去別的

var sentiment = require ('sentiment'); 
var twitterSentiment; 
var geoColor; 
var results; 
var twit = new twitter({ 
    consumer_key: credentials.consumer_key, 
    consumer_secret: credentials.consumer_secret, 
    access_token_key: credentials.access_token_key, 
    access_token_secret: credentials.access_token_secret 
}); 

twit.stream(
    'statuses/filter', 
    { 'locations': location }, 
    function(stream) { 
     stream.on('data', function(tweet) { 
      console.log(tweet.text); 
      results = sentiment (tweet.text); 
      twitterSentiment = results; 


      //Comparison of Sentiment Scores 
       if (twitterSentiment == 0) { 
       geoColor = '#B5B5B5'; 
       } 

      else if (twitterSentiment < 0) { 
       geoColor = '#FC0828'; 
      } 
      else { 
       geoColor = '#00DE1E'; 
      } 

      console.log (geoColor);   
     }); 
    }); 

這是一個例子輸出:

omg yes!! 
#00DE1E 
Do you think will actually understand? I want to ask mine the same question. Just not sure I'm ready to have that conversation. 
#00DE1E 
A thing of beauty by: 
@youtube 
#00DE1E 
do you still do this?? 
#00DE1E 

正如你可以看到所有的微博都被只用一種顏色標識;好像我的if/else聲明沒有正確實現?

當我改變console.log (geoColor);console.log (results);這是我的輸出:

omg yes!! 
{ score: 1, 
    comparative: 0.25, 
    tokens: [ 'omg', 'yes' ], 
    words: [ 'yes' ], 
    positive: [ 'yes' ], 
    negative: [] } 

Do you think will actually understand? I want to ask mine the same question. Just not sure I'm ready to have that conversation. 
{ score: 1, 
    comparative: 0.041666666666666664, 
    tokens: 
    [ 
    'do', 
    'you', 
    'think', 
    'will', 
    'actually', 
    'understand', 
    'i', 
    'want', 
    'to', 
    'ask', 
    'mine', 
    'the', 
    'same', 
    'question', 
    'just', 
    'not', 
    'sure', 
    'i\'m', 
    'ready', 
    'to', 
    'have', 
    'that', 
    'conversation' ], 
    words: [ 'want' ], 
    positive: [ 'want' ], 
    negative: [] } 

A thing of beauty by: 
@youtube 
{ score: 3, 
    comparative: 0.25, 
    tokens: 
    [ 'a', 
    'thing', 
    'of', 
    'beauty', 
    'by', 
    'youtube', ], 
    words: [ 'beauty' ], 
    positive: [ 'beauty' ], 
    negative: [] } 

do you still do this?? 
{ score: 0, 
    comparative: 0, 
    tokens: 
    [ 
    'do', 
    'you', 
    'still', 
    'do', 
    'this' ], 
    words: [], 
    positive: [], 
    negative: [] } 

正如你可以看到每個鳴叫分別擁有1,1,3,0他們個人的情感分那麼,爲什麼我的if/else語句無視這些數字?

我可以在我的代碼中更改哪些內容,以便我的if/else語句正確實現並考慮推文的情感分數?我的目標是爲每條推文輸出適當的顏色。

+4

因此,您將對象與數字進行比較? – zerkms

+1

所以你想要得分? 'twitterSentiment = results.score;'?? – epascarello

+0

正如zerkms所說,「twitterSentiment」是一個_object_ ...該對象不等於零。該對象不小於零,因此最後的else被執行。對一個對象和一個整數使用數字比較甚至沒有意義。也許你想比較'twitterSentiment.score == 0'? –

回答

0

您正在將twitterSentiment設置爲結果對象,並將整個對象而不僅僅是分數進行比較。將您的代碼更改爲:

if (twitterSentiment.score == 0) { 
    geoColor = '#B5B5B5'; 
} 

else if (twitterSentiment.score < 0) { 
    geoColor = '#FC0828'; 
} 
else { 
    geoColor = '#00DE1E'; 
} 
+0

這讓感覺真棒醬非常感謝你! –

1

您的變種results是一個對象,它包含許多其他屬性。在JavaScript中,「if」子句將始終爲非空對象實例返回「true」。將一個對象與一個數字進行比較時 - 非基本對象的任何實例將返回1

正如你所說,你想比較你的score屬性的值,所以你需要做的是在你的if子句中引用你的results.score

更改爲

twitterSentiment = results.score; 

應該解決您的問題。

+0

回答時,建議您添加一些指導,而不是僅發佈解決方案。 – RedJandal

+0

謝謝你的評論,我已經更新了答案 – Zilvinas