Ejemplo n.º 1
0
        public static bool VerifyDetectLanguage(DetectedLanguage dl)
        {
            string error = "";

            try
            {
                if (dl.DetectedLanguageProperty == null)
                {
                    error += "Detected Language is null";
                }
                if (!VerifyStatus(dl.Status))
                {
                    error += TestBase.ErrorMessage;
                }
                if (dl.TrackingId == null)
                {
                    error += "Trackingid is null";
                }

                TestBase.ErrorMessage = error;
                return(string.IsNullOrEmpty(error));
            }
            catch (Exception e)
            {
                TestBase.ErrorMessage += "Unable to verify the detect language response . Error:" + e.InnerException.Message;
                return(false);
            }
        }
Ejemplo n.º 2
0
 private void ValidateInDocumenResult(DetectedLanguage language)
 {
     Assert.That(language.Name, Is.Not.Null.And.Not.Empty);
     Assert.That(language.Iso6391Name, Is.Not.Null.And.Not.Empty);
     Assert.GreaterOrEqual(language.ConfidenceScore, 0.0);
     Assert.IsNotNull(language.Warnings);
 }
        public async Task DetectLanguageAsync()
        {
            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));

            #region Snippet:DetectLanguageAsync
            string document = @"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.";

            try
            {
                Response <DetectedLanguage> response = await client.DetectLanguageAsync(document);

                DetectedLanguage language = response.Value;
                Console.WriteLine($"Detected language {language.Name} with confidence score {language.ConfidenceScore}.");
            }
            catch (RequestFailedException exception)
            {
                Console.WriteLine($"Error Code: {exception.ErrorCode}");
                Console.WriteLine($"Message: {exception.Message}");
            }
            #endregion
        }
        static void LanguageDetectionExample(TextAnalyticsClient client)
        {
            DetectedLanguage detectedLanguage = client.DetectLanguage("Ce document est rédigé en Français.");

            Console.WriteLine("Language:");
            Console.WriteLine($"\t{detectedLanguage.Name},\tISO-6391: {detectedLanguage.Iso6391Name}\n");
        }
        public static async Task <string> DetectLanguageAsync(TextAnalyticsClient client, string content)
        {
            DetectedLanguage detectedLanguage = await client.DetectLanguageAsync(content);

            Console.WriteLine("Language:");
            Console.WriteLine($"\t{detectedLanguage.Name},\tISO-6391: {detectedLanguage.Iso6391Name}\n");
            return(detectedLanguage.Iso6391Name);
        }
Ejemplo n.º 6
0
        //Language
        private string LanguageDetection(string textInput)
        {
            DetectedLanguage detectedLanguage = client.DetectLanguage(textInput);

            Console.WriteLine("Language:");
            Console.WriteLine($"\t{detectedLanguage.Name},\tISO-6391: {detectedLanguage.Iso6391Name}\n");

            return($"\t{detectedLanguage.Name},\tISO-6391: {detectedLanguage.Iso6391Name}\n");
        }
Ejemplo n.º 7
0
        public async Task DetectLanguageTest()
        {
            TextAnalyticsClient client = GetClient();
            string document            = SingleEnglish;

            DetectedLanguage language = await client.DetectLanguageAsync(document);

            ValidateInDocumenResult(language);
        }
Ejemplo n.º 8
0
        public async Task DetectLanguageWithNoneCountryHintTest()
        {
            TextAnalyticsClient client = GetClient();
            string document            = SingleSpanish;

            DetectedLanguage language = await client.DetectLanguageAsync(document, DetectLanguageInput.None);

            ValidateInDocumenResult(language);
        }
Ejemplo n.º 9
0
        // language detection
        static void LanguageDetection(TextAnalyticsClient client, string textSource)
        {
            DetectedLanguage detectedLanguage = client.DetectLanguage(textSource);

            // Console.WriteLine("Language:");
            // detectedLanguage.Name + " " + detectedLanguage.Iso6391Name;
            Console.WriteLine($"\t{detectedLanguage.Name},\tISO-6391: {detectedLanguage.Iso6391Name}\n");
            CultureInfo myCIintl = new CultureInfo(detectedLanguage.Iso6391Name, false);
        }
Ejemplo n.º 10
0
        public string LanguageDetectionExample(TextAnalyticsClient client, string text)
        {
            DetectedLanguage detectedLanguage = client.DetectLanguage(text);

            Console.WriteLine("Language:");
            Console.WriteLine($"\t{detectedLanguage.Name},\tISO-6391: {detectedLanguage.Iso6391Name}\n");

            return(detectedLanguage.Iso6391Name.ToString());
        }
Ejemplo n.º 11
0
        public async Task DetectLanguageWithAADTest()
        {
            TextAnalyticsClient client = GetClient(useTokenCredential: true);
            string document            = SingleEnglish;

            DetectedLanguage language = await client.DetectLanguageAsync(document);

            ValidateInDocumenResult(language);
        }
