private static Dictionary<string, int> CountDistinctWordsInternal2(string str, WordCountOptions options, params char[] punctuations) { str.ThrowIfNull("str"); punctuations = punctuations.Length != 0 ? punctuations : Punctuations.Default; Dictionary<string, int> distinctWords = new Dictionary<string, int>(); var pset = new HashSet<char>(punctuations); int start = -1; for (int i = 0; i < str.Length; i++) { if (char.IsWhiteSpace(str[i])) { if (start != -1) { int end = i - 1; CountWord(GetWord(str, start, end, pset), distinctWords, options); start = -1; } continue; } if (start != -1 && i == str.Length - 1) { int end = str.Length - 1; CountWord(GetWord(str, start, end, pset), distinctWords, options); start = -1; continue; } if (start == -1) { start = i; } } return distinctWords; }
private static void CountWord(string word, Dictionary<string, int> words, WordCountOptions options) { if (word == string.Empty) { return; } if (options == WordCountOptions.CaseInsensitive) { word = word.ToLowerInvariant(); } int count = words.ContainsKey(word) ? words[word] : 0; words[word] = ++count; }
public static Dictionary<string, int> CountDistinctWords(this string str, WordCountOptions options, params char[] punctuations) { return CountDistinctWordsInternal2(str, options, punctuations); }