topical media & game development

talk show tell print

student-twitter-swarm-Tweet.ax

student-twitter-swarm-Tweet.ax (swf ) [ flash ] flex


  package {
          import mx.controls.Alert;
          
          public class @ax-student-twitter-swarm-Tweet {
                  private var dateStamp:String, id:String, text:String, screenName:String, profilePic:String;
                  private var keywords:XMLList; 
                  private var moods:Array, colors:Array, color:Array;
                  private var emotions:Array;
                   
                  public function @ax-student-twitter-swarm-Tweet(data:XML, emotions:Array, colors:Array)        {
                          this.dateStamp = data.created_at;
                          this.id = data.id;
                          this.text = data.text;
                          this.keywords = data.keywords.keyword;
                          this.screenName = data.user.screen_name;
                          this.profilePic = data.user.profile_image_url;
                          this.color = [0, 0, 0];
                          
                          this.emotions = emotions;
                          this.colors = colors;
                          
                          extractEmotions();
                  }
                  
                  public function extractEmotions ():void {
                          // Goes through the message and finds the words that are associated
                          // with certain emotions.
  
                          var result:Array = [];
                          var color:Array = [0.5, 0.5, 0.5];
                          
                          for(var i:int = 0; i < this.emotions.length; i++) {
                                  var found:Boolean = false;
                                  for(var j:int = 0; j < this.emotions[i].length; j++) {
                                          var item:XML;
                                          for each(item in this.keywords) { 
                                                  if (!found && item.toString() == this.emotions[i][j]) {
                                                          result[result.length] = this.emotions[i][0];
                                                          found = true;
                                                          
                                                          color[0] += this.colors[i][0];
                                                          color[1] += this.colors[i][1];
                                                          color[2] += this.colors[i][2];
                                                  } 
                                          } 
                                          if (found) break;
                                  }
                          }
                          if (result.length == 0) {
                                  result[0] = "unknown";
                          }
                          this.moods = result;
                          
                          this.color[0] = color[0] / result.length;
                          this.color[1] = color[1] / result.length;
                          this.color[2] = color[2] / result.length;
                  }
                  
                  public function getMoods ():String {
                          // Returns a string containing all the moods this tweet has been 
                          // tagged with. Used for display purposes
                          
                          var result:String = "";
                          for(var i:int = 0; i < this.moods.length; i++) {
                                  result += this.moods[i] + " ";
                          }
                          return result;
                  }
                  
                  public function getText ():String {
                          return this.text;
                  }
                  
                  public function getScreenName ():String {
                          return this.screenName;
                  }
                  
                  public function getPic ():String {
                          return this.profilePic;
                  }
                  
                  public function getColor ():Array {
                          return this.color;
                  }
                                  
                  public function calculateSimilarity (other:student_twitter_swarm_Tweet):Number {  
                          // Compares this tweet to the other one to see if they should be 
                          // attracted to each other.
  
                          if (this.id == other.id) { return 1; } 
                          
                          for(var i:int = 0; i < this.moods.length; i++) {
                                  if ( other.containsMood(this.moods[i]) ) {
                                          return 1.0;
                                  }
                          }
                          return 0.0;
                  }
                  
                  public function containsMood (input:String):Boolean {
                          // Returns true if this tweet has been tagged with the given mood, 
                          // otherwise false. 
  
                          for(var i:int = 0; i < this.moods.length; i++) {
                                  if (this.moods[i] == input) return true;
                          }
                          return false;
                  }
          }
  }
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.