public string Start([FromBody] ItemData itemData) { string moderatedOutput = ""; var item = SitecoreHelpers.GetItemById(itemData.ItemId); if (itemData.IsImage) { var imgStream = SitecoreHelpers.GetImageStream(item, itemData.FieldName); } else { var text = item.Fields[itemData.FieldName].Value.StripHtml(); var mod = new ModeratorApiTextModel() { autoCorrect = true, contentType = (CMConstants.MediaType)Enum.Parse(typeof(CMConstants.MediaType), "Plain"), language = "eng", detectPii = true, textToScreen = text }; var tempmoderatedOutput = GetModeratedOutput(mod); Task.WaitAll(tempmoderatedOutput); moderatedOutput = tempmoderatedOutput.Result; } return(moderatedOutput); }
/// <summary> /// Moderates the text to check for inappropriate content /// </summary> /// <param name="textToScreen">Text to be moderated</param> /// <param name="contentType">Content Type of the input text to be moderated</param> /// <param name="language">Language of the input text to be moderated</param> /// <param name="autoCorrect">Enable Autocorrection on the text to be moderated</param> /// <param name="identifyUrls">Enable Url identification on the text to be moderated</param> /// <param name="detectPii">Detect Personal Identifiable Information on the text moderation</param> /// <param name="listId">ListId for the Content Moderation API</param> /// <returns>Moderated string with the inappropriate words highlighted</returns> public static async Task <String> ModerateText(ModeratorApiTextModel moderationTextApiInput) { var inputText = moderationTextApiInput.textToScreen; var responseText = string.Empty; var wrapTemplate = "<span data-cls='moderator-highlight'>{0}</span>"; var chrCount = 0; List <string> convertedItem = new List <string>(); // Invoke the Moderation Client API ModeratorClient moderatorClient = new ModeratorClient(SUBSCRIPTIONKEY); // Split the job into batches of character limit var lines = inputText.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) .GroupBy(w => (chrCount += w.Length + 1) / APICHARACTERLIMIT) .Select(g => string.Join(" ", g)); // Perform the text operation based on the character limit the API supports foreach (var line in lines) { var splitText = line; var moderatedTextResponse = await moderatorClient.ScreenTextAsync(splitText, moderationTextApiInput.contentType, moderationTextApiInput.language, moderationTextApiInput.autoCorrect, moderationTextApiInput.identifyUrls, moderationTextApiInput.detectPii, moderationTextApiInput.listId); // Analyze the reponse object for inappropriate content foreach (TextContract.MatchTerm item in ((TextContract.ScreenTextResult)moderatedTextResponse).Terms) { // Process the current inappropriate content, if not already processed if (!convertedItem.Contains(item.Term)) { // Keep Track of the inappropriate word convertedItem.Add(item.Term); // Wrap it with a span tag and class to highlight it splitText = Regex.Replace(splitText, item.Term, string.Format(wrapTemplate, item.Term), RegexOptions.Singleline | RegexOptions.IgnoreCase); } } // Merge the moderated text back into the final response responseText += splitText.Replace("data-cls", "class"); } // Return the final response with the inappropriate words highlighted Console.WriteLine(responseText); return(responseText); }
async Task <string> GetModeratedOutput(ModeratorApiTextModel toModerate) { return(await Helpers.Utilities.ContentModeratorApiHelper.ModerateText(toModerate)); }