Exemple #1
0
        //  keyPhraseExtraction
        static void KeyPhraseExtraction(TextAnalyticsClient client, string textSource)
        {
            var response = client.ExtractKeyPhrases(textSource);

            foreach (string keyphrase in response.Value)
            {
                Console.Write($"\t{keyphrase}");
                strKeyPhrases += keyphrase + ","; //string.Join(",", keyphrase);
                strKeyPhrases.TrimEnd(',');
            }
            Console.WriteLine();
        }
        public List <string> AnalyzeText(string text)
        {
            List <string> tags     = new List <string>();
            var           response = client.ExtractKeyPhrases(text);


            foreach (string keyphrase in response.Value)
            {
                tags.Add(keyphrase);
            }
            return(tags);
        }
Exemple #3
0
        static void KeyPhraseExtractionExample(TextAnalyticsClient client)
        {
            var response = client.ExtractKeyPhrases("My cat might need to see a veterinarian.");

            // Printing key phrases
            Console.WriteLine("Key phrases:");

            foreach (string keyphrase in response.Value)
            {
                Console.WriteLine($"\t{keyphrase}");
            }
        }
Exemple #4
0
        static void KeyPhraseExtractionExample(TextAnalyticsClient client, string inputText)
        {
            var response = client.ExtractKeyPhrases(inputText);

            // Printing key phrases
            Console.WriteLine("Key phrases:");

            foreach (string keyphrase in response.Value)
            {
                Console.WriteLine($"\t{keyphrase}");
            }
        }
Exemple #5
0
        static void KeyPhraseExtractionExample(TextAnalyticsClient client)
        {
            var response = client.ExtractKeyPhrases("Necesito Comprar un regalo para mi tia", "ES");

            // Printing key phrases
            Console.WriteLine("Key phrases:");

            foreach (string keyphrase in response.Value)
            {
                Console.WriteLine($"\t{keyphrase}");
            }
        }
        static Response <KeyPhraseCollection> KeyPhraseExtraction(TextAnalyticsClient client, string input)
        {
            var response = client.ExtractKeyPhrases(input, "ko");

            // Printing key phrases
            Debug.Print("Key phrases:");
            foreach (string keyphrase in response.Value)
            {
                Debug.Print($"\t{keyphrase}");
            }

            return(response);
        }
        public List <string> KeyPhraseExtraction(string texto)
        {
            List <string> frasesClave = new List <string>();
            var           client      = new TextAnalyticsClient(endpoint, credentials);
            var           response    = client.ExtractKeyPhrases(texto, "ES");

            Console.WriteLine("Key phrases:");

            foreach (string keyphrase in response.Value)
            {
                frasesClave.Add(keyphrase);
            }
            return(frasesClave);
        }
        static IEnumerable <string> KeyPhraseExtraction(TextAnalyticsClient client, string content)
        {
            var response = client.ExtractKeyPhrases(content);

            // Printing key phrases
            Console.WriteLine("Key phrases:");
            List <string> keyPhrases = new List <string>();

            foreach (string keyphrase in response.Value)
            {
//                Console.WriteLine($"\t{keyphrase}");
                keyPhrases.Add(keyphrase);
            }
            return(keyPhrases);
        }
        public static HashSet <string> AnalyzeText([ActivityTrigger] string text, ILogger log)
        {
            string textAnalyticsKey      = Environment.GetEnvironmentVariable("TextAnalyticsKey");
            string textAnalyticsEndpoint = Environment.GetEnvironmentVariable("TextAnalyticsEndpoint");

            var keyPhrases = new HashSet <string>();
            var client     = new TextAnalyticsClient(new Uri(textAnalyticsEndpoint), new AzureKeyCredential(textAnalyticsKey));
            var response   = client.ExtractKeyPhrases(text);

            foreach (string keyphrase in response.Value)
            {
                keyPhrases.Add(keyphrase);
            }
            return(keyPhrases);
        }
Exemple #10
0
        static string KeyPhraseExtractionExample(TextAnalyticsClient client, string text)
        {
            var    response = client.ExtractKeyPhrases(text);
            string result   = "";

            // Printing key phrases
            Console.WriteLine("Key phrases:");
            result = "Key phrases: ";
            foreach (string keyphrase in response.Value)
            {
                Console.WriteLine($"      {keyphrase}");
                result = result + $"      {keyphrase}" + ", ";
            }
            result = result + "\n\n";
            return(result);
        }
