Exemplo n.º 1
0
        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(id: "1", text: "This is a document written in English."),
                new LanguageInput(id: "2", text: "Este es un document escrito en Español."),
                new LanguageInput(id: "3", text: "这是一个用中文写的文件")
            });

            var langResults = await client.DetectLanguageAsync(false, inputDocuments);

            // Printing detected languages
            foreach (var document in langResults.Documents)
            {
                Console.WriteLine($"Document ID: {document.Id} , Language: {document.DetectedLanguages[0].Name}");
            }
        }
Exemplo n.º 2
0
        internal string DetectLanguage(string requestBody)
        {
            var client = GetClient();

            LanguageBatchInput input = new LanguageBatchInput()
            {
                Documents = new List <LanguageInput>()
                {
                    new LanguageInput()
                    {
                        Id   = "1",
                        Text = requestBody
                    }
                }
            };
            var result = client.DetectLanguageAsync(true, input).Result;

            foreach (var document in result.Documents)
            {
                Console.WriteLine($"Document ID: {document.Id} , " +
                                  $"Language: {document.DetectedLanguages[0].Name}," +
                                  $" Score: " +
                                  $"{document.DetectedLanguages[0].Score}");
            }

            return(result.Documents[0].DetectedLanguages[0].Iso6391Name);
        }
Exemplo n.º 3
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());
        }
Exemplo n.º 4
0
 public virtual Response <LanguageResult> Languages(LanguageBatchInput input, string modelVersion = null, bool?showStats = null, CancellationToken cancellationToken = default)
 {
     using var scope = _clientDiagnostics.CreateScope("ServiceClient.Languages");
     scope.Start();
     try
     {
         return(RestClient.Languages(input, modelVersion, showStats, cancellationToken));
     }
     catch (Exception e)
     {
         scope.Failed(e);
         throw;
     }
 }
        /// <summary>
        /// The API returns the detected language and a numeric score between 0 and 1.
        /// </summary>
        /// <remarks>
        /// Scores close to 1 indicate 100% certainty that the identified language is
        /// true. A total of 120 languages are supported.
        /// </remarks>
        /// <param name='operations'>
        /// The operations group for this extension method.
        /// </param>
        /// <param name='showStats'>
        /// (optional) if set to true, response will contain input and document level
        /// statistics.
        /// </param>
        /// <param name='inputText'>
        /// Input text of one document.
        /// </param>
        /// <param name='countryHint'>
        /// Contry hint.
        /// </param>
        /// <param name='cancellationToken'>
        /// The cancellation token.
        /// </param>
        public static LanguageBatchResult DetectLanguage(
            this ITextAnalyticsClient operations,
            string inputText   = default,
            string countryHint = "en",
            bool?showStats     = default,
            CancellationToken cancellationToken = default)
        {
            var languageBatchInput = new LanguageBatchInput(new List <LanguageInput> {
                new LanguageInput("1", inputText, countryHint)
            });
            var _result = operations.DetectLanguageWithHttpMessagesAsync(showStats, languageBatchInput, null, cancellationToken).ConfigureAwait(false).GetAwaiter().GetResult();

            return(_result.Body);
        }
