public static double GetCostEstimation( TimeSpan timeSpan, int numberOfChannels, bool isCustomModel, SentimentAnalysisSetting sentimentSetting, EntityRedactionSetting entityRedactionSetting) { double costPerHour = isCustomModel ? STTCustomModelCostPerHour : STTCostPerHour; var price = timeSpan.TotalHours * costPerHour; if (sentimentSetting != SentimentAnalysisSetting.None) { price += timeSpan.TotalHours * TextAnalyticsCostPerHour; } if (entityRedactionSetting != EntityRedactionSetting.None) { price += timeSpan.TotalHours * TextAnalyticsCostPerHour; } price *= numberOfChannels; return(price); }
private async Task <List <string> > RedactEntitiesInSpeechTranscriptAsync(List <HttpResponseMessage> responses, SpeechTranscript speechTranscript, EntityRedactionSetting entityRedactionSetting) { var entityRedactionErrors = new List <string>(); if (!speechTranscript.RecognizedPhrases.Any()) { return(entityRedactionErrors); } var displayFull = new List <string>(); var fullTranscriptionPerChannelDict = new Dictionary <int, StringBuilder>(); foreach (var message in responses) { message.EnsureSuccessStatusCode(); var responseBody = await message.Content.ReadAsStringAsync().ConfigureAwait(false); var textAnalyticsResponse = JsonConvert.DeserializeObject <TextAnalyticsResponse>(responseBody); foreach (var error in textAnalyticsResponse.Errors) { var errorMessage = $"Entity redaction failed with error: {error.Error.InnerError.Message}"; Log.LogError(errorMessage); entityRedactionErrors.Add(errorMessage); } foreach (var document in textAnalyticsResponse.Documents) { if (entityRedactionSetting == EntityRedactionSetting.UtteranceLevel) { var phrase = speechTranscript.RecognizedPhrases.Where(e => $"{e.Channel}_{e.Offset}".Equals(document.Id, StringComparison.Ordinal)).FirstOrDefault(); // Remove all text but the display form if (phrase.NBest == null || !phrase.NBest.Any()) { continue; } var nBest = phrase.NBest.FirstOrDefault(); nBest.ITN = string.Empty; nBest.MaskedITN = string.Empty; nBest.Lexical = string.Empty; // Remove word level timestamps if they exist nBest.Words = null; nBest.Display = RedactEntitiesInText(nBest.Display, document.Entities); phrase.NBest = new[] { nBest }; if (fullTranscriptionPerChannelDict.ContainsKey(phrase.Channel)) { fullTranscriptionPerChannelDict[phrase.Channel].Append(" " + nBest.Display); } else { fullTranscriptionPerChannelDict.Add(phrase.Channel, new StringBuilder(nBest.Display)); } } } } if (entityRedactionSetting == EntityRedactionSetting.UtteranceLevel) { foreach (var combinedRecognizedPhrase in speechTranscript.CombinedRecognizedPhrases) { var channel = combinedRecognizedPhrase.Channel; // Remove full transcription for channel: combinedRecognizedPhrase.MaskedITN = string.Empty; combinedRecognizedPhrase.ITN = string.Empty; combinedRecognizedPhrase.Lexical = string.Empty; combinedRecognizedPhrase.Display = string.Empty; if (fullTranscriptionPerChannelDict.ContainsKey(channel)) { combinedRecognizedPhrase.Display = fullTranscriptionPerChannelDict[channel].ToString(); } } } return(entityRedactionErrors); }
public async Task <IEnumerable <string> > RedactEntitiesAsync(SpeechTranscript speechTranscript, EntityRedactionSetting entityRedactionSetting) { if (speechTranscript == null) { throw new ArgumentNullException(nameof(speechTranscript)); } var entityRedactionErrors = new List <string>(); try { var textAnalyticsChunks = new List <TextAnalyticsRequestsChunk>(); if (entityRedactionSetting == EntityRedactionSetting.UtteranceLevel) { textAnalyticsChunks = CreateUtteranceLevelRequests(speechTranscript, EntityRecognitionRequestLimit); } var responses = new List <HttpResponseMessage>(); foreach (var chunk in textAnalyticsChunks) { var chunkString = JsonConvert.SerializeObject(chunk); var response = await MakeRequestAsync(chunkString, EntityRecognitionSuffix).ConfigureAwait(false); responses.Add(response); } Log.LogInformation($"Total responses: {responses.Count}"); entityRedactionErrors = await RedactEntitiesInSpeechTranscriptAsync(responses, speechTranscript, entityRedactionSetting).ConfigureAwait(false); return(entityRedactionErrors); } catch (Exception e) { var entityRedactionError = $"Entity Redaction failed with exception: {e.Message}"; Log.LogError(entityRedactionError); entityRedactionErrors.Add(entityRedactionError); return(entityRedactionErrors); } }