/// <summary>
        /// Main execution method for activity
        /// </summary>
        /// <param name="context"></param>
        protected TextEvaluationOutputModel Execute(string Text, string apiKey = "")
        {
            TextEvaluationOutputModel _contentModeratorOutput = new TextEvaluationOutputModel();
            var textInput = Text;

            textInput = textInput.Replace(System.Environment.NewLine, " ");
            if (apiKey != "")
            {
                // Sets to the user's apiKey, if supplied; if not, defaults to a free key
                ContentModeratorHelper.ApiKey = apiKey;
            }

            try
            {
                // Screen the input text: check for profanity, classify the text into three categories
                // do autocorrect text, and check for personally identifying
                // information (PII)
                _contentModeratorOutput = ContentModeratorHelper.GetTextEvaluationOutput(textInput);
                if (_contentModeratorOutput == null)
                {
                    throw new System.Exception("Null Output");
                }
                else
                {
                    return(_contentModeratorOutput);
                }
            }
            catch (System.Exception ex)
            {
                Debug.WriteLine(ex.InnerException);
                Debug.WriteLine(ex.Message);
                throw new System.Exception($"Actual Exception Message: {ex.Message}\n{TextModerator.GeneralException}");
            }
        }
Пример #2
0
        /// <summary>
        /// Main activity method
        /// </summary>
        /// <param name="context"></param>
        protected override void Execute(CodeActivityContext context)
        {
            var textInput = Text.Get(context);

            textInput = textInput.Replace(System.Environment.NewLine, " ");
            var apiKey = ApiKey.Get(context);

            if (apiKey != null)
            {
                // Sets to the user's apiKey, if supplied; if not, defaults to a free key
                ContentModeratorHelper.ApiKey = apiKey;
            }

            try
            {
                // Screen the input text: check for profanity, classify the text into three categories
                // do autocorrect text, and check for personally identifying
                // information (PII)
                _contentModeratorOutput = ContentModeratorHelper.GetTextEvaluationOutput(textInput);
                if (_contentModeratorOutput == null)
                {
                    throw new System.Exception("Null Output");
                }
                else
                {
                    FriendlyOutput.Set(context, _contentModeratorOutput.FriendlyOutput);
                    JsonOutput.Set(context, _contentModeratorOutput.JsonOutput);
                    AutoCorrectedText.Set(context, _contentModeratorOutput.AutoCorrectedText);
                    NormalizedText.Set(context, _contentModeratorOutput.NormalizedText);
                    ProfanityCategory1Score.Set(context, _contentModeratorOutput.ProfanityCategory1Score);
                    ProfanityCategory2Score.Set(context, _contentModeratorOutput.ProfanityCategory2Score);
                    ProfanityCategory3Score.Set(context, _contentModeratorOutput.ProfanityCategory3Score);
                    ReviewRecommended.Set(context, _contentModeratorOutput.ReviewRecommended);
                }
            }
            catch (System.Exception ex)
            {
                throw new System.Exception($"Actual Exception Message: {ex.Message}\n{GeneralException}");
            }
        }
        /// <summary>
        /// Returns the text evalulation output based on the input text
        /// </summary>
        /// <param name="textInput"></param>
        /// <returns>The text evaluation output</returns>
        public static TextEvaluationOutputModel GetTextEvaluationOutput(string textInput)
        {
            var           output         = new TextEvaluationOutputModel();
            StringBuilder friendlyOutput = new StringBuilder();
            string        jsonOutput;

            using (var client = ContentModeratorHelper.NewModeratorClient())
            {
                // Screen the input text: check for profanity, classify the text into three categories
                // do autocorrect text, and check for personally identifying
                // information (PII)
                using (var stream = GenerateStreamFromString(textInput))
                {
                    //var detectedLanguage = client.TextModeration.DetectLanguage("text/plain", stream).DetectedLanguageProperty;
                    var screenResult = client.TextModeration.ScreenText("text/plain", stream, null, true, true, null, true);
                    jsonOutput = Newtonsoft.Json.JsonConvert.SerializeObject(screenResult);
                    var autoCorrectedText       = screenResult?.AutoCorrectedText;
                    var normalizedText          = screenResult?.NormalizedText;
                    var profanityCategory1Score = screenResult?.Classification?.Category1?.Score;
                    var profanityCategory2Score = screenResult?.Classification?.Category2?.Score;
                    var profanityCategory3Score = screenResult?.Classification?.Category3?.Score;
                    var reviewRecommended       = screenResult?.Classification?.ReviewRecommended;
                    friendlyOutput.AppendLine("Autocorrect typos, check for matching terms, PII, and classify.");
                    friendlyOutput.AppendLine($"===Original text: ");
                    friendlyOutput.AppendLine($"> {screenResult?.OriginalText}");
                    friendlyOutput.AppendLine($"===Normalized Text: ");
                    friendlyOutput.AppendLine($"> {normalizedText}");
                    friendlyOutput.AppendLine($"===Autocorrected Text: {autoCorrectedText}");
                    friendlyOutput.AppendLine($"> {autoCorrectedText}");
                    friendlyOutput.AppendLine($"===Classification:");
                    friendlyOutput.AppendLine($"> Category 1 Score (potential presence of language that may be considered sexually explicit or adult in certain situations): {profanityCategory1Score}");
                    friendlyOutput.AppendLine($"> Category 2 Score (potential presence of language that may be considered sexually suggestive or mature in certain situations): {profanityCategory2Score}");
                    friendlyOutput.AppendLine($"> Category 3 Score (potential presence of language that may be considered offensive in certain situations): {profanityCategory3Score}");
                    friendlyOutput.AppendLine($"> Review Recommended?: {reviewRecommended}");
                    friendlyOutput.AppendLine($"===Language: ");
                    friendlyOutput.AppendLine($"> {screenResult?.Language}");
                    friendlyOutput.AppendLine($"===Personally Identifiable Information:");
                    friendlyOutput.AppendLine($">Address(es): ");
                    foreach (var item in screenResult?.PII?.Address)
                    {
                        friendlyOutput.AppendLine($"  Index -- {item?.Index}");
                        friendlyOutput.AppendLine($"  Text -- {item?.Text}");
                    }
                    friendlyOutput.AppendLine($"> Email(s): ");
                    foreach (var item in screenResult?.PII?.Email)
                    {
                        friendlyOutput.AppendLine($"  Detected -- {item?.Detected}");
                        friendlyOutput.AppendLine($"  Subtype -- {item?.SubType}");
                        friendlyOutput.AppendLine($"  Index -- {item?.Index}");
                        friendlyOutput.AppendLine($"  Text -- {item?.Text}");
                    }
                    friendlyOutput.AppendLine($"> IPA(s): ");
                    foreach (var item in screenResult?.PII?.IPA)
                    {
                        friendlyOutput.AppendLine($"  Subtype -- {item?.SubType}");
                        friendlyOutput.AppendLine($"  Index -- {item?.Index}");
                        friendlyOutput.AppendLine($"  Text -- {item?.Text}");
                    }
                    friendlyOutput.AppendLine($"> Phone(s): ");
                    foreach (var item in screenResult?.PII?.Phone)
                    {
                        friendlyOutput.AppendLine($"  CountryCode -- {item?.CountryCode}");
                        friendlyOutput.AppendLine($"  Index -- {item?.Index}");
                        friendlyOutput.AppendLine($"  Text -- {item?.Text}");
                    }
                    friendlyOutput.AppendLine($"> Social Security Number(s): ");
                    foreach (var item in screenResult?.PII?.SSN)
                    {
                        friendlyOutput.AppendLine($"  Index -- {item?.Index}");
                        friendlyOutput.AppendLine($"  Text -- {item?.Text}");
                    }
                    friendlyOutput.AppendLine($"===Status: ");
                    friendlyOutput.AppendLine($"> Code: {screenResult?.Status?.Code}");
                    friendlyOutput.AppendLine($"> Description: {screenResult?.Status?.Description}");
                    friendlyOutput.AppendLine($"> Exception: {screenResult?.Status?.Exception}");
                    friendlyOutput.AppendLine($"===Terms: ");
                    if (screenResult?.Terms != null)
                    {
                        foreach (var item in screenResult?.Terms)
                        {
                            friendlyOutput.AppendLine($"> ListId -- {item?.ListId}");
                            friendlyOutput.AppendLine($"> Original Index -- {item?.OriginalIndex}");
                            friendlyOutput.AppendLine($"> Index -- {item?.Index}");
                            friendlyOutput.AppendLine($"> Term -- {item?.Term}");
                        }
                    }
                    friendlyOutput.AppendLine($"===Transaction Id: ");
                    friendlyOutput.AppendLine($"> {screenResult?.TrackingId}");

                    output.FriendlyOutput          = friendlyOutput?.ToString();
                    output.JsonOutput              = jsonOutput;
                    output.AutoCorrectedText       = autoCorrectedText;
                    output.NormalizedText          = normalizedText;
                    output.ProfanityCategory1Score = profanityCategory1Score;
                    output.ProfanityCategory2Score = profanityCategory2Score;
                    output.ProfanityCategory3Score = profanityCategory3Score;
                    output.ReviewRecommended       = reviewRecommended;
                }
            }

            return(output);
        }