Click here to Skip to main content
15,912,504 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
so I found some code from

[^]
that practically just takes in a word and tells you how many times it comes up in the text but I want to do his for every single word in the text.

What I have tried:

I have tried using foreach functionality and it doenst seem to work if anything it caused for there to be "0 occurences" of every word.
Posted
Updated 21-Aug-19 20:21pm

Simple solution: break each word into separate strings, then either sort them and compare adjacent words, or use Linq GroupBy to "gather them together", and ToDictionary to create a word + Count collection.
Nice and simple both ways, depending on your experience level.
 
Share this answer
 
Linq solution:
C#
string[] words = text.ToLower() //needed!
	.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries); 

var result = words
	.GroupBy(x=>x)
	.Select(grp=> new
	{
		word = grp.Key, 
		count = grp.Count()
	})
	.OrderByDescending(o=>o.count)
	.ToList();
		
foreach(var o in result)
{
	Console.WriteLine("{0} - {1}", o.word, o.count);
}



[EDIT]
Non-Linq solution:
C#
//declare file name
string filename = @"fullfilenamehere";
//grab text from file to string array
string[] lines = File.ReadAllLines(filename);
//create a dictionary, which will hold word and number of occurencies of that word
Dictionary<string, int> listofwords = new Dictionary<string, int>();
//for each line 
foreach(string line in lines)
{
	//split line into words
	string[] words = line.ToLower().Split(new char[] { '.', '?', '!', ' ', ';', ':', ',', '-' }, StringSplitOptions.RemoveEmptyEntries); 
	//for each word 
	foreach(string word in words)
		if(!listofwords.ContainsKey(word)) //if word does not exists on listofwords
			listofwords.Add(word, 1); //add word and set no. of occurencies to 1
		else
			listofwords[word] += 1; //increase no. of occurencies
}
//display result
Console.WriteLine("Word - No. of occurencies");
foreach(KeyValuePair<string, int> kvp in listofwords)
{
	Console.WriteLine("{0} - {1}", kvp.Key, kvp.Value);
}
 
Share this answer
 
v2
Comments
Richard Deeming 22-Aug-19 7:24am    
You can avoid the need for the ToLower call by passing StringComparer.OrdinalIgnoreCase to the GroupBy method.
Maciej Los 22-Aug-19 7:30am    
Yeah, this is another option.
Thanks, Richard.
HamzaMcBob 22-Aug-19 9:01am    
yeah this is beautiful code like literally this is amazing but its so dam high level that I am finding it difficult to understand and add on to , is it possible if you could explain it to me , you see I want to create it so it can compare 2 sets of text and say which one has more occurrences of a word. but one of the text is going to be an average of hundreds of texts as in I am comparing 1 text file to see how many occurrences of every word exist in it to an average of how many words exist in 100 text file
Maciej Los 22-Aug-19 9:25am    
Well, creating application which is be able to read over 100 text files and create statistics for each one and the final report, is not trivial job. You'll need a database or - at least - a dataset (without a database). This makes your program more difficult. If you are a beginner, i'd suggest to start with single file. I'll improve my answer to show you non-linq solution. It would be easier to understand what code is doing... OK?
HamzaMcBob 22-Aug-19 11:04am    
that makes so much more sense , oh my daze you absolute genius.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900