0

Here is my code:

string StringFromTheInput = TextBox1.Text;

string source = StringFromTheInput.ToString();

var frequencies = new Dictionary<string, int>();
frequencies.Add("item", 0);
string highestWord = null;

var message = string.Join(" ", source);
var splichar = new char[] { ' ', '.' };
var single = message.Split(splichar);
           
int highestFreq = 0;

foreach (var item in single)
{
    if (item.Length > 4)
    {
        int freq;
        frequencies.TryGetValue(item, out freq);
        freq += 1;

        if (freq> highestFreq)
        {
            highestFreq = freq;
            highestWord = item.Trim();
        }
                        
        frequencies[item] = freq;
        Label1.Text = highestWord.ToString();
    }
                
}

This is successfully gets me the most frequent word from the text but I tried to increment highestFreq= freq+1 to get the second most frequent word but it doesn`t work!

Fildor
  • 14,510
  • 4
  • 35
  • 67

3 Answers3

3

Can you use Linq or is this homework?

using System;
using System.Linq;

string StringFromTheInput = "Her life in the confines of the house became her new normal. He wondered if she would appreciate his toenail collection. My secretary is the only person who truly understands my stamp-collecting obsession. This is the last random sentence I will be writing and I am going to stop mid-sent. She tilted her head back and let whip cream stream into her mouth while taking a bath.";

string[] words = StringFromTheInput.Split(" ");
var setsByFrequency = words
    .Where(x => x.Length > 4)   // For words with more than 4 characters
    .GroupBy(x => x.ToLower()) // ToLower so 'House' and 'house' both gets placed into the same group
    .Select(g => new { Freq = g.Count(), Word = g.Key})  
    .OrderByDescending(g => g.Freq)
    .ToList();

var mostFrequent = setsByFrequency[0];
var secondMostFrequent = setsByFrequency[1];

Console.WriteLine(mostFrequent);
Console.WriteLine(secondMostFrequent);
LoLo2207
  • 103
  • 5
  • `ToLower()` is not a bad start but may not always work. See also https://learn.microsoft.com/en-us/dotnet/api/system.string.tolowerinvariant?redirectedfrom=MSDN&view=net-7.0#System_String_ToLowerInvariant – Fildor Nov 10 '22 at 07:54
  • This works well can you help me please at this one? https://stackoverflow.com/questions/74390064/find-a-word-in-a-textbox1-and-if-it-found-add-the-line-to-textbox2?noredirect=1#comment131325430_74390064 – user20465780 Nov 10 '22 at 14:24
0

you can order your Dictionary by their value, then take the first, second... element found.

int indexYouNeed =2;
int indexRead=1;
string textFound="";
foreach (KeyValuePair<string,int> entry in frequencies.Where(x=>x.Key.Length>4).OrderBy(x=>x.Value))
{
    if(indexRead==indexYouNeed)
    {
        textFound=entry.Key;
        break;
    }
    indexRead++;
}
Siegfried.V
  • 1,508
  • 1
  • 16
  • 34
  • 1
    If you have an ordered collection, you can also index the second element. You'd only need to loop if you wanted to make sure the second element has actually less count than the first instead of equal. – Fildor Nov 10 '22 at 07:43
  • @Fildor I know about Ordered collections, but to be honnest, I never used them, that's why I offered a solution that I understand (and so he would). But you're right, if anybody would answer with ordered collection, this would be the best solution. – Siegfried.V Nov 10 '22 at 07:48
0

I would do something like this (simple and easy to understand) First, load the dictionary with your words and the frequency (only words with more than 4 characters, because I saw in your question that), in this case the dictionary must ignore case. Second, order the dictionary and take the second (please check if it is empty, or if it has only 1 element before trying to access):

string StringFromTheInput = "";
var wordsFreq = new Dictionary<string,int>(StringComparer.OrdinalIgnoreCase);
foreach(var s in StringFromTheInput.Split(' ')){
    if(s.Length <=4) continue;
    if(wordsFreq.ContainsKey(s)){
        wordsFreq[s]++; 
    }else{
        wordsFreq.Add(s,1);
    }
}
if(!wordsFreq.Any()) return;
var secondFreqWord = wordsFreq.OrderByDescending(x => x.Value).ToList()[1];