【发布时间】:2016-11-11 01:48:55
【问题描述】:
我的 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 语句正确实施并考虑推文的情绪得分?我的目标是为每条推文输出适当的颜色。
【问题讨论】:
-
所以你是在比较一个对象和一个数字?
-
所以你想要分数??
twitterSentiment = results.score;?? -
正如 zerkms 所说,
twitterSentiment是一个 object ...该对象不等于 0。该对象不小于零,因此执行最终的 else。在对象与整数上使用数值比较甚至没有意义。也许你想比较一下twitterSentiment.score == 0?
标签: javascript node.js