// Pruning QnA input via Text Analytics for better responses
        public StringBuilder TextAnalyticsRequest(string message)
        {
            // Create client
            ITextAnalyticsAPI client = new TextAnalyticsAPI(new ApiKeyServiceClientCredentials());

            client.AzureRegion = AzureRegions.Northeurope;

            // Getting key-phrases
            var result = client.KeyPhrasesAsync(new MultiLanguageBatchInput(
                                                    new List <MultiLanguageInput>
            {
                new MultiLanguageInput("no", "1", message)
            })).Result;

            // Print / string build keyphrases
            var stringBuilder = new StringBuilder();

            foreach (var document in result.Documents)
            {
                foreach (var keyphrase in document.KeyPhrases)
                {
                    stringBuilder.Append(keyphrase + " ");
                }
            }
            return(stringBuilder);
        }
Example #2
0
        public string[] getPhrases(string para)
        {
            string[] phrases = new string[100];
            // Create a client.
            TextAnalyticsAPI client = new TextAnalyticsAPI(new ApiKeyServiceMicrosoftClientCredentials())
            {
                AzureRegion = AzureRegions.Southeastasia // example
            };

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

            KeyPhraseBatchResult result2 = client.KeyPhrasesAsync(new MultiLanguageBatchInput(
                                                                      new List <MultiLanguageInput>()
            {
                //new MultiLanguageInput("en", "1", "????"),
                //new MultiLanguageInput("en", "2", "Fahrt nach Stuttgart und dann zum Hotel zu Fu."),
                //new MultiLanguageInput("en", "3", "My cat is stiff as a rock."),
                new MultiLanguageInput("en", "1", para + ".")
            })).Result;

            // Printing keyphrases
            foreach (var document in result2.Documents)
            {
                int i = 0;
                foreach (string keyphrase in document.KeyPhrases)
                {
                    phrases[i++] = keyphrase;
                }
            }
            return(phrases);
        }
        private static void ProcessKeyPhrases(string documentid, string text)
        {
            var credentials = new ApiKeyServiceClientCredentials("key here");

            ITextAnalyticsAPI client = new TextAnalyticsAPI(new ApiKeyServiceClientCredentials("key here"));

            client.AzureRegion = AzureRegions.Westeurope;

            KeyPhraseBatchResult result2 = client.KeyPhrasesAsync(new MultiLanguageBatchInput(
                                                                      new List <MultiLanguageInput>()
            {
                new MultiLanguageInput("en", documentid + ":" + text, text)
            })).Result;

            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);
                }
            }
        }
Example #4
0
        public static string AnalizarTexto(string textoPDF, string numPagina)
        {
            string result = string.Empty;
            var    json   = "[\"\"]";
            // Create a client.
            ITextAnalyticsAPI client = new TextAnalyticsAPI(new ApiKeyServiceClientCredentials());

            client.AzureRegion = AzureRegions.Southcentralus;



            KeyPhraseBatchResult result2;

            try
            {
                // Getting key-phrases
                var length = textoPDF.Length;
                textoPDF = textoPDF.Replace(".", "");
                result2  = client.KeyPhrasesAsync(new MultiLanguageBatchInput(
                                                      new List <MultiLanguageInput>()
                {
                    new MultiLanguageInput("es", numPagina, textoPDF)
                })).Result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (result2.Documents.Count > 0)
            {
                json = RemoveSpecialCharacters(JsonConvert.SerializeObject(result2.Documents[0].KeyPhrases));
            }
            return(json);
        }
Example #5
0
        private async void button1_Click(object sender, EventArgs e)
        {
            textBox2.Clear();
            pictureBox1.Refresh();

            try
            {
                string imageFilePath = textBox1.Text;

                if (File.Exists(imageFilePath))
                {
                    pictureBox1.Load(imageFilePath);
                    // Make the REST API call.
                    textBox2.AppendText("\nWait a moment for the results to appear.");
                    await MakeOCRRequest(imageFilePath);
                }
                else
                {
                    textBox2.Text = "Invalid file path";
                }

                textBox2.AppendText("\n===== Poster - Key ======\n");

                // Create a client.
                ITextAnalyticsAPI client = new TextAnalyticsAPI(new ApiKeyServiceClientCredentials())
                {
                    AzureRegion = AzureRegions.Westcentralus
                };

                // Getting key-phrases
                KeyPhraseBatchResult result2 = client.KeyPhrasesAsync(input: new MultiLanguageBatchInput(
                                                                          documents: new List <MultiLanguageInput>()
                {
                    new MultiLanguageInput(language, "0", poster),
                })
                                                                      ).Result;
                // Printing keyphrases
                foreach (var document in result2.Documents)
                {
                    textBox2.AppendText(String.Format("\nDocument ID: {0} \n", document.Id));

                    foreach (string keyphrase in document.KeyPhrases)
                    {
                        textBox2.AppendText("  " + keyphrase);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "发生异常:" + ex, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                textBox2.Text = "上一次操作中发生了异常。";
            }
        }
Example #6
0
        static void Main(string[] args)
        {
            // Get the path and filename to process from the user.
            Console.WriteLine("Optical Character Recognition:");
            Console.Write("Enter the path to an image with text you wish to read: ");
            string imageFilePath = Console.ReadLine();

            if (File.Exists(imageFilePath))
            {
                // Make the REST API call.
                Console.WriteLine("\nWait a moment for the results to appear.\n");
                MakeOCRRequest(imageFilePath).Wait();
            }
            else
            {
                Console.WriteLine("\nInvalid file path");
            }

            Console.OutputEncoding = System.Text.Encoding.UTF8;
            Console.WriteLine("\n===== Poster - Key ======\n");

            // Create a client.
            ITextAnalyticsAPI client = new TextAnalyticsAPI(new ApiKeyServiceClientCredentials())
            {
                AzureRegion = AzureRegions.Westcentralus
            };

            // Getting key-phrases
            KeyPhraseBatchResult result2 = client.KeyPhrasesAsync(input: new MultiLanguageBatchInput(
                                                                      documents: new List <MultiLanguageInput>()
            {
                new MultiLanguageInput(language, "0", poster),
            })
                                                                  ).Result;

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

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

            Console.WriteLine("\nPress Enter to exit...");
            Console.ReadLine();
        }
        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);
            }
        }
