public string GetFilteredText(string content) { // assuming that words in store are written in lowercase string[] wordsToFilter = _wordsDataStore.GetWords(); // initial filtering string cleanString = Regex.Replace(content, @"[^ .:a-zA-Z0-9\-]", ""); // word filtering var re = new Regex( @"\b(" + string.Join("|", wordsToFilter.Select(word => string.Join(@"\s*", word.ToCharArray()))) + @")\b", RegexOptions.IgnoreCase); return(re.Replace(cleanString, match => { if (match.Length > 2) { return match.Value[0] + new string('*', match.Length - 2) + match.Value[match.Length - 1]; } else { return new string('*', match.Length); } })); }
public int CountWordsFromDatastore(string content) { // assuming that words in store are written in lowercase string[] wordsToCount = _wordsDataStore.GetWords(); string[] source = content.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries); var matchQuery = from word in source where wordsToCount.Contains(word.ToLowerInvariant()) select word; int wordCount = matchQuery.Count(); return(wordCount); }