コード例 #1
0
        internal static async Task <string> Sentiment(this string text, string apiKey)
        {
            if (string.IsNullOrEmpty(text))
            {
                return("0.0");
            }

            // Create a client
            var client = new TextAnalyticsAPI(new ServiceCredentials.ApiKeyServiceClientCredentials(apiKey));

            // Extract the language
            var result = await client.DetectLanguageAsync(new BatchInput(new List <Input>()
            {
                new Input("1", text)
            }));

            var language = result.Documents?[0].DetectedLanguages?[0].Name;

            // Get the sentiment
            var sentimentResult = await client.SentimentAsync(
                new MultiLanguageBatchInput(
                    new List <MultiLanguageInput>()
            {
                new MultiLanguageInput(language, "0", text),
            }));

            return(sentimentResult.Documents?[0].Score?.ToString("#.#"));
        }
コード例 #2
0
        public async Task OnTurn(ITurnContext context, MiddlewareSet.NextDelegate next)
        {
            if (context.Activity.Type is ActivityTypes.Message)
            {
                if (string.IsNullOrEmpty(context.Activity.Text))
                {
                    context.Services.Add <string>("0.0");
                }

                // Create a client
                var client = new TextAnalyticsAPI(new ApiKeyServiceClientCredentials(ApiKey));

                client.AzureRegion = AzureRegions.Westeurope;

                // Extract the language
                var result = await client.DetectLanguageAsync(new BatchInput(new List <Input>()
                {
                    new Input("1", context.Activity.Text)
                }));

                var language = result.Documents?[0].DetectedLanguages?[0].Name;

                // Get the sentiment
                var sentimentResult = await client.SentimentAsync(
                    new MultiLanguageBatchInput(
                        new List <MultiLanguageInput>()
                {
                    new MultiLanguageInput("en", "0", context.Activity.Text),
                }));

                context.Services.Add <string>(sentimentResult.Documents?[0].Score?.ToString("#.#"));
            }

            await next();
        }
コード例 #3
0
        private double?ExtractSentiments(List <string> Queries)
        {
            // Create a client.
            ITextAnalyticsAPI client = new TextAnalyticsAPI(new ApiKeyServiceClientCredentials());

            client.AzureRegion = AzureRegions.Westus;

            MultiLanguageBatchInput multiLanguageBatchInput = new MultiLanguageBatchInput();

            multiLanguageBatchInput.Documents = new List <MultiLanguageInput>();

            for (int i = 0; i < Queries.Count; i++)
            {
                multiLanguageBatchInput.Documents.Add(new MultiLanguageInput("en", i.ToString(), Queries[i]));
            }

            SentimentBatchResult sentimentBatchResult = client.SentimentAsync(multiLanguageBatchInput).Result;
            double?score = 0;

            // Printing sentiment results
            foreach (var document in sentimentBatchResult.Documents)
            {
                score += document.Score;
            }

            return(score / sentimentBatchResult.Documents.Count);
        }
コード例 #4
0
    public double GetSentiment(string phrase)
    {
        // Extracting sentiment.
        SentimentBatchResult result = _client.SentimentAsync(
            new MultiLanguageBatchInput(
                new List <MultiLanguageInput>()
        {
            new MultiLanguageInput("en", "0", phrase)
        })).Result;


        // Printing sentiment results.
        return(result.Documents.FirstOrDefault()?.Score ?? 0.5);
    }
コード例 #5
0
        public static async Task <SentimentResult> AnalyzeWords(string words)
        {
            SentimentResult sentimentResult = new SentimentResult();

            try
            {
                var sentimentInput = new MultiLanguageInput(text: words, id: "1");

                // First find out the input language
                var detectLanguageInput = new Input(id: "1", text: words);
                var languageResult      = await client.DetectLanguageAsync(new BatchInput(new List <Input> {
                    detectLanguageInput
                }), numberOfLanguagesToDetect : 1);

                if (languageResult.Errors.Count > 0)
                {
                    return(null);
                }

                sentimentResult.DetectedLanguage = languageResult.Documents.FirstOrDefault().DetectedLanguages.FirstOrDefault().Iso6391Name;

                // Setup for the sentiment
                sentimentInput.Language = sentimentResult.DetectedLanguage;
                var mlbi = new MultiLanguageBatchInput(new List <MultiLanguageInput>()
                {
                    sentimentInput
                });

                // Find the sentiment!
                var sentimentResultCall = await client.SentimentAsync(mlbi);

                if (sentimentResultCall.Errors.Count > 0)
                {
                    return(null);
                }

                sentimentResult.SentimentAnalysis = sentimentResultCall.Documents.FirstOrDefault().Score ?? 0;

                sentimentResult.ScoreResult();

                return(sentimentResult);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }

            return(sentimentResult);
        }
