public async Task DetectLanguageBatchConvenienceAsync()
        {
            string endpoint = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_ENDPOINT");
            string apiKey   = Environment.GetEnvironmentVariable("TEXT_ANALYTICS_API_KEY");

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

            var documents = new List <string>
            {
                "Hello world",
                "Bonjour tout le monde",
                "Hola mundo",
                ":) :( :D",
            };

            Console.WriteLine($"Detecting language for documents:");
            foreach (string document in documents)
            {
                Debug.WriteLine($"    {document}");
            }

            DetectLanguageResultCollection results = await client.DetectLanguageBatchAsync(documents);

            int i = 0;

            foreach (DetectLanguageResult result in results)
            {
                Console.WriteLine($"On document {documents[i++]}:");
                Console.WriteLine($"Detected language: {result.PrimaryLanguage.Name}, with confidence {result.PrimaryLanguage.Score}.");
            }
        }
Exemple #2
0
        public async Task DetectLanguageBatchConvenienceWithStatisticsTest()
        {
            TextAnalyticsClient client = GetClient();
            var documents = new List <string>
            {
                "Hello world",
                "This is a test"
            };

            var options = new TextAnalyticsRequestOptions()
            {
                IncludeStatistics = true,
                ModelVersion      = "2019-10-01"
            };

            DetectLanguageResultCollection results = await client.DetectLanguageBatchAsync(documents, "us", options);

            Assert.AreEqual("English", results[0].PrimaryLanguage.Name);
            Assert.AreEqual("English", results[1].PrimaryLanguage.Name);

            Assert.IsNotNull(results.Statistics);
            Assert.Greater(results.Statistics.DocumentCount, 0);
            Assert.Greater(results.Statistics.TransactionCount, 0);
            Assert.GreaterOrEqual(results.Statistics.InvalidDocumentCount, 0);
            Assert.GreaterOrEqual(results.Statistics.ValidDocumentCount, 0);

            Assert.IsNotNull(results[0].Statistics);
            Assert.Greater(results[0].Statistics.CharacterCount, 0);
            Assert.Greater(results[0].Statistics.TransactionCount, 0);
        }
        public void ThrowExceptionTest()
        {
            TextAnalyticsClient client = GetClient();
            var input = new List <string>();

            Assert.ThrowsAsync <RequestFailedException>(() => client.DetectLanguageBatchAsync(input));
        }
        public async Task DetectLanguageBatchTest()
        {
            TextAnalyticsClient client = GetClient();
            var inputs = new List <DetectLanguageInput>
            {
                new DetectLanguageInput("1", "Hello world")
                {
                    CountryHint = "us",
                },
                new DetectLanguageInput("2", "Bonjour tout le monde")
                {
                    CountryHint = "fr",
                },
                new DetectLanguageInput("3", "Hola mundo")
                {
                    CountryHint = "es",
                },
                new DetectLanguageInput("4", ":) :( :D")
                {
                    CountryHint = "us",
                }
            };

            DetectLanguageResultCollection results = await client.DetectLanguageBatchAsync(inputs);

            Assert.AreEqual("English", results[0].PrimaryLanguage.Name);
            Assert.AreEqual("French", results[1].PrimaryLanguage.Name);
            Assert.AreEqual("Spanish", results[2].PrimaryLanguage.Name);
            Assert.AreEqual("English", results[3].PrimaryLanguage.Name);
        }
        public async Task DetectLanguageBatchWithStatisticsTest()
        {
            TextAnalyticsClient client = GetClient();
            var inputs = new List <DetectLanguageInput>
            {
                new DetectLanguageInput("1", "Hello world")
                {
                    CountryHint = "us",
                },
                new DetectLanguageInput("2", "Bonjour tout le monde")
                {
                    CountryHint = "fr",
                },
                new DetectLanguageInput("3", "Hola mundo")
                {
                    CountryHint = "es",
                },
                new DetectLanguageInput("4", ":) :( :D")
                {
                    CountryHint = "us",
                }
            };

            DetectLanguageResultCollection results = await client.DetectLanguageBatchAsync(inputs, new TextAnalyticsRequestOptions { IncludeStatistics = true });

            Assert.AreEqual("English", results[0].PrimaryLanguage.Name);
            Assert.AreEqual("French", results[1].PrimaryLanguage.Name);
            Assert.AreEqual("Spanish", results[2].PrimaryLanguage.Name);
            Assert.AreEqual("English", results[3].PrimaryLanguage.Name);
            Assert.IsNotNull(results[0].Statistics);
            Assert.IsNotNull(results[0].Statistics.CharacterCount);
            Assert.IsNotNull(results[0].Statistics.TransactionCount);
        }
