我的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語句正確實現並考慮推文的情感分數?我的目標是爲每條推文輸出適當的顏色。
因此,您將對象與數字進行比較? – zerkms
所以你想要得分? 'twitterSentiment = results.score;'?? – epascarello
正如zerkms所說,「twitterSentiment」是一個_object_ ...該對象不等於零。該對象不小於零,因此最後的else被執行。對一個對象和一個整數使用數字比較甚至沒有意義。也許你想比較'twitterSentiment.score == 0'? –