2

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();
2
  • 3
    "but got an error" --- is it a secret error? Commented Jun 12, 2013 at 23:37
  • You can use String.Join on the dictionary's Values property, if that's what you're trying to do. Commented Jun 12, 2013 at 23:37

1 Answer 1

2

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() +", ";
            }
Sign up to request clarification or add additional context in comments.

8 Comments

Stephen, you nail it, but it takes me to understand the logic behind it, sorry haven't got any reputation to vote up. Thanks a lot. The key is on: string mostFrequent = resultsList.FirstOrDefault().Key;
Will do John, one more question if you don't mind, how about if I wanna show the whole keys(Words) which has occurred most, example: John(occured 3x), David(occured 3x) instead only John, should I pass it into string array?
Thank you fr your answer, it's helpful, but maybe it was my wrong on my comment, what I meant was that I need to show on my Textbox both name: John, David; while on the first answer would only come up with one name. And for the updated answer, I am a bit confused on how to put it into a Textbox, will it show ex: John, David. I tried to do it but it seems it didn't update the answer on the Textbox.
Updated my answer to show you how to add text to the textbox.
Thanks Stephen, try to fiddle diddle with it to get only display the most Word(s) that occurred. Tried this: txtFreqWord.Text = txtFreqWord.Text + x.Key + ", "; but it will display the whole words in descending order of occurrances, I guess I have to put something afer x.Key so I had tried Max(), IndexOf, but they gave me only first element of the word (char) and error instead only the top occurrence Word(s), any final tip?, >.<
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.