Exemple #11
0
        public List <string> KeyPhraseExtractionExample(TextAnalyticsClient client, string text)
        {
            string document = text.Replace("\r", "").Replace("\n", "");

            var response = client.ExtractKeyPhrases(document);

            // Printing key phrases
            Console.WriteLine("Key phrases:");
            var phrases = new List <string>();

            foreach (string keyphrase in response.Value)
            {
                phrases.Add(keyphrase);
                Console.WriteLine($"\t{keyphrase}");
            }

            return(phrases);
        }
        public void ExtractKeyPhrases()
        {
            string endpoint        = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_ENDPOINT");
            string subscriptionKey = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_SUBSCRIPTION_KEY");

            // Instantiate a client that will be used to call the service.
            var client = new TextAnalyticsClient(new Uri(endpoint), subscriptionKey);

            string input = "My cat might need to see a veterinarian.";

            Debug.WriteLine($"Extracting key phrases for input: \"{input}\"");
            var keyPhrases = client.ExtractKeyPhrases(input).Value;

            Debug.WriteLine($"Extracted {keyPhrases.Count()} key phrases:");
            foreach (string keyPhrase in keyPhrases)
            {
                Debug.WriteLine(keyPhrase);
            }
        }
        private List <KeyPhrase> KeyPhraseExtraction(string textInput)
        {
            List <KeyPhrase> keys = new List <KeyPhrase>();
            var response          = client.ExtractKeyPhrases(textInput);

            // Printing key phrases
            Console.WriteLine("Key phrases:");

            foreach (string keyphrase in response.Value)
            {
                KeyPhrase key = new KeyPhrase();
                Console.WriteLine($"\t{keyphrase}");
                key.Id   = keyphrase;
                key.Text = keyphrase;

                keys.Add(key);
            }

            return(keys);
        }
Exemple #14
0
        public void ExtractKeyPhrases()
        {
            string endpoint = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_ENDPOINT");
            string apiKey   = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_API_KEY");

            #region Snippet:TextAnalyticsSample3CreateClient
            var client = new TextAnalyticsClient(new Uri(endpoint), new AzureKeyCredential(apiKey));
            #endregion

            #region Snippet:ExtractKeyPhrases
            string document = "My cat might need to see a veterinarian.";

            IReadOnlyCollection <string> keyPhrases = client.ExtractKeyPhrases(document).Value;

            Console.WriteLine($"Extracted {keyPhrases.Count} key phrases:");
            foreach (string keyPhrase in keyPhrases)
            {
                Console.WriteLine(keyPhrase);
            }
            #endregion
        }
Exemple #15
0
        public void ExtractKeyPhrases()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

            #region Snippet:TextAnalyticsSample3CreateClient
            var client = new TextAnalyticsClient(new Uri(endpoint), new AzureKeyCredential(apiKey));
            #endregion

            #region Snippet:ExtractKeyPhrases
            string document = "My cat might need to see a veterinarian.";

            KeyPhraseCollection keyPhrases = client.ExtractKeyPhrases(document);

            Console.WriteLine($"Extracted {keyPhrases.Count} key phrases:");
            foreach (string keyPhrase in keyPhrases)
            {
                Console.WriteLine(keyPhrase);
            }
            #endregion
        }
        public void ExtractKeyPhrases()
        {
            string endpoint        = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_ENDPOINT");
            string subscriptionKey = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_SUBSCRIPTION_KEY");

            // Instantiate a client that will be used to call the service.
            var client = new TextAnalyticsClient(new Uri(endpoint), subscriptionKey);

            #region Snippet:ExtractKeyPhrases
            string input = "My cat might need to see a veterinarian.";

            // Extract key phrases from the input text.
            ExtractKeyPhrasesResult      result     = client.ExtractKeyPhrases(input);
            IReadOnlyCollection <string> keyPhrases = result.KeyPhrases;

            Console.WriteLine($"Extracted {keyPhrases.Count()} key phrases:");
            foreach (string keyPhrase in keyPhrases)
            {
                Console.WriteLine(keyPhrase);
            }
            #endregion
        }