コード例 #6
0
        public async Task <TextAnalyticsResult> AnalyzeTextAsync(string text)
        {
            try
            {
                // COGNITIVE SERVICE : client API REST
                ITextAnalyticsAPI client = new TextAnalyticsAPI(new ApiKeyServiceClientCredentials())
                {
                    AzureRegion = AzureRegions.Australiaeast
                };

                var result = new TextAnalyticsResult();

                // COGNITIVE SERVICE : Détection de la langue
                var resultLanguage = await client.DetectLanguageAsync(new BatchInput(
                                                                          new List <Input>()
                {
                    new Input("0", text)
                }));

                var l = resultLanguage.Documents[0].DetectedLanguages[0];
                result.Language    = l.Name;
                result.LanguageIso = l.Iso6391Name;

                // COGNITIVE SERVICE : Détection des phrases clés
                var resultKeyPhrases = client.KeyPhrasesAsync(new MultiLanguageBatchInput(
                                                                  new List <MultiLanguageInput>()
                {
                    new MultiLanguageInput(result.LanguageIso, "0", text)
                }));

                result.KeyPhrases = resultKeyPhrases.Result.Documents[0].KeyPhrases.ToArray();

                // COGNITIVE SERVICE : Détection du score de sentiment
                var resultSentiment = client.SentimentAsync(new MultiLanguageBatchInput(
                                                                new List <MultiLanguageInput>()
                {
                    new MultiLanguageInput(result.LanguageIso, "0", text)
                }));

                result.ScoreSentiment = resultSentiment.Result.Documents[0].Score;

                return(result);
            }
            catch (Exception ex)
            {
                _logger.LogError($"Text analyze : {ex.Message}");
                return(null);
            }
        }
コード例 #7
0
        public async Task <IEnumerable <Feedback> > GetFeedback()
        {
            var data = await LoadFeedback();

            var inputs = data.Select(f => new MultiLanguageInput("en", f.Key, f.Value.Text)).ToList();

            var result = await _textAnalyticsAPI.SentimentAsync(new MultiLanguageBatchInput(inputs));

            foreach (var document in result.Documents)
            {
                data[document.Id].Score = document.Score;
            }

            return(data.Values);
        }
コード例 #8
0
        public async Task <double?> AnalyzeSentiment(string input)
        {
            var client = new TextAnalyticsAPI();

            client.AzureRegion     = AzureRegions.Westus;
            client.SubscriptionKey = "7003dfb3f5484db4829a98c046cef71b";
            var langInput  = new MultiLanguageInput("en", "1", input);
            var batchInput = new MultiLanguageBatchInput(new List <MultiLanguageInput> {
                langInput
            });
            var sentiment = await client.SentimentAsync(batchInput);

            var result = sentiment.Documents.Single();

            return(result.Score);
        }
コード例 #9
0
        public async Task <double?> AnalyzeTextAsync(string text, string isoLanguage)
        {
            ITextAnalyticsAPI client = new TextAnalyticsAPI(_apiKeyServiceClientCredentialsFactory.GetServiceClientCredentials());

            client.AzureRegion = AzureRegions.Eastus;

            var result = await client.SentimentAsync(new MultiLanguageBatchInput
            {
                Documents = new List <MultiLanguageInput>
                {
                    new MultiLanguageInput(id: Guid.NewGuid().ToString(), text: text, language: isoLanguage) // Id is nullable, but it's required!
                }
            });

            return(result.Documents.First().Score);
        }
コード例 #10
0
        public static async Task <double?> obtainSentiment(string key, AzureRegions region, string text)
        {
            TextAnalyticsAPI client = new TextAnalyticsAPI(new ApiKeyServiceClientCredentials());

            client.AzureRegion = region;

            MultiLanguageBatchInput input = new MultiLanguageBatchInput(new List <MultiLanguageInput>()
            {
                new MultiLanguageInput("en", "1", text)
            });
            var result = await client.SentimentAsync(input);

            var score = result.Documents.FirstOrDefault().Score;

            //var tostring = Convert.ToString(score);
            return(score);
        }
        public static async Task <string> ObtainSentiment(string key, AzureRegions region, string text)
        {
            ITextAnalyticsAPI client = new TextAnalyticsAPI();

            client.SubscriptionKey = key;
            client.AzureRegion     = region;

            MultiLanguageBatchInput input = new MultiLanguageBatchInput(new List <MultiLanguageInput>()
            {
                new MultiLanguageInput("en", "1", text)
            });
            var result = await client.SentimentAsync(input);

            var score = result.Documents.FirstOrDefault()?.Score;

            return($"Score for {text} is **{score}**");
        }
