2016-11-11 2 views
-3

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.

+4

Sie vergleichen also ein Objekt mit einer Nummer? – zerkms

+1

Sie wollen also die Gäste? 'twitterSentiment = results.score;' ?? – epascarello

+0

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? –

Antwort

0

Sie setzen twitterSentiment auf das Ergebnisobjekt und vergleichen das gesamte Objekt und nicht nur das Ergebnis. Ändern Sie Ihren Code in:

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

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

Das macht so viel Sinn super Sauce danke !! –

1

Ihr var results ist ein Objekt, das viele andere Attribute enthält. In JavaScript gibt die "if" -Klausel für Nicht-Null-Objektinstanzen immer "true" zurück. Wenn Sie ein Objekt mit einer Zahl vergleichen, gibt jede Instanz eines nicht primitiven Objekts 1 zurück.

Wie Sie bereits gesagt haben, möchten Sie den Wert Ihres score Attributs vergleichen. Sie müssen also Ihre results.score in Ihrer if-Klausel referenzieren.

Wechsel zu

twitterSentiment = results.score; 

Sollte Ihr Problem beheben.

+0

Bei der Beantwortung wird empfohlen, einige Hinweise hinzuzufügen, anstatt nur die Lösung zu posten. – RedJandal

+0

Vielen Dank für den Kommentar, ich habe die Antwort aktualisiert – Zilvinas

Verwandte Themen