Reputation: 23
Am newbie here and tried the search, but not quite understood it, so I am thinking to ask to the forum for help.
I want to get the result into the text box from the following code but got an error. Confused on how to overcome it, appreciate for any help. I believe it was an error on the conversion from linqIgroup to string to be put in textboxt.Text
It's about to display the most word(s) that has been occurred in a text file.
string sentence;
string[] result = {""};
sentence = txtParagraph.Text;
char[] delimiters = new char[] { ' ', '.', '?', '!' };
string[] splitStr = sentence.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
var dic = splitStr.ToLookup(w => w.ToLowerInvariant());
var orderedDic = dic.OrderByDescending(g => g.Count(m=>m.First()).ToString()));
txtFreqWord.Text = orderedDic.ToString();
Upvotes: 2
Views: 78
Reputation: 4726
Try the following to do what you are after. I am using regular expressions aswell.
var resultsList = System.Text.RegularExpressions.Regex.Split("normal text here normal normal".ToLower(), @"\W+")
.Where(s => s.Length > 3)
.GroupBy(s => s)
.OrderByDescending(g => g.Count());
string mostFrequent = resultsList.FirstOrDefault().Key;
To get all of them with their count, do the following :
foreach (var x in resultsList) { txtFreqWord.Text = txtFreqWord.Text + x.Key + " " + x.Count() +", "; }
Upvotes: 2