Exemple #6
0
        public async Task DetectLanguageBatchTest()
        {
            TextAnalyticsClient client = GetClient();
            var documents = new List <DetectLanguageInput>
            {
                new DetectLanguageInput("1", "Hello world")
                {
                    CountryHint = "us",
                },
                new DetectLanguageInput("2", "Bonjour tout le monde")
                {
                    CountryHint = "fr",
                },
                new DetectLanguageInput("3", "Hola mundo")
                {
                    CountryHint = "es",
                },
                new DetectLanguageInput("4", ":) :( :D")
                {
                    CountryHint = "us",
                }
            };

            DetectLanguageResultCollection results = await client.DetectLanguageBatchAsync(documents, options : new TextAnalyticsRequestOptions()
            {
                ModelVersion = "2019-10-01"
            });

            Assert.AreEqual("English", results[0].PrimaryLanguage.Name);
            Assert.AreEqual("French", results[1].PrimaryLanguage.Name);
            Assert.AreEqual("Spanish", results[2].PrimaryLanguage.Name);
            Assert.AreEqual("(Unknown)", results[3].PrimaryLanguage.Name);
        }
        public static async Task RunAsync(string endpoint, string key)
        {
            var credentials = new ApiKeyServiceClientCredentials(key);
            var client      = new TextAnalyticsClient(credentials)
            {
                Endpoint = endpoint
            };

            // The documents to be submitted for language detection. The ID can be any value.
            var inputDocuments = new LanguageBatchInput(
                new List <LanguageInput>
            {
                new LanguageInput("1", "This is a document written in English."),
                new LanguageInput("2", "Este es un document escrito en Español."),
                new LanguageInput("3", "这是一个用中文写的文件")
            });

            var langResults = await client.DetectLanguageBatchAsync(inputDocuments);

            // Printing detected languages
            Console.WriteLine("===== Language Detection =====\n");

            foreach (var document in langResults.Documents)
            {
                Console.WriteLine($"Document ID: {document.Id} , Language: {document.DetectedLanguages[0].Name}");
            }
            Console.WriteLine();
        }
Exemple #8
0
        private static async Task <string[]> GetDetectLanguage(TextAnalyticsClient client,
                                                               LanguageBatchInput docs)
        {
            List <string> ls = new List <string>();

            var res = await client.DetectLanguageBatchAsync(docs);

            foreach (var document in res.Documents)
            {
                ls.Add("|" + document.DetectedLanguages[0].Iso6391Name);
            }

            return(ls.ToArray());
        }
Exemple #9
0
        public async Task DetectLanguageBatchTest()
        {
            TextAnalyticsClient        client    = GetClient();
            List <DetectLanguageInput> documents = batchDocuments;

            DetectLanguageResultCollection results = await client.DetectLanguageBatchAsync(documents, options : new TextAnalyticsRequestOptions()
            {
                ModelVersion = "2019-10-01"
            });

            Assert.AreEqual("English", results[0].PrimaryLanguage.Name);
            Assert.AreEqual("French", results[1].PrimaryLanguage.Name);
            Assert.AreEqual("Spanish", results[2].PrimaryLanguage.Name);
            Assert.AreEqual("(Unknown)", results[3].PrimaryLanguage.Name);
        }
Exemple #10
0
        public void DetectLanguageBatchWithNullIdTest()
        {
            TextAnalyticsClient client = GetClient();
            var documents = new List <DetectLanguageInput> {
                new DetectLanguageInput(null, "Hello world")
            };

            RequestFailedException ex = Assert.ThrowsAsync <RequestFailedException>(
                async() => await client.DetectLanguageBatchAsync(documents, options: new TextAnalyticsRequestOptions()
            {
                ModelVersion = "2019-10-01"
            }));

            Assert.AreEqual(TextAnalyticsErrorCode.InvalidDocument, ex.ErrorCode);
        }