コード例 #12
0
        private static void ProcessSentiment(string documentid, string text)
        {
            ITextAnalyticsAPI client = new TextAnalyticsAPI(new ApiKeyServiceClientCredentials("your key here"));

            client.AzureRegion = AzureRegions.Westeurope;

            SentimentBatchResult results = client.SentimentAsync(
                new MultiLanguageBatchInput(
                    new List <MultiLanguageInput>()
            {
                new MultiLanguageInput("en", documentid + ":" + text, text),
            })).Result;

            foreach (var document in results.Documents)
            {
                Console.WriteLine("Document ID: {0} , Sentiment Score: {1:0.00}", document.Id, document.Score);
            }
        }
コード例 #13
0
        private static async Task <double?> GetScore(string text)
        {
            using (ITextAnalyticsAPI client = new TextAnalyticsAPI
            {
                SubscriptionKey = ConfigurationManager.AppSettings["CognitiveKey"],
                AzureRegion = AzureRegions.Eastus2
            })
            {
                var result = await client.SentimentAsync(
                    new MultiLanguageBatchInput(
                        new List <MultiLanguageInput>()
                {
                    new MultiLanguageInput(id: "1", text: ScrubHtml(text)),
                }));

                return(result.Documents.First().Score);
            }
        }
コード例 #14
0
        public static async Task <List <TweetSentiment> > AnalyzeAsync(List <TweetSentiment> inputList, string incomingApiKey)
        {
            ITextAnalyticsAPI client = new TextAnalyticsAPI(new ApiKeyServiceClientCredentials(incomingApiKey));

            client.AzureRegion = AzureRegions.Westeurope;

            if (inputList.Count() >= 100)
            {
                List <MultiLanguageInput> multiLanguageInputs = ProcessToProperList(inputList);

                try
                {
                    SentimentBatchResult batchResult  = client.SentimentAsync(new MultiLanguageBatchInput(multiLanguageInputs)).Result;
                    KeyPhraseBatchResult batchResult2 = client.KeyPhrasesAsync(new MultiLanguageBatchInput(multiLanguageInputs)).Result;

                    foreach (var d in batchResult.Documents)
                    {
                        var tweet = inputList.Find(i => i.Id == d.Id);
                        tweet.Score = d.Score;
                    }

                    foreach (var d in batchResult2.Documents)
                    {
                        var tweet = inputList.Find(i => i.Id == d.Id);
                        tweet.KeyPhrases = String.Join(",", d.KeyPhrases);
                    }
                }

                catch (Exception ex)
                {
                    throw new Exception(ex.InnerException.ToString());
                }
            }

            else
            {
                inputList.ForEach(delegate(TweetSentiment action)
                {
                    action.Score = 0.5;
                });
            }

            return(inputList);
        }
コード例 #15
0
        public static async Task <double> GetTextSentiment(string input)
        {
            var client = new TextAnalyticsAPI
            {
                AzureRegion     = AzureRegions.Westus,
                SubscriptionKey = ConfigurationManager.AppSettings["TextAnalyticsKey"]
            };

            var language = await client.DetectLanguageAsync(new BatchInput(
                                                                new[] { new Input(id: "0", text: input) }
                                                                ));

            var langCode = language.Documents[0].DetectedLanguages[0].Iso6391Name;

            var sentiment = await client.SentimentAsync(new MultiLanguageBatchInput(
                                                            new[] { new MultiLanguageInput(id: "0", language: langCode, text: input) }
                                                            ));

            return(sentiment.Documents[0].Score.GetValueOrDefault());
        }
        public static async Task <TextAnalyticsResults> AnalyseSentiment(string input)
        {
            var client = new TextAnalyticsAPI
            {
                AzureRegion     = AzureRegions.Westus,
                SubscriptionKey = ConfigurationManager.AppSettings["TextAnalyticsSubscriptionKey"]
            };



            var language = client.DetectLanguage(
                new BatchInput(
                    new List <Input>()
            {
                new Input("1", input)
            }));

            var result = new TextAnalyticsResults
            {
                LanguageName        = language.Documents[0].DetectedLanguages[0].Name,
                LanguageIso6391Name = language.Documents[0].DetectedLanguages[0].Iso6391Name
            };


            var keyPhrases = await client.KeyPhrasesAsync(new MultiLanguageBatchInput(
                                                              new[] { new MultiLanguageInput(language: result.LanguageIso6391Name, id: "1", text: input) }));

            result.KeyPhrases = keyPhrases.Documents[0].KeyPhrases;

            var sentiment = await client.SentimentAsync(new MultiLanguageBatchInput(
                                                            new[] { new MultiLanguageInput(language: result.LanguageIso6391Name, id: "1", text: input) }));

            result.Score = sentiment.Documents[0].Score;



            return(result);
        }
