/// <summary> /// Task 3 /// /// Part A) /// /// Create a function which counts the number of occurrences of words from a "censored words list" in a text. /// /// Example: /// /// Input: /// /// {"dog", "cat", "large"} and "I have a cat named Meow and a dog name Woof. I love the dog a lot. He is larger than a small horse." /// Output: /// cat: 1, dog: 2, large: 1, total: 4 /// /// </summary> /// <returns>The word count.</returns> /// <param name="text">Text.</param> /// <param name="words">Words.</param> /// <param name="matchSubstring">set false to avoid censoring words within words (e.g. "A school in Scunthorpe couldn't access their own website for weeks.</param> public WordCount GetWordCount(string text, string [] words, bool matchSubstring = true) { WordCount wordCount = new WordCount(); text = " " + text.ToLower(); foreach (string word in words) { string pattern = matchSubstring? word : @"\W" + word; wordCount.Add(word, CountInString(text, pattern)); } return(wordCount); }
public override void ReadEntity(IDictionary <string, EntityProperty> properties, OperationContext operationContext) { base.ReadEntity(properties, operationContext); foreach (var entityProperty in properties) { if (entityProperty.Key.StartsWith("WordCount_")) { WordCount.Add(entityProperty.Key.Substring(10), entityProperty.Value.Int32Value ?? 0); } } }
public void CountWords() { //creates dictionary <string, int> to store words and their counts foreach (var word in Words) { if (!WordCount.ContainsKey(word.ToString().Trim())) { WordCount.Add(word, 1); } else { WordCount[word] += 1; } } }