Example #8
0
        public async Task <IList <string> > AnalyzeTextAsync(string text, string isoLanguage)
        {
            ITextAnalyticsAPI client = new TextAnalyticsAPI(_apiKeyServiceClientCredentialsFactory.GetServiceClientCredentials());

            client.AzureRegion = AzureRegions.Eastus;

            var result = await client.KeyPhrasesAsync(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?.FirstOrDefault()?.KeyPhrases);
        }
Example #9
0
        private static async Task UpdateFoundationsTags(int searchStart, int searchSize, Nest.ElasticClient client, TextAnalyticsAPI textAnalyticsClient)
        {
            var searchResponse = client.Search <Stiftung>(s => s
                                                          .From(searchStart)
                                                          .Size(searchSize)
                                                          );

            var stiftungen = searchResponse.Documents.Where(s => s.tags == null || s.tags.Length < 1).ToArray();

            if (stiftungen.Length >= 1)
            {
                var stiftungsZwecke = await GetPurposeDescriptionsAsMultiLanguageInput(textAnalyticsClient, stiftungen);

                if (stiftungsZwecke.Count < 1)
                {
                    return;
                }
                KeyPhraseBatchResult result = await textAnalyticsClient.KeyPhrasesAsync(
                    new MultiLanguageBatchInput(stiftungsZwecke)
                    );

                // Printing key phrases and writing phrases to stiftung.
                foreach (var document in result.Documents)
                {
                    Console.WriteLine("Document ID: {0} ", document.Id);

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

                    var  tags        = new List <string>();
                    Guid stiftungsId = new Guid(document.Id);

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

                    var stiftung = new Stiftung();
                    stiftung.id        = stiftungsId;
                    stiftung.tags      = tags.ToArray();
                    stiftung.timestamp = DateTime.Now;

                    client.Update <Stiftung>(new Nest.DocumentPath <Stiftung>(stiftungsId), u => u.Doc(stiftung));
                }
                await Task.Delay(30000);
            }
        }
Example #10
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);
        }
        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);
        }
Example #12
0
        public static async Task <string> obtainKeyPhrase(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.KeyPhrasesAsync(input);

            var keyphrases = result.Documents.FirstOrDefault().KeyPhrases;

            if (keyphrases.Count > 0)
            {
                return("Keyphrases: " + string.Join(", ", keyphrases));
            }
            else
            {
                return("");
            }
        }
        public static async Task <string> ObtainKeyPhrase(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.KeyPhrasesAsync(input);

            var keyphrases = result.Documents.FirstOrDefault()?.KeyPhrases;

            if (keyphrases.Count > 0)
            {
                return(string.Join(", ", keyphrases));
            }
            else
            {
                return("Not found any Keyphrases!");
            }
        }
Example #14
0
        public static string AnalizarTextoJson(string textoPDF, string numPagina, TraceWriter log)
        {
            //devuelve un json con las keyphrases
            string result = string.Empty;
            var    json   = "[\"\"]";
            // Create a client.
            ITextAnalyticsAPI client = new TextAnalyticsAPI(new ApiKeyServiceClientCredentials());

            client.AzureRegion = AzureRegions.Southcentralus;

            KeyPhraseBatchResult result2;

            try
            {
                // Getting key-phrases
                textoPDF = RemoveSpecialCharacters(textoPDF);

                result2 = client.KeyPhrasesAsync(new MultiLanguageBatchInput(
                                                     new List <MultiLanguageInput>()
                {
                    new MultiLanguageInput("es", numPagina, textoPDF)
                })).Result;
                Thread.Sleep(1000);

                if (result2.Documents.Count > 0)
                {
                    json = RemoveSpecialCharacters(JsonConvert.SerializeObject(result2.Documents[0].KeyPhrases));
                }
            }
            catch (Exception ex)
            {
                log.Info(textoPDF);
            }

            return(json);
        }
Example #15
0
        public static string AnalizarTexto(string textoPDF, string numPagina, TraceWriter log)
        {
            string result = string.Empty;
            var    json   = "";
            // Create a client.
            ITextAnalyticsAPI client = new TextAnalyticsAPI(new ApiKeyServiceClientCredentials());

            client.AzureRegion = AzureRegions.Southcentralus;

            KeyPhraseBatchResult result2;

            try
            {
                // Getting key-phrases
                textoPDF = RemoveSpecialCharacters(textoPDF);

                result2 = client.KeyPhrasesAsync(new MultiLanguageBatchInput(
                                                     new List <MultiLanguageInput>()
                {
                    new MultiLanguageInput("es", numPagina, textoPDF)
                })).Result;
                var TiempoEspera = System.Configuration.ConfigurationManager.AppSettings["CustomTrheadSleep"];
                Thread.Sleep(Convert.ToInt32(TiempoEspera));

                if (result2.Documents.Count > 0)
                {
                    json = string.Join(",", result2.Documents[0].KeyPhrases);
                }
            }
            catch (Exception ex)
            {
                log.Info(textoPDF);
            }

            return(json);
        }
Example #16
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);
        }
Example #17
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();
        }
Example #18
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();
        }
Example #19
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");
        }
Example #20
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);

            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();
        }