Example #1
0
        public static async Task <IActionResult> GetTextSentimentAzure(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "text/sentiment/azure")]
            HttpRequest request,
            TraceWriter traceWriter,
            ExecutionContext executionContext
            )
        {
            var appsettings = new AppSettings(executionContext);
            var azureCognitiveServicesConfig = appsettings.AzureCognitiveServicesConfig;

            _textAnalyticsClient = _textAnalyticsClient ?? GetTextAnalyticsClient(azureCognitiveServicesConfig);

            var documentBatch = JsonConvert.DeserializeObject <DocumentBatch>(await GetBodyAsString(request));

            var documents = documentBatch.Documents.Select((Models.Document doc) => new MultiLanguageInput
            {
                Id       = doc.Id,
                Language = doc.LanguageCode,
                Text     = doc.Text
            }).ToList();

            try
            {
                var sentimentBatchResult = await _textAnalyticsClient.SentimentAsync(new MultiLanguageBatchInput(documents));

                var result = DocumentBatchSentimentUtil.FromSentimentBatchResult(sentimentBatchResult);
                return(new OkObjectResult(result));
            }
            catch (Exception ex)
            {
                string error = ex.ToString() + ex.InnerException?.ToString();
                traceWriter.Error(error, ex);
                return(new BadRequestObjectResult(error));
            }
        }
Example #2
0
        public static async Task <IActionResult> GetTextSentimentGoogle(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "text/sentiment/google")]
            HttpRequest request,
            TraceWriter traceWriter,
            ExecutionContext executionContext
            )
        {
            var appsettings = new AppSettings(executionContext);

            string bodyString = await GetBodyAsString(request);

            var documentBatch = JsonConvert.DeserializeObject <DocumentBatch>(bodyString.Trim());

            //var creds = GoogleCredential.FromJson(File.ReadAllText("CommentQulity-19b70adeab44.json"));
            var creds        = GoogleCredential.FromJson(JsonConvert.SerializeObject(appsettings.GoogleCredentialsData));
            var channelCreds = creds.ToChannelCredentials();
            var channel      = new Channel(LanguageServiceClient.DefaultEndpoint.ToString(), channelCreds);

            var langServiceClient = LanguageServiceClient.Create(channel);

            var analyzeSentimentRequest  = GetSentimentAnalysisRequest(documentBatch);
            var analyzeSentimentResponse = langServiceClient.AnalyzeSentiment(analyzeSentimentRequest);

            var result = DocumentBatchSentimentUtil.FromAnnotatedAnalyzeSentimentResponse(analyzeSentimentResponse);

            return(new ObjectResult(result));
        }