Exemple #17
0
        public void ExtractKeyPhrases()
        {
            string endpoint        = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_ENDPOINT");
            string subscriptionKey = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_SUBSCRIPTION_KEY");

            #region Snippet:TextAnalyticsSample3CreateClient
            var client = new TextAnalyticsClient(new Uri(endpoint), new TextAnalyticsSubscriptionKeyCredential(subscriptionKey));
            #endregion

            #region Snippet:ExtractKeyPhrases
            string input = "My cat might need to see a veterinarian.";

            ExtractKeyPhrasesResult      result     = client.ExtractKeyPhrases(input);
            IReadOnlyCollection <string> keyPhrases = result.KeyPhrases;

            Console.WriteLine($"Extracted {keyPhrases.Count()} key phrases:");
            foreach (string keyPhrase in keyPhrases)
            {
                Console.WriteLine(keyPhrase);
            }
            #endregion
        }
Exemple #18
0
        public void ExtractKeyPhrasesWithWarnings()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

            var client = new TextAnalyticsClient(new Uri(endpoint), new AzureKeyCredential(apiKey));

            string document = @"Anthony runs his own personal training business so
                              thisisaverylongtokenwhichwillbetruncatedtoshowushowwarningsareemittedintheapi";

            try
            {
                Response <KeyPhraseCollection> response   = client.ExtractKeyPhrases(document);
                KeyPhraseCollection            keyPhrases = response.Value;

                if (keyPhrases.Warnings.Count > 0)
                {
                    Console.WriteLine("**Warnings:**");
                    foreach (TextAnalyticsWarning warning in keyPhrases.Warnings)
                    {
                        Console.WriteLine($"  Warning: Code: {warning.WarningCode}, Message: {warning.Message}");
                    }
                }

                Console.WriteLine($"Extracted {keyPhrases.Count} key phrases:");
                foreach (string keyPhrase in keyPhrases)
                {
                    Console.WriteLine($"  {keyPhrase}");
                }
            }
            catch (RequestFailedException exception)
            {
                Console.WriteLine($"Error Code: {exception.ErrorCode}");
                Console.WriteLine($"Message: {exception.Message}");
            }
        }
        public void ExtractKeyPhrasesBatch()
        {
            string endpoint        = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_ENDPOINT");
            string subscriptionKey = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_SUBSCRIPTION_KEY");

            // Instantiate a client that will be used to call the service.
            var client = new TextAnalyticsClient(new Uri(endpoint), new TextAnalyticsSubscriptionKeyCredential(subscriptionKey));

            #region Snippet:TextAnalyticsSample3ExtractKeyPhrasesBatch
            var inputs = new List <TextDocumentInput>
            {
                new TextDocumentInput("1", "Microsoft was founded by Bill Gates and Paul Allen.")
                {
                    Language = "en",
                },
                new TextDocumentInput("2", "Text Analytics is one of the Azure Cognitive Services.")
                {
                    Language = "en",
                },
                new TextDocumentInput("3", "My cat might need to see a veterinarian.")
                {
                    Language = "en",
                }
            };

            ExtractKeyPhrasesResultCollection results = client.ExtractKeyPhrases(inputs, new TextAnalyticsRequestOptions {
                IncludeStatistics = true
            });
            #endregion

            int i = 0;
            Debug.WriteLine($"Results of Azure Text Analytics \"Extract Key Phrases\" Model, version: \"{results.ModelVersion}\"");
            Debug.WriteLine("");

            foreach (ExtractKeyPhrasesResult result in results)
            {
                TextDocumentInput document = inputs[i++];

                Debug.WriteLine($"On document (Id={document.Id}, Language=\"{document.Language}\", Text=\"{document.Text}\"):");

                if (result.ErrorMessage != default)
                {
                    Debug.WriteLine($"On document (Id={document.Id}, Language=\"{document.Language}\", Text=\"{document.Text}\"):");
                }
                else
                {
                    Debug.WriteLine($"    Extracted the following {result.KeyPhrases.Count()} key phrases:");

                    foreach (string keyPhrase in result.KeyPhrases)
                    {
                        Debug.WriteLine($"        {keyPhrase}");
                    }

                    Debug.WriteLine($"    Document statistics:");
                    Debug.WriteLine($"        Character count: {result.Statistics.CharacterCount}");
                    Debug.WriteLine($"        Transaction count: {result.Statistics.TransactionCount}");
                    Debug.WriteLine("");
                }
            }

            Debug.WriteLine($"Batch operation statistics:");
            Debug.WriteLine($"    Document count: {results.Statistics.DocumentCount}");
            Debug.WriteLine($"    Valid document count: {results.Statistics.ValidDocumentCount}");
            Debug.WriteLine($"    Invalid document count: {results.Statistics.InvalidDocumentCount}");
            Debug.WriteLine($"    Transaction count: {results.Statistics.TransactionCount}");
            Debug.WriteLine("");
        }