Ejemplo n.º 12
0
        public static string GetLanguageCode(DetectedLanguage detectedLanguage)
        {
            if (LanguageCodeMap.ContainsKey(detectedLanguage.Iso6391Name))
            {
                return(LanguageCodeMap[detectedLanguage.Iso6391Name]);
            }

            return(!string.IsNullOrEmpty(detectedLanguage.Iso6391Name) ? detectedLanguage.Iso6391Name : DefaultLanguageCode);
        }
        public async Task DetectLanguageWithNoneCountryHintTest()
        {
            TextAnalyticsClient client = GetClient();
            string input = "Este documento está en español";

            DetectedLanguage language = await client.DetectLanguageAsync(input, DetectLanguageInput.None);

            Assert.AreEqual("Spanish", language.Name);
        }
Ejemplo n.º 14
0
        static string LanguageDetectionExample(TextAnalyticsClient client, string text)
        {
            DetectedLanguage detectedLanguage = client.DetectLanguage(text);
            string           response         = "Language: \n" + $"      {detectedLanguage.Name},      ISO-6391: {detectedLanguage.Iso6391Name}\n\n";

            Console.WriteLine(response);


            return(response);
        }
Ejemplo n.º 15
0
        public async Task DetectLanguageWithCountryHintTest()
        {
            TextAnalyticsClient client = GetClient();
            string input = "Este documento está en español";

            DetectLanguageResult result = await client.DetectLanguageAsync(input, "CO");

            DetectedLanguage language = result.PrimaryLanguage;

            Assert.AreEqual("Spanish", language.Name);
        }
Ejemplo n.º 16
0
        public async Task DetectLanguageWithAADTest()
        {
            TextAnalyticsClient client = GetClient(useTokenCredential: true);
            string document            = singleEnglish;

            DetectedLanguage language = await client.DetectLanguageAsync(document);

            Assert.IsNotNull(language.Name);
            Assert.IsNotNull(language.Iso6391Name);
            Assert.Greater(language.ConfidenceScore, 0.0);
        }
Ejemplo n.º 17
0
        public async Task DetectLanguageWithCountryHintTest()
        {
            TextAnalyticsClient client = GetClient();
            string document            = singleSpanish;

            DetectedLanguage language = await client.DetectLanguageAsync(document, "CO");

            Assert.IsNotNull(language.Name);
            Assert.IsNotNull(language.Iso6391Name);
            Assert.Greater(language.ConfidenceScore, 0.0);
        }
        public async Task DetectLanguageTest()
        {
            TextAnalyticsClient client = GetClient();
            string input = "This is written in English.";

            DetectedLanguage language = await client.DetectLanguageAsync(input);

            Assert.AreEqual("English", language.Name);
            Assert.AreEqual("en", language.Iso6391Name);
            Assert.AreEqual(1.0, language.Score);
        }
Ejemplo n.º 19
0
        public async Task DetectLanguageWithNoneCountryHintTest()
        {
            TextAnalyticsClient client = GetClient();
            string document            = "Este documento está en español";

            DetectedLanguage language = await client.DetectLanguageAsync(document, DetectLanguageInput.None);

            Assert.IsNotNull(language.Name);
            Assert.IsNotNull(language.Iso6391Name);
            Assert.Greater(language.ConfidenceScore, 0.0);
        }
Ejemplo n.º 20
0
        public async Task DetectLanguageTest()
        {
            TextAnalyticsClient client = GetClient();
            string document            = "This is written in English.";

            DetectedLanguage language = await client.DetectLanguageAsync(document);

            Assert.IsNotNull(language.Name);
            Assert.IsNotNull(language.Iso6391Name);
            Assert.Greater(language.ConfidenceScore, 0.0);
        }
Ejemplo n.º 21
0
        private static async Task <bool> IsSpanishAsync(string document, TextAnalyticsClient client, CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                DetectedLanguage language = await client.DetectLanguageAsync(document);

                return(language.Iso6391Name == "es");
            }

            cancellationToken.ThrowIfCancellationRequested();
            return(false);
        }
Ejemplo n.º 22
0
        static string GetLanguage(string text)
        {
            // Create client using endpoint and key
            AzureKeyCredential credentials = new AzureKeyCredential(cogSvcKey);
            Uri endpoint = new Uri(cogSvcEndpoint);
            var client   = new TextAnalyticsClient(endpoint, credentials);

            // Call the service to get the detected language
            DetectedLanguage detectedLanguage = client.DetectLanguage(text);

            return(detectedLanguage.Name);
        }
        public IActionResult AddUMessage(UMessage obj)
        {
            var data   = rep.AddObj(obj);
            var client = new TextAnalyticsClient(endpoint, credentials);
            DetectedLanguage detectedLanguage = client.DetectLanguage(rep.GetById(obj.Id).MContent);

            obj.LanguageId = detectedLanguage.Name;
            rep.EditObj(obj);


            return(LocalRedirect("~/api/RMessage/GetData/1"));
        }