コード例 #17
0
        private static int GetScore(string message)
        {
            ITextAnalyticsAPI client = new TextAnalyticsAPI(new ApiKeyServiceClientCredentials())
            {
                AzureRegion = AzureRegions.Westus2
            };

            var results = client.SentimentAsync(
                new MultiLanguageBatchInput(
                    new List <MultiLanguageInput>()
            {
                new MultiLanguageInput("en", "0", message)
            })).Result;

            if (results.Documents.Count == 0)
            {
                return(0);
            }
            var score      = results.Documents[0].Score.GetValueOrDefault();
            var fixedScore = (int)(score * 100);

            return(fixedScore);
        }
コード例 #18
0
        public double textAnalyse(string s)
        {
            double ans = 0;

            ITextAnalyticsAPI client = new TextAnalyticsAPI(new ApiKeyServiceClientCredentials());

            client.AzureRegion = AzureRegions.Westcentralus;


            SentimentBatchResult result3 = client.SentimentAsync(
                new MultiLanguageBatchInput(
                    new List <MultiLanguageInput>()
            {
                new MultiLanguageInput("en", "0", s),
            })).Result;


            foreach (var document in result3.Documents)
            {
                ans += (double)document.Score;
            }
            return(ans);
        }
コード例 #19
0
        public async Task <TextAnalysisResult> AnalyzeTextAsync(string text)
        {
            // See: https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/quickstarts/csharp

            var analysisResult = new TextAnalysisResult();

            if (string.IsNullOrEmpty(text))
            {
                Console.WriteLine("\t\t\tContentAnalyzer.AnalyzeTextAsync(): no text to analyze");
                return(analysisResult);
            }

            string textToAnalyze = text;

            if (text.Length > 5000)
            {
                Console.WriteLine("\t\t\tContentAnalyzer.AnalyzeTextAsync(): text longer than supported length. Trimming it...");
                textToAnalyze = text.Substring(0, 5000);
            }

            Console.WriteLine("\t\t\tContentAnalyzer.AnalyzeTextAsync(): initializing TextAnalyticsAPI");

            ITextAnalyticsAPI m_TextAnalyticsClient = new TextAnalyticsAPI
            {
                AzureRegion     = AzureRegions.Westeurope,
                SubscriptionKey = m_TextAnalyticsAPISubscriptionKey
            };

            Console.WriteLine("\t\t\tContentAnalyzer.AnalyzeTextAsync(): detecting content language");

            var batchLanguageResult = await m_TextAnalyticsClient.DetectLanguageAsync(new BatchInput(new List <Input>()
            {
                new Input("1", textToAnalyze)
            })).ConfigureAwait(false);

            if (batchLanguageResult.Errors.Count > 0)
            {
                Console.WriteLine("\t\t\tContentAnalyzer.AnalyzeTextAsync(): error while detecting language");
                foreach (var errors in batchLanguageResult.Errors)
                {
                    Console.WriteLine($"\t{errors.Message}");
                }
                return(analysisResult);
            }

            analysisResult.DetectedLanguage      = batchLanguageResult.Documents[0].DetectedLanguages[0].Name;
            analysisResult.DetectedLanguageScore = batchLanguageResult.Documents[0].DetectedLanguages[0].Score.GetValueOrDefault();

            Console.WriteLine($"\t\t\tContentAnalyzer.AnalyzeTextAsync(): detected language is '{analysisResult.DetectedLanguage}' ({(analysisResult.DetectedLanguageScore * 100):0.00}%)");

            Console.WriteLine("\t\t\tContentAnalyzer.AnalyzeTextAsync(): performing key-phrase extraction");

            var multiLanguageInput = new MultiLanguageBatchInput(new List <MultiLanguageInput>()
            {
                new MultiLanguageInput(batchLanguageResult.Documents[0].DetectedLanguages[0].Iso6391Name, "1", textToAnalyze)
            });
            var batchKeyphraseResult = await m_TextAnalyticsClient.KeyPhrasesAsync(multiLanguageInput).ConfigureAwait(false);

            if (batchKeyphraseResult.Errors.Count > 0)
            {
                Console.WriteLine("\t\t\tContentAnalyzer.AnalyzeTextAsync(): error while extracting key-phrases");
                foreach (var errors in batchKeyphraseResult.Errors)
                {
                    Console.WriteLine($"\t\t\t\t{errors.Message}");
                }
                return(analysisResult);
            }

            Console.WriteLine($"\t\t\tContentAnalyzer.AnalyzeTextAsync(): retrieved {batchKeyphraseResult.Documents[0].KeyPhrases.Count} key-phrases:");
            foreach (var keyphrase in batchKeyphraseResult.Documents[0].KeyPhrases)
            {
                analysisResult.KeyPhrases.Add(keyphrase);
                Console.WriteLine($"\t\t\t\t{keyphrase}");
            }

            Console.WriteLine("\t\t\tContentAnalyzer.AnalyzeTextAsync(): performing sentiment analysis");

            var batchSentimentResult = await m_TextAnalyticsClient.SentimentAsync(multiLanguageInput).ConfigureAwait(false);

            if (batchSentimentResult.Errors.Count > 0)
            {
                Console.WriteLine("\t\t\tContentAnalyzer.AnalyzeTextAsync(): error while detecting sentiment");
                foreach (var errors in batchSentimentResult.Errors)
                {
                    Console.WriteLine($"\t\t\t\t{errors.Message}");
                }
                return(analysisResult);
            }

            analysisResult.SentimentScore = batchSentimentResult.Documents[0].Score.GetValueOrDefault();
            analysisResult.Sentiment      = GetSentiment(analysisResult.SentimentScore);

            Console.WriteLine($"\t\t\tContentAnalyzer.AnalyzeTextAsync(): sentiment is '{analysisResult.Sentiment}' ({(analysisResult.SentimentScore * 100):0.00}%)");

            // Extend analysis by estimating reading time for content
            analysisResult.WordCount            = TextTokenizer.GetWordCount(text);
            analysisResult.ReadingTimeInMinutes = ReadingTimeEstimator.GetEstimatedReadingTime(analysisResult.WordCount, analysisResult.DetectedLanguage);

            return(analysisResult);
        }
