2016-11-11 55 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