public static TwitterPayload ComputeScore(Tweet tweet)
        {

            var payload = new TwitterPayload
            {
                ID = tweet.Id,
                CreatedAt = ParseTwitterDateTime(tweet.CreatedAt),
                UserName = tweet.User != null ? tweet.User.Name : null,
                TimeZone = tweet.User != null ? (tweet.User.TimeZone != null ? tweet.User.TimeZone : "(unknown)") : "(unknown)", 
                ProfileImageUrl = tweet.User != null ? (tweet.User.ProfileImageUrl != null ? tweet.User.ProfileImageUrl : "(unknown)") : "(unknown)",
                Text = tweet.Text,
                Language = tweet.Language != null ? tweet.Language : "(unknown)",
                RawJson = tweet.RawJson,
                SentimentScore = (int)Analyze(tweet.Text),
                //Topic = DetermineTopic(tweet, twitterKeywords),
            };

            // Don't be fooled - this really is the Pacific time zone,
            // not just standard time...
            var zone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
            payload.UtcOffset = zone.BaseUtcOffset.Hours;


            return payload;
        }
Example #2
0
        public static TwitterPayload ComputeScore(Tweet tweet, string twitterKeywords)
        {

            return new TwitterPayload
            {
                ID = tweet.Id,
                CreatedAt = ParseTwitterDateTime(tweet.CreatedAt),
                UserName = tweet.User != null ? tweet.User.Name : null,
                TimeZone = tweet.User != null ? (tweet.User.TimeZone != null ? tweet.User.TimeZone : "(unknown)") : "(unknown)", 
                ProfileImageUrl = tweet.User != null ? (tweet.User.ProfileImageUrl != null ? tweet.User.ProfileImageUrl : "(unknown)") : "(unknown)",
                Text = tweet.Text,
                Language = tweet.Language != null ? tweet.Language : "(unknown)",
                RawJson = tweet.RawJson,
                Topic = DetermineTopc(tweet.Text, twitterKeywords),
            };
        }
 public static TwitterPayload ComputeScore(Tweet tweet, string twitterKeywords)
 {
     // ss = Analyze(tweet.Text);
     MLAnalyze(tweet.Text).Wait();
     return new TwitterPayload
     {
         ID = tweet.Id,
         CreatedAt = ParseTwitterDateTime(tweet.CreatedAt),
         UserName = tweet.User != null ? tweet.User.Name : null,
         TimeZone = tweet.User != null ? (tweet.User.TimeZone != null ? tweet.User.TimeZone : "(unknown)") : "(unknown)", 
         ProfileImageUrl = tweet.User != null ? (tweet.User.ProfileImageUrl != null ? tweet.User.ProfileImageUrl : "(unknown)") : "(unknown)",
         Text = tweet.Text,
         Language = tweet.Language != null ? tweet.Language : "(unknown)",
         RawJson = tweet.RawJson,
         SentimentScore = (int)ss.Score,
         Sentiment = ss.Sentiment,
         Topic = DetermineTopc(tweet.Text, twitterKeywords),
         Location = tweet.User.location != null ? tweet.User.location: null,
         hashtag = tweet.entities.hashtags != null ? getHashTags(tweet.entities.hashtags) : null
     };
 }
