public static bool checkPhraseFoundSimple(string Content, PhraseFilter filter, out string context) { context = ""; if ((filter.Phrase ?? "") == "") { return(false); } bool found = false; Content = Content.ToLower(); switch (filter.Type) { case BlockPhraseType.CONTAIN: var index = Content.IndexOf(filter.Phrase); if (index > -1) { context = getWordSurrounding(Content, index, filter.Phrase.Length); } found = index > -1; break; case BlockPhraseType.REGEX: var match = Regex.Match(Content, filter.Phrase); if (match.Groups[0].Length > 0) { context = getWordSurrounding(Content, match.Groups[0].Index, match.Groups[0].Length); } found = match.Groups[0].Length > 0; break; } return(found); }
/// <summary> /// Find the phrase that the content is blocked from using. /// </summary> /// <param name="Content">string chunk</param> /// <returns>Null if no phrase rule is applicabalbe (allowed)</returns> public PhraseFilter findBlockingPhrase(string Content, BlockPhraseScope scope, out string context) { PhraseFilter result = null; List <string> Words = getWords(Content); context = ""; for (int i = 0; i < BlockedPhrases.Count && (result == null); i++) { if ( // Any of the scope is ALL scope == BlockPhraseScope.ANY || BlockedPhrases[i].Scope == BlockPhraseScope.ANY // Scopes are equal: || BlockedPhrases[i].Scope == scope ) { switch (BlockedPhrases[i].Type) { case BlockPhraseType.CONTAIN: case BlockPhraseType.REGEX: if (checkPhraseFoundSimple(Content, BlockedPhrases[i], out context)) { result = BlockedPhrases[i]; } break; case BlockPhraseType.EXACTWORD: case BlockPhraseType.WORDCONTAINING: if (checkPhraseFoundWord(Words, BlockedPhrases[i], out context)) { result = BlockedPhrases[i]; } break; } } } return(result); }
public static bool checkPhraseFoundWord(List <string> words, PhraseFilter filter, out string context) { context = ""; if ((filter.Phrase ?? "") == "") { return(false); } bool found = false; int index = 0; switch (filter.Type) { case BlockPhraseType.EXACTWORD: index = words.IndexOf(filter.Phrase.ToLower()); if (index > -1) { context = words[index]; } found = index > -1; break; case BlockPhraseType.WORDCONTAINING: index = words.FindIndex((word) => word.Contains(filter.Phrase.ToLower())); if (index > -1) { context = words[index]; } found = index > -1; break; } context = "_<" + context + ">_"; return(found); }