コード例 #20
0
ファイル: Program.cs プロジェクト: dannradkov/TweetAnalysis
        static void Main(string[] args)
        {
            //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"];

            // https://github.com/linvi/tweetinvi/wiki/Streams
            Auth.SetUserCredentials(oauthCustomerKey, oauthConsumerSecret, oauthToken, oauthTokenSecret);

            var stream = Stream.CreateFilteredStream();

            stream.AddTrack("beer");
            stream.AddTweetLanguageFilter(LanguageFilter.English);

            // Create client for CA
            ITextAnalyticsAPI client = new TextAnalyticsAPI(new ApiKeyServiceClientCredentials())
            {
                AzureRegion = AzureRegions.Westeurope
            };

            stream.MatchingTweetReceived += (sender, targs) =>
            {
                var tweet = targs.Tweet;
                Console.WriteLine("New {0} Beer Tweet: {1}", tweet.Language, tweet.Text);

                // Get Language
                var result = client.DetectLanguageAsync(new BatchInput(new List <Input> {
                    new Input(tweet.Id.ToString(), tweet.Text)
                })).Result;
                foreach (var document in result.Documents)
                {
                    Console.WriteLine("Document ID: {0} , Language: {1}", document.Id, document.DetectedLanguages[0].Name);

                    // Get Phrases
                    var inputTweet = new List <MultiLanguageInput>()
                    {
                        new MultiLanguageInput(document.DetectedLanguages[0].Iso6391Name, document.Id, tweet.Text)
                    };
                    KeyPhraseBatchResult result2 = client.KeyPhrasesAsync(new MultiLanguageBatchInput(inputTweet)).Result;

                    foreach (var document2 in result2.Documents)
                    {
                        Console.WriteLine("Document ID: {0} ", document2.Id);
                        Console.WriteLine("\t Key phrases:");
                        foreach (string keyphrase in document2.KeyPhrases)
                        {
                            Console.WriteLine("\t\t" + keyphrase);
                        }
                    }

                    // Get Sentiment
                    SentimentBatchResult result3 = client.SentimentAsync(new MultiLanguageBatchInput(inputTweet)).Result;
                    foreach (var document3 in result3.Documents)
                    {
                        Console.WriteLine("Document ID: {0} , Sentiment Score: {1:0.00}", document3.Id, document3.Score);
                    }

                    // Get Named Entities
                    var result4 = client.EntitiesAsync(new MultiLanguageBatchInput(inputTweet)).Result;
                    foreach (var document4 in result4.Documents)
                    {
                        Console.WriteLine("Entities found:");
                        foreach (var entity in document4.Entities)
                        {
                            Console.WriteLine("Entity Name: {0}", entity.Name);
                        }
                    }
                }
            };

            stream.StartStreamMatchingAllConditions();
        }
