Meine if/else Aussage geht direkt zu anderen und ich kann nicht herausfinden, warum. Hier ist mein Code:If/else Anweisung geht direkt zu anderen
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);
});
});
Dies ist ein Beispiel für die Ausgabe:
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
Wie Sie alle Tweets identifiziert werden durch nur eine Farbe sehen kann; fast so, als ob meine if/else
-Anweisung nicht korrekt implementiert ist?
Wenn ich console.log (geoColor);
-console.log (results);
ändern Dies ist meine Ausgabe:
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: [] }
Wie Sie jeder tweet hat ihre individuellen Stimmungs Punktzahl von jeweils sehen 1,1,3,0
Warum ist meine if/else-Anweisung, diese Zahlen abgesehen?
Was kann ich in meinem Code ändern, damit meine if/else-Anweisung den Sentiment-Score der Tweets korrekt implementiert und berücksichtigt? Mein Ziel ist es, für jeden Tweet die passende Farbe auszugeben.
Sie vergleichen also ein Objekt mit einer Nummer? – zerkms
Sie wollen also die Gäste? 'twitterSentiment = results.score;' ?? – epascarello
Wie zerkms sagt, ist "twitterSentiment" ein _object_ ... das Objekt ist nicht gleich null. Dieses Objekt ist nicht kleiner als Null, daher wird das letzte else ausgeführt. Es macht nicht einmal Sinn, numerische Vergleiche für ein Objekt und eine ganze Zahl zu verwenden. Vielleicht möchtest du "twitterSentiment.score == 0" vergleichen? –