Exemplo n.º 6
0
        /// <summary>
        /// The API returns the detected language and a numeric score between 0 and 1.
        /// </summary>
        /// <remarks>
        /// Scores close to 1 indicate 100% certainty that the identified language is
        /// true. A total of 120 languages are supported.
        /// </remarks>
        /// <param name='operations'>
        /// The operations group for this extension method.
        /// </param>
        /// <param name='showStats'>
        /// (optional) if set to true, response will contain input and document level
        /// statistics.
        /// </param>
        /// <param name='inputText'>
        /// Input text of one document.
        /// </param>
        /// <param name='countryHint'>
        /// Contry hint.
        /// </param>
        /// <param name='cancellationToken'>
        /// The cancellation token.
        /// </param>
        public static LanguageResult DetectLanguage(
            this ITextAnalyticsClient operations,
            string inputText   = default,
            string countryHint = "US",
            bool?showStats     = default,
            CancellationToken cancellationToken = default)
        {
            var languageBatchInput = new LanguageBatchInput(new List <LanguageInput> {
                new LanguageInput("1", inputText, countryHint)
            });
            var _result = operations.DetectLanguageWithHttpMessagesAsync(showStats, languageBatchInput, null, cancellationToken).ConfigureAwait(false).GetAwaiter().GetResult();

            IList <DetectedLanguage> languages = _result.Body.Documents.Count > 0 ? _result.Body.Documents[0].DetectedLanguages : null;
            string            errorMessage     = _result.Body.Errors.Count > 0 ? _result.Body.Errors[0].Message : null;
            RequestStatistics stats            = _result.Body.Statistics;

            return(new LanguageResult(languages, errorMessage, stats));
        }
        static void Main(string[] args)
        {
            if (args == null || !args.Any())
            {
                System.Console.WriteLine("Por favor indicar el texto");
                return;
            }

            var text = args[0];


            string endpoint = "https://westeurope.api.cognitive.microsoft.com/";
            string key      = "c50bac85176f4da384d8e2d06bf17d41";

            var credentials = new ApiKeyServiceClientCredentials(key);
            var client      = new TextAnalyticsClient(credentials);

            client.Endpoint = endpoint;


            var input = new List <LanguageInput>();

            input.Add(new LanguageInput(id: "1", text: text));
            var batch    = new LanguageBatchInput(input);
            var analysis = client.DetectLanguageAsync(false, batch).Result;

            foreach (var document in analysis.Documents)
            {
                var language = document.DetectedLanguages.FirstOrDefault()?.Iso6391Name;


                var multiInput = new List <MultiLanguageInput>();
                multiInput.Add(new MultiLanguageInput(language, "1", text));
                var multiBatch      = new MultiLanguageBatchInput(multiInput);
                var sentimentResult = client.SentimentAsync(false, multiBatch).Result;

                foreach (var sentiment in sentimentResult.Documents)
                {
                    System.Console.WriteLine($"{text} : {sentiment.Score}");
                }
            }
        }
        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)
            {
            }
        }
                    public async Task RunAsync(string endpoint, string key, string text)
                    {
                        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(id: "1", text: text)
                        });

                        var langResults = await client.DetectLanguageAsync(false, inputDocuments);

                        // Printing detected languages
                        foreach (var document in langResults.Documents)
                        {
                            Language.Add($"{document.DetectedLanguages[0].Name}");
                            //Console.WriteLine($"Document ID: {document.Id} , Language: {document.DetectedLanguages[0].Name}");
                        }
                    }
        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);
        }