Example #4
0
        static void Main(string[] args)
        {
            /* Get hashtag phrase that will be listened to / analysed by the Text Analytics API from user input. Basic use is to ask user:
             * Which hashtag do you want to listen to?
             * Which Event Hub do you want to send the sentiment score to?
             */

            //string hashtag = "microsoft";
            //string eventhubName = "";
            //string eventhubConn = "";

            //Console.WriteLine("Enter hashtag phrase (without the #): ");
            //hashtag = Console.ReadLine();
            //ConfigurationManager.AppSettings["twitter_keywords"] = hashtag;
            //Console.WriteLine("Enter Event Hub Name: ");
            //eventhubName = Console.ReadLine();
            //Console.WriteLine("Enter Event Hub Connection String: ");
            //eventhubConn = Console.ReadLine();

            //var config = new EventHubConfig();
            //config.ConnectionString = eventhubConn;
            //config.EventHubName = eventhubName;
            //var myEventHubObserver = new EventHubObserver(config);

            //Console.WriteLine("START\n");


            //Configure Twitter OAuth
            var oauthToken          = ConfigurationManager.AppSettings["oauth_token"];
            var oauthTokenSecret    = ConfigurationManager.AppSettings["oauth_token_secret"];
            var oauthCustomerKey    = ConfigurationManager.AppSettings["oauth_consumer_key"];
            var oauthConsumerSecret = ConfigurationManager.AppSettings["oauth_consumer_secret"];
            var keywords            = ConfigurationManager.AppSettings["twitter_keywords"];
            var accountKey          = ConfigurationManager.AppSettings["accountkey"];

            //Configure EventHub (comment this out if using user input code above)
            var config = new EventHubConfig();

            config.ConnectionString = ConfigurationManager.AppSettings["EventHubConnectionString"];
            config.EventHubName     = ConfigurationManager.AppSettings["EventHubName"];
            var myEventHubObserver = new EventHubObserver(config);

            //Azure ML Service
            string ServiceBaseUri = "https://api.datamarket.azure.com/";
            var    httpClient     = new HttpClient();

            httpClient.BaseAddress = new Uri(ServiceBaseUri);
            string creds = "AccountKey:" + accountKey;
            string authorizationHeader = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(creds));

            httpClient.DefaultRequestHeaders.Add("Authorization", authorizationHeader);
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


            //Original Sentiment demo code
            var datum = Tweet.StreamStatuses(new TwitterConfig(oauthToken, oauthTokenSecret, oauthCustomerKey, oauthConsumerSecret,
                                                               keywords)).Select(tweet => Sentiment.ComputeScore(tweet, keywords)).Select(tweet => new Payload {
                CreatedAt = Convert.ToDateTime(tweet.CreatedAt), Topic = tweet.Topic, SentimentScore = SentimentValue(tweet.Text, httpClient), Text = tweet.Text
            });

            datum.ToObservable().Subscribe(myEventHubObserver);
        }
        /// <summary>
        /// This is a simple text analysis from the twitter text based on some keywords
        /// </summary>
        /// <param name="tweetText"></param>
        /// <param name="keywordFilters"></param>
        /// <returns></returns>
        static string DetermineTopic(Tweet tweet, string keywordFilters)
        {
            var tweetText = tweet.Text;
            if (string.IsNullOrEmpty(tweetText))
                return string.Empty;

            if (tweet.Entities != null && tweet.Entities.urls != null)
            {
                foreach (var url in tweet.Entities.urls)
                {
                    // url might contain keyword
                    tweetText += " " + url.expanded_url;
                }
            }

            string subject = string.Empty;

            //keyPhrases are specified in app.config separated by commas.  Can have no leading or trailing spaces.  Example of key phrases in app.config
            //	<add key="twitter_keywords" value="Microsoft, Office, Surface,Windows Phone,Windows 8,Windows Server,SQL Server,SharePoint,Bing,Skype,XBox,System Center"/><!--comma to spit multiple keywords-->
            string[] keyPhrases = keywordFilters.Split(',');

            foreach (string keyPhrase in keyPhrases)
            {
                subject = keyPhrase;

                //a key phrase may have multiple key words, like: Windows Phone.  If this is the case we will only assign it a subject if both words are 
                //included and in the correct order. For example, a tweet will match if "Windows 8" is found within the tweet but will not match if
                // the tweet is "There were 8 broken Windows".  This is not case sensitive

                //Creates one array that breaks the tweet into individual words and one array that breaks the key phrase into individual words.  Within 
                //This for loop another array is created from the tweet that includes the same number of words as the keyphrase.  These are compared.  For example,
                // KeyPhrase = "Microsoft Office" Tweet= "I Love Microsoft Office"  "Microsoft Office" will be compared to "I Love" then "Love Microsoft" and 
                //Finally "Microsoft Office" which will be returned as the subject.  if no match is found "Do Not Include" is returned. 
                string[] KeyChunk = keyPhrase.Trim().Split(' ');
                string[] tweetTextChunk = tweetText.Split(' ');
                string Y;
                for (int i = 0; i <= (tweetTextChunk.Length - KeyChunk.Length); i++)
                {
                    Y = null;
                    for (int j = 0; j <= (KeyChunk.Length - 1); j++)
                    {
                        Y += tweetTextChunk[(i + j)] + " ";
                    }
                    if (Y != null) Y = Y.Trim();
                    if (Y.ToUpper().Contains(keyPhrase.ToUpper()))
                    {
                        return subject;
                    }
                }
            }

            // Generally because keyword is in a quoted status. We are okay ignoring those since they will come up on their own.
            return "Unknown";
        }