Exemple #11
0
        public async Task DetectLanguageBatchConvenienceTest()
        {
            TextAnalyticsClient client    = GetClient();
            List <string>       documents = batchConvenienceDocuments;

            DetectLanguageResultCollection results = await client.DetectLanguageBatchAsync(documents, options : new TextAnalyticsRequestOptions()
            {
                ModelVersion = "2019-10-01"
            });

            ValidateBatchDocumentsResult(results);

            Assert.AreEqual("English", results[0].PrimaryLanguage.Name);
            Assert.AreEqual("French", results[1].PrimaryLanguage.Name);
            Assert.AreEqual("Spanish", results[2].PrimaryLanguage.Name);
        }
        public async Task DetectLanguageBatchConvenienceTest()
        {
            TextAnalyticsClient client = GetClient();
            var inputs = new List <string>
            {
                "Hello world",
                "Bonjour tout le monde",
                "Hola mundo"
            };

            DetectLanguageResultCollection results = await client.DetectLanguageBatchAsync(inputs);

            Assert.AreEqual("English", results[0].PrimaryLanguage.Name);
            Assert.AreEqual("French", results[1].PrimaryLanguage.Name);
            Assert.AreEqual("Spanish", results[2].PrimaryLanguage.Name);
        }
        public async Task DetectLanguageBatchConvenienceWithStatisticsTest()
        {
            TextAnalyticsClient client = GetClient();
            var inputs = new List <string>
            {
                "Hello world",
                "This is a test"
            };

            DetectLanguageResultCollection results = await client.DetectLanguageBatchAsync(inputs, "us", new TextAnalyticsRequestOptions { IncludeStatistics = true });

            Assert.AreEqual("English", results[0].PrimaryLanguage.Name);
            Assert.AreEqual("English", results[1].PrimaryLanguage.Name);
            Assert.IsNotNull(results[0].Statistics);
            Assert.IsNotNull(results[0].Statistics.CharacterCount);
            Assert.IsNotNull(results[0].Statistics.TransactionCount);
        }
Exemple #14
0
        public async Task DetectLanguageBatchWithErrorTest()
        {
            TextAnalyticsClient client = GetClient();
            var inputs = new List <string>
            {
                "Hello world",
                "",
                "Hola mundo"
            };

            DetectLanguageResultCollection results = await client.DetectLanguageBatchAsync(inputs);

            Assert.IsTrue(!results[0].HasError);
            Assert.IsTrue(!results[2].HasError);

            Assert.IsTrue(results[1].HasError);
            Assert.Throws <InvalidOperationException>(() => results[1].PrimaryLanguage.GetType());
        }
Exemple #15
0
        public async Task DetectLanguageBatchWithNullTextTest()
        {
            TextAnalyticsClient client = GetClient();
            var documents = new List <DetectLanguageInput> {
                new DetectLanguageInput("1", null)
            };

            DetectLanguageResultCollection results = await client.DetectLanguageBatchAsync(documents, options : new TextAnalyticsRequestOptions()
            {
                ModelVersion = "2019-10-01"
            });

            var exceptionMessage = "Cannot access result for document 1, due to error InvalidDocument: Document text is empty.";

            Assert.IsTrue(results[0].HasError);
            InvalidOperationException ex = Assert.Throws <InvalidOperationException>(() => results[0].PrimaryLanguage.GetType());

            Assert.AreEqual(exceptionMessage, ex.Message);
        }
        public static async Task <IEnumerable <string> > DetectLanguageBatchAsync(TextAnalyticsClient client, IEnumerable <string> documents)
        {
            var detectedLanguages = await client.DetectLanguageBatchAsync(documents);

            Console.WriteLine("Languages:");

            List <string> languages = new List <string>();

            foreach (var result in detectedLanguages.Value)
            {
                DetectedLanguage dl = result.PrimaryLanguage;

                Console.WriteLine($"\t{dl.Name},\tISO-6391: {dl.Iso6391Name}, \tConfidenceScore: {dl.ConfidenceScore}");
                languages.Add(dl.Iso6391Name);
            }
            Console.WriteLine();

            return(languages);
        }
Exemple #17
0
        public async Task DetectLanguageBatchWithStatisticsTest()
        {
            TextAnalyticsClient        client    = GetClient();
            List <DetectLanguageInput> documents = batchDocuments;

            var options = new TextAnalyticsRequestOptions()
            {
                IncludeStatistics = true,
                ModelVersion      = "2019-10-01"
            };

            DetectLanguageResultCollection results = await client.DetectLanguageBatchAsync(documents, options : options);

            ValidateBatchDocumentsResult(results, includeStatistics: true);

            Assert.AreEqual("English", results[0].PrimaryLanguage.Name);
            Assert.AreEqual("French", results[1].PrimaryLanguage.Name);
            Assert.AreEqual("Spanish", results[2].PrimaryLanguage.Name);
            Assert.AreEqual("(Unknown)", results[3].PrimaryLanguage.Name);
        }
        public async Task DetectLanguageBatchWithErrorTest()
        {
            TextAnalyticsClient client = GetClient();
            var inputs = new List <string>
            {
                "Hello world",
                "",
                "Hola mundo"
            };

            DetectLanguageResultCollection results = await client.DetectLanguageBatchAsync(inputs);

            Assert.IsTrue(!results[0].HasError);
            Assert.IsTrue(!results[2].HasError);

            var exceptionMessage = "Cannot access result for document 1, due to error InvalidDocument: Document text is empty.";

            Assert.IsTrue(results[1].HasError);
            InvalidOperationException ex = Assert.Throws <InvalidOperationException>(() => results[1].PrimaryLanguage.GetType());

            Assert.AreEqual(exceptionMessage, ex.Message);
        }
        public async Task RunLanguageDetectionAsync(CidadaoModel cidadao)
        {
            var credentials = new ApiKeyServiceClientCredentials(key);
            var client      = new TextAnalyticsClient(credentials)
            {
                Endpoint = endpoint
            };

            // The documents to be submitted for language detection. The ID can be any value.
            var inputDocuments = new LanguageBatchInput(
                new List <LanguageInput>
            {
                new LanguageInput("1", cidadao.texto),
            });

            var langResults = await client.DetectLanguageBatchAsync(inputDocuments);

            // Printing detected languages

            foreach (var document in langResults.Documents)
            {
            }
        }