Exemple #20
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            try
            {
                var logEntry = new LogEntry();
                var options  = new JsonSerializerOptions
                {
                    WriteIndented = false
                };
                options.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));

                //var configurationBuilder = new ConfigurationBuilder().AddUserSecrets();
                // var configuration = configurationBuilder.Build();
                var hashTags     = _configuration.GetValue <string>("HashTags").Split(",").ToList();
                var users        = _configuration.GetValue <string>("Users").Split(",").ToList();
                var blockedWords = _configuration.GetValue <string>("BlockedWords").Split(",").ToList();
                var blockedUsers = _configuration.GetValue <string>("BlockedUsers").Split(",").ToList();

                var userCredentials = new TwitterCredentials(
                    _configuration.GetValue <string>("Twitter:ApiKey"),
                    _configuration.GetValue <string>("Twitter:ApiSecret"),
                    _configuration.GetValue <string>("Twitter:AccessToken"),
                    _configuration.GetValue <string>("Twitter:AccessSecret")
                    );

                var textAnalyticsClient = new TextAnalyticsClient(
                    new Uri(_configuration.GetValue <string>("Azure:TextAnalyticsURI")),
                    new AzureKeyCredential(_configuration.GetValue <string>("Azure:TextAnalyticsKey"))
                    );
                var userClient = new TwitterClient(userCredentials);
                var stream     = userClient.Streams.CreateFilteredStream();
                stream.AddLanguageFilter(LanguageFilter.English);
                stream.FilterLevel = StreamFilterLevel.Low;
                foreach (var hashTag in hashTags)
                {
                    stream.AddTrack(hashTag);
                }
                foreach (var user in users)
                {
                    var twitterUser = await userClient.Users.GetUserAsync(user);

                    stream.AddFollow(twitterUser);
                }

                stream.MatchingTweetReceived += (sender, eventReceived) =>
                {
                    ITweet tweet         = eventReceived.Tweet;
                    string textToAnalyze = tweet.FullText ?? tweet.Text;
                    foreach (var blockedWord in blockedWords)
                    {
                        if (textToAnalyze.ToLower().Contains(blockedWord.ToLower()))
                        {
                            return;
                        }
                    }
                    if (blockedUsers.Contains(tweet.CreatedBy.ScreenName))
                    {
                        return;
                    }
                    foreach (var blockedWord in blockedWords)
                    {
                        if (textToAnalyze.ToLower().Contains(blockedWord.ToLower()))
                        {
                            return;
                        }
                    }
                    if (eventReceived.Tweet.IsRetweet)
                    {
                        return;
                    }
                    if (eventReceived.Tweet.CreatedBy.CreatedAt > DateTime.Now.AddMonths(-1))
                    {
                        return;
                    }
                    if (eventReceived.Tweet.CreatedBy.FollowersCount < 100)
                    {
                        return;
                    }
                    if (eventReceived.MatchingFollowers.Length > 0 && eventReceived.MatchingFollowers.Contains(tweet.CreatedBy.Id) == false)
                    {
                        return;
                    }


                    //_logger.LogInformation("Matching tweet: {time}, {text}", DateTimeOffset.Now, textToAnalyze.Replace(Environment.NewLine,""));
                    var connStr        = _configuration.GetConnectionString("DefaultConnection");
                    var optionsBuilder = new DbContextOptionsBuilder <ApplicationDbContext>();
                    optionsBuilder.UseSqlServer(connStr);
                    hashTags = _configuration.GetValue <string>("HashTags").Split(",").ToList();
                    foreach (var hashTag in hashTags)
                    {
                        textToAnalyze = textToAnalyze.Replace(hashTag, "");
                    }

                    DocumentSentiment      documentSentiment = null;
                    TweetSentiment         tweetSentiment    = new TweetSentiment();
                    List <SentimentDetail> sentimentDetails  = new List <SentimentDetail>();
                    List <TweetEntity>     listEntities      = new List <TweetEntity>();
                    List <TweetKeyPhrase>  listKeyPhrases    = new List <TweetKeyPhrase>();


                    //_logger.LogInformation("Analyzing sentiment: {time}", DateTimeOffset.Now);
                    documentSentiment  = textAnalyticsClient.AnalyzeSentiment(textToAnalyze);
                    logEntry.Sentiment = JsonSerializer.Serialize(documentSentiment, options);
                    //_logger.LogInformation("Sentiment: {time}", documentSentiment.Sentiment);
                    tweetSentiment = new TweetSentiment
                    {
                        IsPositive   = (eventReceived.MatchingFollowers.Contains(tweet.CreatedBy.Id) && documentSentiment.Sentiment != TextSentiment.Negative) || (documentSentiment.Sentiment == TextSentiment.Positive) && (!documentSentiment.Sentences.Where(s => s.Sentiment == TextSentiment.Mixed || s.Sentiment == TextSentiment.Negative).Any()),
                        TweetContent = textToAnalyze,
                        TweetedBy    = 0,
                        TweetedOn    = DateTime.Now,
                        TweetID      = tweet.Id
                    };

                    foreach (var sentence in documentSentiment.Sentences)
                    {
                        var sentimentDetail = new SentimentDetail()
                        {
                            Sentence  = sentence.Text,
                            Positive  = sentence.ConfidenceScores.Positive,
                            Negative  = sentence.ConfidenceScores.Negative,
                            Neutral   = sentence.ConfidenceScores.Neutral,
                            TweetID   = tweet.Id,
                            Sentiment = sentence.Sentiment.ToString()
                        };
                        sentimentDetails.Add(sentimentDetail);
                    }
                    logEntry.Details = JsonSerializer.Serialize(sentimentDetails, options);

                    var responseEntities = textAnalyticsClient.RecognizeEntities(textToAnalyze);
                    foreach (var entity in responseEntities.Value)
                    {
                        var tweetEntity = new TweetEntity
                        {
                            EntityText  = entity.Text,
                            Category    = entity.Category.ToString(),
                            SubCategory = entity.SubCategory,
                            Confidence  = entity.ConfidenceScore,
                            TweetID     = tweet.Id
                        };
                        listEntities.Add(tweetEntity);
                    }
                    logEntry.Entities = JsonSerializer.Serialize(listEntities);

                    var responseKeyPhrases = textAnalyticsClient.ExtractKeyPhrases(textToAnalyze);
                    foreach (string keyphrase in responseKeyPhrases.Value)
                    {
                        var tweetKeyPhrase = new TweetKeyPhrase
                        {
                            TweetID   = tweet.Id,
                            KeyPhrase = keyphrase
                        };
                        listKeyPhrases.Add(tweetKeyPhrase);
                    }
                    logEntry.Phrases = JsonSerializer.Serialize(listKeyPhrases, options);


                    using (ApplicationDbContext db = new ApplicationDbContext(optionsBuilder.Options))
                    {
                        //_logger.LogWarning("Saving tweet: {time}", DateTimeOffset.Now);
                        db.TweetSentiments.Add(tweetSentiment);
                        db.SentimentDetails.AddRange(sentimentDetails);
                        db.TweetEntities.AddRange(listEntities);
                        db.TweetKeyPhrases.AddRange(listKeyPhrases);
                        db.SaveChanges();
                    }

                    if (tweetSentiment.IsPositive)
                    {
                        eventReceived.Tweet.FavoriteAsync();
                        eventReceived.Tweet.PublishRetweetAsync();
                    }

                    _logger.LogInformation(@$ "{logEntry.Sentiment} {logEntry.Details} {logEntry.Entities} {logEntry.Phrases}");
                };

                stream.StreamStopped += (sender, eventReceived) =>
                {
                    stream.StartMatchingAnyConditionAsync();
                };
                _ = stream.StartMatchingAnyConditionAsync();
                while (!stoppingToken.IsCancellationRequested)
                {
                    await Task.Delay(1000, stoppingToken);
                }
            }
            catch (OperationCanceledException)
            {
                _logger.LogWarning("Worker process was cancelled");

                Environment.ExitCode = 0;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Worker process caught exception");

                Environment.ExitCode = 0;
            }
            finally
            {
                // No matter what happens (success or exception), we need to indicate that it's time to stop the application.
                applicationLifetime.StopApplication();
            }
        }