Exemplo n.º 11
0
 /// <summary>
 /// The API returns the detected language and a numeric score between 0 and 1.
 /// </summary>
 /// <remarks>
 /// Scores close to 1 indicate 100% certainty that the identified language is
 /// true. A total of 120 languages are supported.
 /// </remarks>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='showStats'>
 /// (optional) if set to true, response will contain input and document level
 /// statistics.
 /// </param>
 /// <param name='languageBatchInput'>
 /// Collection of documents to analyze.
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task<LanguageBatchResult> DetectLanguageAsync(this ITextAnalyticsClient operations, bool? showStats = default(bool?), LanguageBatchInput languageBatchInput = default(LanguageBatchInput), CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.DetectLanguageWithHttpMessagesAsync(showStats, languageBatchInput, null, cancellationToken).ConfigureAwait(false))
     {
         return _result.Body;
     }
 }
        /// <summary>
        /// The API returns the detected language and a numeric score between 0 and 1.
        /// </summary>
        /// <remarks>
        /// Scores close to 1 indicate 100% certainty that the identified language is
        /// true. A total of 120 languages are supported.
        /// </remarks>
        /// <param name='showStats'>
        /// (optional) if set to true, response will contain input and document level
        /// statistics.
        /// </param>
        /// <param name='languageBatchInput'>
        /// Collection of documents to analyze.
        /// </param>
        /// <param name='customHeaders'>
        /// Headers that will be added to request.
        /// </param>
        /// <param name='cancellationToken'>
        /// The cancellation token.
        /// </param>
        /// <exception cref="ErrorResponseException">
        /// Thrown when the operation returned an invalid status code
        /// </exception>
        /// <exception cref="SerializationException">
        /// Thrown when unable to deserialize the response
        /// </exception>
        /// <exception cref="ValidationException">
        /// Thrown when a required parameter is null
        /// </exception>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown when a required parameter is null
        /// </exception>
        /// <return>
        /// A response object containing the response body and response headers.
        /// </return>
        public async Task <HttpOperationResponse <LanguageBatchResult> > DetectLanguageWithHttpMessagesAsync(bool?showStats = default(bool?), LanguageBatchInput languageBatchInput = default(LanguageBatchInput), Dictionary <string, List <string> > customHeaders = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (Endpoint == null)
            {
                throw new ValidationException(ValidationRules.CannotBeNull, "this.Endpoint");
            }
            // Tracing
            bool   _shouldTrace  = ServiceClientTracing.IsEnabled;
            string _invocationId = null;

            if (_shouldTrace)
            {
                _invocationId = ServiceClientTracing.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("showStats", showStats);
                tracingParameters.Add("languageBatchInput", languageBatchInput);
                tracingParameters.Add("cancellationToken", cancellationToken);
                ServiceClientTracing.Enter(_invocationId, this, "DetectLanguage", tracingParameters);
            }
            // Construct URL
            var _baseUrl = BaseUri;
            var _url     = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "languages";

            _url = _url.Replace("{Endpoint}", Endpoint);
            List <string> _queryParameters = new List <string>();

            if (showStats != null)
            {
                _queryParameters.Add(string.Format("showStats={0}", System.Uri.EscapeDataString(SafeJsonConvert.SerializeObject(showStats, SerializationSettings).Trim('"'))));
            }
            if (_queryParameters.Count > 0)
            {
                _url += "?" + string.Join("&", _queryParameters);
            }
            // Create HTTP transport objects
            var _httpRequest = new HttpRequestMessage();
            HttpResponseMessage _httpResponse = null;

            _httpRequest.Method     = new HttpMethod("POST");
            _httpRequest.RequestUri = new System.Uri(_url);
            // Set Headers


            if (customHeaders != null)
            {
                foreach (var _header in customHeaders)
                {
                    if (_httpRequest.Headers.Contains(_header.Key))
                    {
                        _httpRequest.Headers.Remove(_header.Key);
                    }
                    _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
                }
            }

            // Serialize Request
            string _requestContent = null;

            if (languageBatchInput != null)
            {
                _requestContent      = SafeJsonConvert.SerializeObject(languageBatchInput, SerializationSettings);
                _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8);
                _httpRequest.Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
            }
            // Set Credentials
            if (Credentials != null)
            {
                cancellationToken.ThrowIfCancellationRequested();
                await Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
            }
            // Send Request
            if (_shouldTrace)
            {
                ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
            }
            cancellationToken.ThrowIfCancellationRequested();
            _httpResponse = await HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);

            if (_shouldTrace)
            {
                ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
            }
            HttpStatusCode _statusCode = _httpResponse.StatusCode;

            cancellationToken.ThrowIfCancellationRequested();
            string _responseContent = null;

            if ((int)_statusCode != 200)
            {
                var ex = new ErrorResponseException(string.Format("Operation returned an invalid status code '{0}'", _statusCode));
                try
                {
                    _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                    ErrorResponse _errorBody = SafeJsonConvert.DeserializeObject <ErrorResponse>(_responseContent, DeserializationSettings);
                    if (_errorBody != null)
                    {
                        ex.Body = _errorBody;
                    }
                }
                catch (JsonException)
                {
                    // Ignore the exception
                }
                ex.Request  = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
                ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
                if (_shouldTrace)
                {
                    ServiceClientTracing.Error(_invocationId, ex);
                }
                _httpRequest.Dispose();
                if (_httpResponse != null)
                {
                    _httpResponse.Dispose();
                }
                throw ex;
            }
            // Create Result
            var _result = new HttpOperationResponse <LanguageBatchResult>();

            _result.Request  = _httpRequest;
            _result.Response = _httpResponse;
            // Deserialize Response
            if ((int)_statusCode == 200)
            {
                _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                try
                {
                    _result.Body = SafeJsonConvert.DeserializeObject <LanguageBatchResult>(_responseContent, DeserializationSettings);
                }
                catch (JsonException ex)
                {
                    _httpRequest.Dispose();
                    if (_httpResponse != null)
                    {
                        _httpResponse.Dispose();
                    }
                    throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
                }
            }
            if (_shouldTrace)
            {
                ServiceClientTracing.Exit(_invocationId, _result);
            }
            return(_result);
        }
