I need to program that from Vulpes that general solution and checking number of frequency on all steps correctly.(follow program is exclusive)
class Program{
static void Main(){
Dictionary<string, int> dict = new Dictionary<string, int>();
string text = "The quick brown fox jumps over the quick lazy . dog and the quick brown cat.";
text = text.ToLower();
string[] sentences = text.Split(new char[]{'.'}, StringSplitOptions.RemoveEmptyEntries);
// get all phrases in each sentence
foreach(string sentence in sentences){
string temp = sentence + " the";
string[] words = temp.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
List<string> phrases = new List<string>();
foreach(string word in words){
if (word == "the" || word == "and"){
if (phrases.Count == 0) continue;
string phrase = String.Join(" ", phrases.ToArray());
if (!dict.ContainsKey(phrase)){
dict.Add(phrase, 1);
} else
{ dict[phrase]++;
}phrases.Clear();
}else{phrases.Add(word);}
}
}
foreach(string key in dict.Keys) Console.WriteLine("{0} : {1}", key, dict[key]);
Console.WriteLine();
// now get all single word phrases
dict.Clear();
foreach(string sentence in sentences) {
string[] words = sentence.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
foreach(string word in words) {
if (word == "the" || word == "and") continue;
if (!dict.ContainsKey(word)){
dict.Add(word, 1);
}else{
dict[word]++; }
}
}
foreach(string key in dict.Keys) Console.WriteLine("{0} : {1}", key, dict[key]);
Console.WriteLine();
// now get all two word phrases
dict.Clear();
foreach(string sentence in sentences){
string[] words = sentence.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
for(int i = 0; i < words.Length - 1; i++){
if (words[i] == "the" || words[i] == "and") continue;
if (words[i + 1] != "the" && words[i + 1] != "and"){
string phrase = words[i] + " " + words[i + 1];
if (!dict.ContainsKey(phrase)){
dict.Add(phrase, 1);}
else {
dict[phrase]++;}
}
}
}
foreach(string key in dict.Keys) Console.WriteLine("{0} : {1}", key, dict[key]);
Console.WriteLine();
// now get all three word phrases
dict.Clear();
foreach(string sentence in sentences){
string[] words = sentence.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
for(int i = 0; i < words.Length - 2; i++){
if (words[i] == "the" || words[i] == "and") continue;
if (words[i + 1] != "the" && words[i + 1] != "and" && words[i + 2] != "the" && words[i + 2] != "and"){
string phrase = words[i] + " " + words[i + 1] + " " + words[i + 2];
if (!dict.ContainsKey(phrase)) {
dict.Add(phrase, 1);}
else{
dict[phrase]++;
}
}
}
}
foreach(string key in dict.Keys) Console.WriteLine("{0} : {1}", key, dict[key]);
Console.ReadKey();
}
}