Julien Powell
Julien Powell

Reputation: 176

Dictionary re-adding words as new keys instead of increasing value

I am writing a program that finds every unique word in a text and prints it in a text box. I do this by printing each key in a dictionary however my dictionary is adding each word as a separate key instead of ignoring words that are already there.

The function is being called correctly and it does work it simpy prints the entire text I hand it however.

EDIT: I am reading the string from a text file then sending it to the function. This is the input string and the output:

Output:

To be or not to that is the question Whether tis nobler in mind suffer The slings and arrows of outrageous fortune Or take arms against a sea troubles And by opposing end them die sleep No more sleep say we end The heartache thousand natural shocks That flesh heir Tis consummation

public string FindUniqueWords(string text)
{
    Dictionary<string, int> dictionary = new Dictionary<string, int>();

    string uniqueWord = "";
    text = text.Replace(",", ""); //Just cleaning up a bit
    text = text.Replace(".", ""); //Just cleaning up a bit
    string[] arr = text.Split(' '); //Create an array of words

    foreach (string word in arr) //let's loop over the words
    {
        if (dictionary.ContainsKey(word)) //if it's in the dictionary
            dictionary[word] = dictionary[word] + 1; //Increment the count
        else
            dictionary[word] = 1; //put it in the dictionary with a count 1
    }

    foreach (KeyValuePair<string, int> pair in dictionary) //loop through the dictionary
    {
        uniqueWord += (pair.Key + " ");
    }
    uniqueWords.Text = uniqueWord;
    return ("");
}

Upvotes: 0

Views: 117

Answers (3)

Henrik
Henrik

Reputation: 23324

You're reading the text with System.IO.File.ReadAllText, so text may also contain newline characters.

Replace arr = text.Split(' ') by arr = text.Split(' ', '\r', '\n') or add another replace: text = text.Replace(Environment.NewLine, " ");

Of course, by looking at arr in the debugger, you could have found out by yourself.

Upvotes: 2

pyrocumulus
pyrocumulus

Reputation: 9290

Your code works as it's supposed to (ignoring case though). The problem almost certainly lies with showing the results in your application, or with how you are calling the FindUniqueWords method (not the complete text at once).

Also, pretty important to note here: a Dictionary<TKey, TValue> by default simply cannot contain a single key multiple times. It would defeat the whole purpose of the dictionary in the first place. It's only possible if you override the Equality comparison somewhere, which you aren't doing.

If I try your code, with the following input:

To be or not to that is is is is is is is the question

The output becomes :

To be or not to that is the question

It works like it's supposed to.

Upvotes: -1

Cadburry
Cadburry

Reputation: 1864

A shorter way: (Dont forget to use Using System.Linq)

    string strInput = "TEST TEST Text 123";
    var words = strInput.Split().Distinct();                
    foreach (var word in words )
    {
        Console.WriteLine(word);
    }

Upvotes: 1

Related Questions