コード例 #21
0
        static void Main(string[] args)
        {
            //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"];

            // https://github.com/linvi/tweetinvi/wiki/Streams
            Auth.SetUserCredentials(oauthCustomerKey, oauthConsumerSecret, oauthToken, oauthTokenSecret);

            // Create a stream for the requested keyword
            var stream = Stream.CreateFilteredStream();
            var tracks = keywords.Split(',');

            foreach (var keyword in tracks)
            {
                stream.AddTrack(keyword);
            }

            // Create client for CA
            ITextAnalyticsAPI client = new TextAnalyticsAPI(new ApiKeyServiceClientCredentials())
            {
                AzureRegion = AzureRegions.Westeurope
            };

            stream.MatchingTweetReceived += (sender, targs) =>
            {
                var tweet = targs.Tweet;
                Console.WriteLine("New Tweet: {0}", tweet.Text);

                var tweetRow = new TweetWithAnalysis()
                {
                    TweetAuthor              = tweet.CreatedBy.Name,
                    TweetAuthorCreatedAt     = tweet.CreatedBy.CreatedAt,
                    TweetAuthorFollowerCount = tweet.CreatedBy.FollowersCount,
                    TweetAuthorLocation      = tweet.CreatedBy.Location ?? "Unknown",
                    TweetCreatedAt           = tweet.CreatedAt,
                    TweetFavoriteCount       = tweet.FavoriteCount,
                    TweetGeoCountryCode      = (tweet.Place == null) ? "" : tweet.Place.CountryCode,
                    TweetGeoFullName         = (tweet.Place == null) ? "Unknown" : tweet.Place.FullName,
                    TweetRetweetCount        = tweet.RetweetCount,
                    TweetText          = tweet.Text,
                    TweetUrl           = tweet.Url,
                    TweetNamedEntities = ""
                };

                List <string> entityHashtags = new List <string>();
                foreach (var entity in tweet.Entities.Hashtags)
                {
                    entityHashtags.Add(entity.Text);
                }
                tweetRow.TweetHashtags = string.Join(", ", entityHashtags);

                List <string> mediaUrls = new List <string>();
                foreach (var media in tweet.Media)
                {
                    mediaUrls.Add(media.MediaURL);
                }
                tweetRow.TweetMediaUrls = string.Join(", ", mediaUrls);

                // Get Language
                var result = client.DetectLanguageAsync(new BatchInput(new List <Input> {
                    new Input(tweet.Id.ToString(), tweet.Text)
                })).Result;
                foreach (var document in result.Documents)
                {
                    Console.WriteLine("Document ID: {0} , Language: {1}", document.Id, document.DetectedLanguages[0].Name);
                    tweetRow.TweetLanguageCode = document.DetectedLanguages[0].Iso6391Name;

                    // Get Phrases
                    var inputTweet = new List <MultiLanguageInput>()
                    {
                        new MultiLanguageInput(document.DetectedLanguages[0].Iso6391Name, document.Id, tweet.Text)
                    };
                    KeyPhraseBatchResult result2 = client.KeyPhrasesAsync(new MultiLanguageBatchInput(inputTweet)).Result;

                    foreach (var document2 in result2.Documents)
                    {
                        string keyPhrases = string.Join(", ", document2.KeyPhrases);
                        Console.WriteLine("Key phrases: {0}", keyPhrases);
                        tweetRow.TweetKeyPhrases = keyPhrases;
                    }

                    // Get Sentiment
                    SentimentBatchResult result3 = client.SentimentAsync(new MultiLanguageBatchInput(inputTweet)).Result;
                    foreach (var document3 in result3.Documents)
                    {
                        Console.WriteLine("Sentiment Score: {0:0.00}", document3.Score);
                        tweetRow.TweetSentiment = document3.Score;
                    }

                    // Get Named Entities
                    var result4 = client.EntitiesAsync(new MultiLanguageBatchInput(inputTweet)).Result;
                    foreach (var document4 in result4.Documents)
                    {
                        List <string> entityNames = new List <string>();
                        foreach (var entity in document4.Entities)
                        {
                            entityNames.Add(entity.Name);
                        }

                        string entities = string.Join(", ", entityNames);
                        Console.WriteLine("Entities: {0}", entities);
                        tweetRow.TweetNamedEntities = entities;
                        Console.WriteLine("Entities found: {0}", entities);
                    }
                }

                Console.WriteLine(tweetRow.ToJson());

                string conn = "Data Source=localhost;Initial Catalog=WorldCup;Integrated Security=True";
                using (SqlConnection connection = new SqlConnection(conn))
                {
                    string sqlCommand = "INSERT into TweetInfo (TweetCreatedAt, TweetText, TweetAuthor, " +
                                        "TweetAuthorLocation, TweetAuthorFollowerCount, TweetAuthorCreatedAt," +
                                        "TweetGeoFullName, TweetGeoCountryCode, TweetRetweetCount, " +
                                        "TweetFavoriteCount, TweetHashtags, TweetUrl, TweetMediaUrls, " +
                                        "TweetLanguageCode, TweetSentiment, TweetKeyPhrases, TweetNamedEntities)" +
                                        " VALUES (@TweetCreatedAt, @TweetText, @TweetAuthor, " +
                                        "@TweetAuthorLocation, @TweetAuthorFollowerCount, @TweetAuthorCreatedAt," +
                                        "@TweetGeoFullName, @TweetGeoCountryCode, @TweetRetweetCount, " +
                                        "@TweetFavoriteCount, @TweetHashtags, @TweetUrl, @TweetMediaUrls, " +
                                        "@TweetLanguageCode, @TweetSentiment, @TweetKeyPhrases, @TweetNamedEntities)";

                    using (SqlCommand command = new SqlCommand(sqlCommand))
                    {
                        command.Connection = connection;
                        command.Parameters.AddWithValue("@TweetCreatedAt", tweetRow.TweetCreatedAt);
                        command.Parameters.AddWithValue("@TweetText", tweetRow.TweetText);
                        command.Parameters.AddWithValue("@TweetAuthor", tweetRow.TweetAuthor);
                        command.Parameters.AddWithValue("@TweetAuthorLocation", tweetRow.TweetAuthorLocation);
                        command.Parameters.AddWithValue("@TweetAuthorFollowerCount", tweetRow.TweetAuthorFollowerCount);
                        command.Parameters.AddWithValue("@TweetAuthorCreatedAt", tweetRow.TweetAuthorCreatedAt);
                        command.Parameters.AddWithValue("@TweetGeoFullName", tweetRow.TweetGeoFullName);
                        command.Parameters.AddWithValue("@TweetGeoCountryCode", tweetRow.TweetGeoCountryCode);
                        command.Parameters.AddWithValue("@TweetRetweetCount", tweetRow.TweetRetweetCount);
                        command.Parameters.AddWithValue("@TweetFavoriteCount", tweetRow.TweetFavoriteCount);
                        command.Parameters.AddWithValue("@TweetHashtags", tweetRow.TweetHashtags);
                        command.Parameters.AddWithValue("@TweetUrl", tweetRow.TweetUrl);
                        command.Parameters.AddWithValue("@TweetMediaUrls", tweetRow.TweetMediaUrls);
                        command.Parameters.AddWithValue("@TweetLanguageCode", tweetRow.TweetLanguageCode);
                        command.Parameters.AddWithValue("@TweetSentiment", tweetRow.TweetSentiment);
                        command.Parameters.AddWithValue("@TweetKeyPhrases", tweetRow.TweetKeyPhrases);
                        command.Parameters.AddWithValue("@TweetNamedEntities", tweetRow.TweetNamedEntities);

                        connection.Open();
                        int recordsAffected = command.ExecuteNonQuery();
                    }
                };
            };

            // Start the stream but only if all tracks are found
            stream.StartStreamMatchingAllConditions();
        }