Exemplo n.º 13
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            string cognitive_service_key      = Environment.GetEnvironmentVariable("cognitive_service_key");
            string cognitive_service_endpoint = Environment.GetEnvironmentVariable("cognitive_service_endpoint");

            int SentencesToSummarize = 3;

            string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data        = JsonConvert.DeserializeObject(requestBody);
            string  inputText   = data.text;

            var credentials = new ApiKeyServiceClientCredentials(cognitive_service_key);
            var client      = new TextAnalyticsClient(credentials)
            {
                Endpoint = cognitive_service_endpoint
            };

            dynamic result = new JObject();

            //Detecting language first
            var inputDocuments = new LanguageBatchInput(
                new List <LanguageInput>
            {
                new LanguageInput(id: "1", text: inputText)
            });

            var langResults = await client.DetectLanguageAsync(false, inputDocuments);

            string inputLanguage = null;

            foreach (var document in langResults.Documents)
            {
                inputLanguage = document.DetectedLanguages[0].Iso6391Name;
            }

            result.language = inputLanguage;
            log.LogInformation($"{result.ToString()}");

            //Detecting sentiment of the input text
            var inputDocuments2 = new MultiLanguageBatchInput(
                new List <MultiLanguageInput>
            {
                new MultiLanguageInput(inputLanguage, "1", inputText)
            });

            var sentimentResult = await client.SentimentAsync(false, inputDocuments2);

            double?sentimentScore = 0;

            foreach (var document in sentimentResult.Documents)
            {
                sentimentScore = document.Score;
            }

            result.sentimentScore = sentimentScore;
            log.LogInformation($"{result.ToString()}");

            //Detecting entities in the text
            var entitiesResult = await client.EntitiesAsync(false, inputDocuments2);

            JArray entities = new JArray();

            foreach (var document in entitiesResult.Documents)
            {
                dynamic entityObject = new JObject();
                foreach (var entity in document.Entities)
                {
                    entityObject.name    = entity.Name;
                    entityObject.type    = entity.Type;
                    entityObject.subtype = entity.SubType;
                    foreach (var match in entity.Matches)
                    {
                        entityObject.offset = match.Offset;
                        entityObject.length = match.Length;
                        entityObject.score  = match.EntityTypeScore;
                        //log.LogInformation($"\t\t\tOffset: {match.Offset},\tLength: {match.Length},\tScore: {match.EntityTypeScore:F3}");
                    }
                    entities.Add(entityObject);
                }
            }
            result.entities = entities;
            log.LogInformation($"{result.ToString()}");

            //Detecting keyphrases
            var kpResults = await client.KeyPhrasesAsync(false, inputDocuments2);

            JArray keyPhrases = new JArray();
            var    Phrases    = new List <string>();

            // Printing keyphrases
            foreach (var document in kpResults.Documents)
            {
                foreach (string keyphrase in document.KeyPhrases)
                {
                    keyPhrases.Add(keyphrase);
                    Phrases.Add(keyphrase);
                }
            }
            result.keyphrases = keyPhrases;

            //Generating text summary
            String[] sentences = inputText.Split('!', '.', '?');

            List <Match> matchList = new List <Match>();
            int          counter   = 0;
            // Take the 10 best words
            var topPhrases = Phrases.Take(10);

            foreach (var sentence in sentences)
            {
                double count = 0;

                Match match = new Match();
                foreach (var phrase in topPhrases)
                {
                    if ((sentence.ToLower().IndexOf(phrase) > -1) &&
                        (sentence.Length > 20) && (WordCount(sentence) >= 3))
                    {
                        count++;
                    }
                    ;
                }

                if (count > 0)
                {
                    matchList.Add(new Match {
                        sentence = counter, total = count
                    });
                }
                counter++;
            }

            var           MatchList     = matchList.OrderByDescending(y => y.total).Take(SentencesToSummarize).OrderBy(x => x.sentence).ToList();
            StringBuilder summary       = new StringBuilder();
            List <string> SentenceList  = new List <string>();
            int           sentenceCount = 0;

            for (int i = 0; i < MatchList.Count; i++)
            {
                summary.Append(sentences[MatchList[i].sentence] + ".");
                sentenceCount++;
            }
            // If there are no sentences found, just take the first three
            if (sentenceCount == 0)
            {
                for (int i = 0; i < Math.Min(SentencesToSummarize, sentences.Count()); i++)
                {
                    summary.Append(sentences[MatchList[i].sentence] + ".");
                }
            }

            result.summary = summary.ToString();
            log.LogInformation($"{result.ToString()}");

            return(inputText != null
                ? (ActionResult) new OkObjectResult($"{result.ToString()}")
                : new BadRequestObjectResult("{ \"error\": \"Please pass the text input for the text analytics operations\""));
        }
Exemplo n.º 14
0
        /// <summary>
        /// The API returns the detected language and a numeric score between 0 and 1.
        /// </summary>
        /// <remarks>
        /// Scores close to 1 indicate 100% certainty that the identified language is
        /// true. A total of 120 languages are supported.
        /// </remarks>
        /// <param name='operations'>
        /// The operations group for this extension method.
        /// </param>
        /// <param name='showStats'>
        /// (optional) if set to true, response will contain input and document level
        /// statistics.
        /// </param>
        /// <param name='languageBatchInput'>
        /// Collection of documents to analyze.
        /// </param>
        /// <param name='cancellationToken'>
        /// The cancellation token.
        /// </param>
        public static LanguageBatchResult DetectLanguageBatch(this ITextAnalyticsClient operations, LanguageBatchInput languageBatchInput = default, bool?showStats = default, CancellationToken cancellationToken = default)
        {
            var _result = operations.DetectLanguageWithHttpMessagesAsync(showStats, languageBatchInput, null, cancellationToken).GetAwaiter().GetResult();

            return(_result.Body);
        }