Ejemplo n.º 24
0
        private static async Task <DialogTurnResult> DetectLanguage(DialogContext dc, System.Object options)
        {
            string           incomingText = dc.State.GetValue("turn.activity.text", () => "");
            DetectedLanguage language     = _textAnalyticsClient.DetectLanguage(incomingText);

            if (language.ConfidenceScore > 0.8)
            {
                dc.State.SetValue("conversation.currLanguage", language.Name);
            }
            Console.WriteLine($"Detected language {language.Name} with confidence score {language.ConfidenceScore}.");
            return(await dc.EndDialogAsync());
        }
        public async Task DetectLanguageWithNoneDefaultCountryHintTest()
        {
            var options = new TextAnalyticsClientOptions()
            {
                DefaultCountryHint = DetectLanguageInput.None
            };

            TextAnalyticsClient client = GetClient(options: options);
            string input = "Este documento está en español";

            DetectedLanguage language = await client.DetectLanguageAsync(input, DetectLanguageInput.None);

            Assert.AreEqual("Spanish", language.Name);
        }
        public static DetectedLanguage AnalyzeLanguage(string id, string text)
        {
            DetectedLanguage language = new DetectedLanguage();
            var document = new Document()
            {
                Id   = id,
                Text = text
            };

            var client = new LanguageClient(Constants.ApiKey)
            {
                Url = "https://westeurope.api.cognitive.microsoft.com/text/analytics/v2.0/languages"
            };
            var request = new LanguageRequest();

            request.Documents.Add(document);

            try
            {
                var response = client.GetLanguages(request);
                language = response.Documents.First().DetectedLanguages.First();
            }
            catch (Exception ex)
            {
                var message      = "";
                var innerMessage = "";
                if (!String.IsNullOrEmpty(ex.Message))
                {
                    message = ex.Message;
                }

                try
                {
                    if ((ex.InnerException != null) && (!String.IsNullOrEmpty(ex.InnerException.Message)))
                    {
                        innerMessage = ex.InnerException.Message;
                    }
                }
                catch (Exception innerEx)
                {
                    if ((innerEx.InnerException != null) && (!String.IsNullOrEmpty(innerEx.InnerException.Message)))
                    {
                        innerMessage = innerEx.InnerException.Message;
                    }
                }

                Console.WriteLine(String.Format("Error in AnalyzeSentiment: {0}:{1}", message, innerMessage));
            }
            return(language);
        }
Ejemplo n.º 27
0
        public async Task DetectLanguageWithNoneDefaultCountryHintTest()
        {
            var options = new TextAnalyticsClientOptions()
            {
                DefaultCountryHint = DetectLanguageInput.None
            };

            TextAnalyticsClient client = GetClient(options: options);
            string document            = SingleSpanish;

            DetectedLanguage language = await client.DetectLanguageAsync(document, DetectLanguageInput.None);

            ValidateInDocumenResult(language);
        }
Ejemplo n.º 28
0
        public static DetectedLanguage AnalyzeLanguage(string id, string text)
        {
            DetectedLanguage language = new DetectedLanguage();
            var document = new Document()
            {
                Id   = id,
                Text = text
            };

            var client = new LanguageClient(Constants.API_KEY)
            {
                Url = Constants.API_URI_LANG
            };
            var request = new LanguageRequest();

            request.Documents.Add(document);

            try
            {
                var response = client.GetLanguages(request);
                language = response.Documents.First().DetectedLanguages.First();
            }
            catch (Exception ex)
            {
                var message      = "";
                var innerMessage = "";
                if (!String.IsNullOrEmpty(ex.Message))
                {
                    message = ex.Message;
                }

                try
                {
                    if ((ex.InnerException != null) && (!String.IsNullOrEmpty(ex.InnerException.Message)))
                    {
                        innerMessage = ex.InnerException.Message;
                    }
                }
                catch (Exception innerEx)
                {
                    if ((innerEx.InnerException != null) && (!String.IsNullOrEmpty(innerEx.InnerException.Message)))
                    {
                        innerMessage = innerEx.InnerException.Message;
                    }
                }

                //Console.WriteLine(String.Format("Error in AnalyzeSentiment: {0}:{1}", message, innerMessage));
            }
            return(language);
        }
Ejemplo n.º 29
0
        static void Main(string[] args)
        {
            Uri    endpoint    = new Uri(""); // insert your endpoint
            string key         = "";          // insert your key
            var    credentials = new AzureKeyCredential(key);
            var    client      = new TextAnalyticsClient(endpoint, credentials);

            DetectedLanguage lang1 = client.DetectLanguage("This is a document written in English.");

            Console.WriteLine($"Language: {lang1.Name}");

            DetectedLanguage lang2 = client.DetectLanguage("Parlez Vous Francais?");

            Console.WriteLine($"Language: {lang2.Name}");
        }
        public async Task DetectLanguageAsync()
        {
            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 TextAnalyticsApiKeyCredential(apiKey));

            #region Snippet:DetectLanguageAsync
            string input = "Este documento está en español.";

            DetectedLanguage language = await client.DetectLanguageAsync(input);

            Console.WriteLine($"Detected language {language.Name} with confidence {language.Score}.");
            #endregion
        }