コード例 #22
0
        static void Main(string[] args)
        {
            // Create a client.
            //ITextAnalyticsClient client = new TextAnalyticsClient(new ApiKeyServiceClientCredentials())
            //{
            //    Endpoint = "https://eastus.api.cognitive.microsoft.com"
            //};
            TextAnalyticsAPI client = new TextAnalyticsAPI(new ApiKeyServiceClientCredentials())
            {
                AzureRegion = AzureRegions.Eastus2 // example
            };

            Console.OutputEncoding = System.Text.Encoding.UTF8;

            // Extracting language
            Console.WriteLine("===== LANGUAGE EXTRACTION ======");

            var result = client.DetectLanguageAsync(new BatchInput(
                                                        new List <Input>()
            {
                new Input("1", "This is a document written in English."),
                new Input("2", "Este es un document escrito en Español."),
                new Input("3", "这是一个用中文写的文件")
            })).Result;

            // Printing language results.
            foreach (var document in result.Documents)
            {
                Console.WriteLine("Document ID: {0} , Language: {1}", document.Id, document.DetectedLanguages[0].Name);
            }

            // Getting key-phrases
            Console.WriteLine("\n\n===== KEY-PHRASE EXTRACTION ======");

            KeyPhraseBatchResult result2 = client.KeyPhrasesAsync(new MultiLanguageBatchInput(
                                                                      new List <MultiLanguageInput>()
            {
                new MultiLanguageInput("ja", "1", "猫は幸せ"),
                new MultiLanguageInput("de", "2", "Fahrt nach Stuttgart und dann zum Hotel zu Fu."),
                new MultiLanguageInput("en", "3", "My cat is stiff as a rock."),
                new MultiLanguageInput("es", "4", "A mi me encanta el fútbol!")
            })).Result;

            // Printing keyphrases
            foreach (var document in result2.Documents)
            {
                Console.WriteLine("Document ID: {0} ", document.Id);

                Console.WriteLine("\t Key phrases:");

                foreach (string keyphrase in document.KeyPhrases)
                {
                    Console.WriteLine("\t\t" + keyphrase);
                }
            }

            // Extracting sentiment
            Console.WriteLine("\n\n===== SENTIMENT ANALYSIS ======");

            SentimentBatchResult result3 = client.SentimentAsync(
                new MultiLanguageBatchInput(
                    new List <MultiLanguageInput>()
            {
                new MultiLanguageInput("en", "0", "I had the best day of my life."),
                new MultiLanguageInput("en", "1", "This was a waste of my time. The speaker put me to sleep."),
                new MultiLanguageInput("es", "2", "No tengo dinero ni nada que dar..."),
                new MultiLanguageInput("it", "3", "L'hotel veneziano era meraviglioso. È un bellissimo pezzo di architettura."),
            })).Result;


            // Printing sentiment results
            foreach (var document in result3.Documents)
            {
                Console.WriteLine("Document ID: {0} , Sentiment Score: {1:0.00}", document.Id, document.Score);
            }

            Console.ReadLine();
        }