Exemple #20
0
        public async Task DetectLanguageBatchConvenienceTest()
        {
            TextAnalyticsClient client = GetClient();
            var documents = new List <string>
            {
                "Hello world",
                "Bonjour tout le monde",
                "Hola mundo"
            };

            DetectLanguageResultCollection results = await client.DetectLanguageBatchAsync(documents, options : new TextAnalyticsRequestOptions()
            {
                ModelVersion = "2019-10-01"
            });

            Assert.AreEqual("English", results[0].PrimaryLanguage.Name);
            Assert.AreEqual("French", results[1].PrimaryLanguage.Name);
            Assert.AreEqual("Spanish", results[2].PrimaryLanguage.Name);

            Assert.AreEqual(0, results[0].Statistics.CharacterCount);
            Assert.AreEqual(0, results[0].Statistics.TransactionCount);
            Assert.IsNull(results.Statistics);
        }
        protected override async Task OnMessageActivityAsync(ITurnContext <IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            string key      = "c5350e440b0e4f61884b61e5786962cc";
            string endpoint = "https://ai-meetup-entity-rec.cognitiveservices.azure.com/";

            //TODO (demo): Add language detction here for demo - Aris

            var credentials = new ApiKeyServiceClientCredentials(key);
            var client      = new TextAnalyticsClient(credentials)
            {
                Endpoint = endpoint
            };

            var inputDocuments = new LanguageBatchInput(
                new List <LanguageInput>
            {
                new LanguageInput("1", turnContext.Activity.Text)
            });

            var langResults = await client.DetectLanguageBatchAsync(inputDocuments);

            var replyText = $"Your detected language is: {langResults.Documents[0].DetectedLanguages[0].Name}";
            await turnContext.SendActivityAsync(MessageFactory.Text(replyText, replyText), cancellationToken);
        }
 public override async Task RunAsync(CancellationToken cancellationToken)
 {
     await _client.DetectLanguageBatchAsync(_batchDocuments, cancellationToken : cancellationToken);
 }
Exemple #23
0
        public async Task DetectLanguageBatchAsync()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

            var documents = new List <DetectLanguageInput>
            {
                new DetectLanguageInput("1", "Hello world")
                {
                    CountryHint = "us",
                },
                new DetectLanguageInput("2", "Bonjour tout le monde")
                {
                    CountryHint = "fr",
                },
                new DetectLanguageInput("3", "Hola mundo")
                {
                    CountryHint = "es",
                },
                new DetectLanguageInput("4", ":) :( :D")
                {
                    CountryHint = DetectLanguageInput.None,
                }
            };

            DetectLanguageResultCollection results = await client.DetectLanguageBatchAsync(documents, new TextAnalyticsRequestOptions { IncludeStatistics = true });

            int i = 0;

            Console.WriteLine($"Results of Azure Text Analytics \"Detect Language\" Model, version: \"{results.ModelVersion}\"");
            Console.WriteLine("");

            foreach (DetectLanguageResult result in results)
            {
                DetectLanguageInput document = documents[i++];

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

                if (result.HasError)
                {
                    Console.WriteLine($"    Document error code: {result.Error.Code}.");
                    Console.WriteLine($"    Message: {result.Error.Message}.");
                }
                else
                {
                    Console.WriteLine($"    Detected language {result.PrimaryLanguage.Name} with confidence score {result.PrimaryLanguage.ConfidenceScore}.");

                    Console.WriteLine($"    Document statistics:");
                    Console.WriteLine($"        Character count (in Unicode graphemes): {result.Statistics.CharacterCount}");
                    Console.WriteLine($"        Transaction count: {result.Statistics.TransactionCount}");
                    Console.WriteLine("");
                }
            }

            Console.WriteLine($"Batch operation statistics:");
            Console.WriteLine($"    Document count: {results.Statistics.DocumentCount}");
            Console.WriteLine($"    Valid document count: {results.Statistics.ValidDocumentCount}");
            Console.WriteLine($"    Invalid document count: {results.Statistics.InvalidDocumentCount}");
            Console.WriteLine($"    Transaction count: {results.Statistics.TransactionCount}");
            Console.WriteLine("");
        }
