Output Program
I say that if you this sent program run will see that single words and same sentences repeatly demonstrate and their frequency separatly 1 demonstrate.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
ensie safdariPosted Jun 13, 2012, 12:10 PM
I have a code that not general (not for all string files) .i want be follow code generall.
this code dont compare a words and sentences and two words and three words.
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(); }
vinay gnPosted Jun 13, 2012, 1:50 AM
Could u be more clear on your Question, whoever views your post are not getting whats your Question is all about...
Regards,
VinayGN