コード例 #23
0
        static void Main(string[] args)
        {
            // Create a client.
            TextAnalyticsAPI client = new TextAnalyticsAPI(new ApiKeyServiceClientCredentials())
            {
                AzureRegion = AzureRegions.Eastus
            };

            Console.OutputEncoding = System.Text.Encoding.UTF8;

            Console.WriteLine("===== MS_CA_TEXT_ANALYSIS ======");
            Console.WriteLine("==== KEY PHRASE & SENTIMENT ====");
            Console.WriteLine("Type 'quit' to exit application");

            string text;

            do
            {
                Console.WriteLine("\nText to Analyze:");
                text = Console.ReadLine();

                //** GATHER (POST)
                //Key Phrase(s)
                KeyPhraseBatchResult result_k = client.KeyPhrasesAsync(new MultiLanguageBatchInput(
                                                                           new List <MultiLanguageInput>()
                {
                    new MultiLanguageInput("en", "3", text),
                })).Result;

                //Sentiment
                SentimentBatchResult result_s = client.SentimentAsync(new MultiLanguageBatchInput(
                                                                          new List <MultiLanguageInput>()
                {
                    new MultiLanguageInput("en", "3", text),
                })).Result;

                //Entities
                EntitiesBatchResult result_e = client.EntitiesAsync(new MultiLanguageBatchInput(
                                                                        new List <MultiLanguageInput>()
                {
                    new MultiLanguageInput("en", "3", text),
                })).Result;

                //** PRINT (RESPONSE)
                //Sentiment
                foreach (var document in result_s.Documents)
                {
                    Console.WriteLine("\n Sentiment: ");
                    Console.WriteLine("\t " + Math.Round(document.Score.Value, 3, MidpointRounding.AwayFromZero));
                }

                //Key Phrase(s)
                foreach (var document in result_k.Documents)
                {
                    Console.WriteLine("\n Key Phrases:");
                    foreach (string keyphrase in document.KeyPhrases)
                    {
                        Console.WriteLine("\t " + keyphrase);
                    }
                }

                //Entities
                foreach (var document in result_e.Documents)
                {
                    Console.WriteLine("\n Entities:");
                    foreach (var entity in document.Entities)
                    {
                        Console.WriteLine("\t " + entity.Name + " (" + entity.WikipediaUrl + ")");
                    }
                }
            } while (text != "quit");
        }