Exemple #24
0
        public async Task DetectLanguageBatchAsync()
        {
            string endpoint = TestEnvironment.Endpoint;
            string apiKey   = TestEnvironment.ApiKey;

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

            string documentA = @"Este documento está escrito en un idioma diferente al Inglés. Tiene como objetivo demostrar
                                cómo invocar el método de Detección de idioma del servicio de Text Analytics en Microsoft Azure.
                                También muestra cómo acceder a la información retornada por el servicio. Esta capacidad es útil
                                para los sistemas de contenido que recopilan texto arbitrario, donde el idioma es desconocido.
                                La característica Detección de idioma puede detectar una amplia gama de idiomas, variantes,
                                dialectos y algunos idiomas regionales o culturales.";

            string documentB = @"This document is written in a language different than Spanish. It's objective is to demonstrate
                                how to call the Detect Language method from the Microsoft Azure Text Analytics service.
                                It also shows how to access the information returned from the service. This capability is useful
                                for content stores that collect arbitrary text, where language is unknown.
                                The Language Detection feature can detect a wide range of languages, variants, dialects, and some
                                regional or cultural languages.";

            string documentC = @"Ce document est rédigé dans une langue différente de l'espagnol. Son objectif est de montrer comment
                                appeler la méthode Detect Language à partir du service Microsoft Azure Text Analytics.
                                Il montre également comment accéder aux informations renvoyées par le service. Cette capacité est
                                utile pour les magasins de contenu qui collectent du texte arbitraire dont la langue est inconnue.
                                La fonctionnalité Détection de langue peut détecter une grande variété de langues, de variantes,
                                de dialectes, et certaines langues régionales ou de culture.";

            var documents = new List <DetectLanguageInput>
            {
                new DetectLanguageInput("1", documentA)
                {
                    CountryHint = "es",
                },
                new DetectLanguageInput("2", documentB)
                {
                    CountryHint = "us",
                },
                new DetectLanguageInput("3", documentC)
                {
                    CountryHint = "fr",
                },
                new DetectLanguageInput("4", ":) :( :D")
                {
                    CountryHint = DetectLanguageInput.None,
                },
                new DetectLanguageInput("5", "")
                {
                    CountryHint = "en",
                }
            };

            var options = new TextAnalyticsRequestOptions {
                IncludeStatistics = true
            };

            Response <DetectLanguageResultCollection> response = await client.DetectLanguageBatchAsync(documents, options);

            DetectLanguageResultCollection documentsLanguage = response.Value;

            int i = 0;

            Console.WriteLine($"Results of \"Detect Language\" Model, version: \"{documentsLanguage.ModelVersion}\"");
            Console.WriteLine("");

            foreach (DetectLanguageResult documentLanguage in documentsLanguage)
            {
                DetectLanguageInput document = documents[i++];

                Console.WriteLine($"On document (Id={document.Id}, CountryHint=\"{document.CountryHint}\")");

                if (documentLanguage.HasError)
                {
                    Console.WriteLine("  Error!");
                    Console.WriteLine($"  Document error code: {documentLanguage.Error.ErrorCode}.");
                    Console.WriteLine($"  Message: {documentLanguage.Error.Message}");
                }
                else
                {
                    Console.WriteLine($"  Detected language: {documentLanguage.PrimaryLanguage.Name}");
                    Console.WriteLine($"  Confidence score: {documentLanguage.PrimaryLanguage.ConfidenceScore}");

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

            Console.WriteLine($"Batch operation statistics:");
            Console.WriteLine($"  Document count: {documentsLanguage.Statistics.DocumentCount}");
            Console.WriteLine($"  Valid document count: {documentsLanguage.Statistics.ValidDocumentCount}");
            Console.WriteLine($"  Invalid document count: {documentsLanguage.Statistics.InvalidDocumentCount}");
            Console.WriteLine($"  Transaction count: {documentsLanguage.Statistics.TransactionCount}");
        }