28 lines
781 B
C#
28 lines
781 B
C#
|
using System.Text.RegularExpressions;
|
||
|
using System.Collections.Generic;
|
||
|
|
||
|
class Phrase {
|
||
|
public static Dictionary<string,int> WordCount(string phrase) {
|
||
|
Dictionary<string,int> ret = new Dictionary<string,int>();
|
||
|
Regex rgx = new Regex("[^a-zA-Z0-9 ,']");
|
||
|
phrase = rgx.Replace(phrase, "");
|
||
|
string[] words = Regex.Split(phrase, @"[\s,]");
|
||
|
char[] trimChars = new char[2];
|
||
|
trimChars[0] = ' ';
|
||
|
trimChars[1] = '\'';
|
||
|
foreach(string wrd in words) {
|
||
|
string normWrd = wrd.ToLower().Trim(trimChars);
|
||
|
if(normWrd != "") {
|
||
|
int num = 0;
|
||
|
if(ret.ContainsKey(normWrd)) {
|
||
|
ret.TryGetValue(normWrd, out num);
|
||
|
ret.Remove(normWrd);
|
||
|
}
|
||
|
num++;
|
||
|
ret.Add(normWrd, num);
|
||
|
}
|
||
|
}
|
||
|
return ret;
|
||
|
}
|
||
|
}
|