• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Finding words and values in a dictionary

 
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So this is suppose to be pretty easy, but of course I make things harder than they should be. So I have a dictionary that holds words and a count for each word that shows up in the file. What I want to find is the total number of words in the book which I assume I can just use dictionary.count please correct me if im wrong. Also the number of different words in the dictionary, and last what word occurs the most, and how many times the word occurs. The rest I have already done, but I dont know how to approach these.

Note We can not use LINQ.

 
Marshal
Posts: 79967
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So . . . your Dictionary already contains the count of each word in the book.  So assuming the word recidivist doesn't occur in the book, but it is a real word, why are you returning −1 in line 27? Surely you would want something like return 0; there? I presume that the TryGetValue method returns a bool and puts the number found in the output parameter. It using out correct syntax?
Can you get rid of the if? Something like return dictionary.TryGetValue(word, out value) ? value : 0;
Is anything going wrong? I don't know enough C# to be able to predict what will go wrong, so I shall have to ask you to provide full details. Are out and value keywords or reserved words? Does that parameter value have a particular declared kind? Or is the class declared as Dictionary<K, V> or similar and instantiated as Dictionary<String, int> so value would implicitly be an int?

I would have thought that dictionary.Count would give you the number of different keys, not the total of counts, and you would have to iterate the entry set and summate the values as you go. Please check in the documentation you get with Visual Studio.
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the -1 was a suggestion from the instructor after I showed him my initial method.
Yes TryGetValue is a bool that returns the value associated with the key
I could possibly get rid of the if, but seeing as the instructor has already taken a look, and approved I would rather keep it
My dictionary is Dictionary<String, int> the String being the key which are just words from a text file, and int representing the value of how many times that word is read. So value has to be an int.
For the documention regarding count it says "Gets the number of key valued pairs contained in the dictionary. So I guess that would for the number of different words.

As for anything going wrong I am set on the three methods I have because I am just looking for how many times count, may and color are read. What I am trying to figure out now is how to get what word comes across the most, and how many times, and I guess now I need the total of all words if count is going to be for different words

The main methods talked about are containsKey, containsValue, count, trygetvalue, and also keyvaluepair which defines a key/value pair that can be retrieved.

So thinking of the keyvaluepair I can walk through each pair in the dictionary and get the key/value of the pair like this. So below I have to figure out how to compare for the highest values, and return botg key and value

 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but I couldnt do that could I? Key is a string and value is an int, so I cant return both
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless I did two methods. Same loop one returns the key, and the other the value? Just thinkin. Just Incase anyone sees this and has an idea with what I’m saying.
 
Campbell Ritchie
Marshal
Posts: 79967
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cody Biggs wrote:. . . the instructor has already taken a look, and approved I would rather keep it.

I still think I would go for the best code I can, but code that works will do for a start. Have you actually got code that works; I don't think you do.

My dictionary is Dictionary<String, int> the String being the key which are just words from a text file, and int representing the value of how many times that word is read.

That is what I thought. Similar to Java®, only in Java® you would use Integer rather than int.

. . . regarding count . . . I guess that would for the number of different words.

Again as I thought, which means you can't get the total number of words from Count.

. . . I am just looking for how many times count, may and color are read. . . .

Don't understand that bit. The three methods seem to do the same thing, which is to return the number of times a word appears. I don't think you will get anywhere with three identical methods, nor do I think that −1 is correct. If you use such methods for adding word counts, you will only get the right answer if you return 0. But I don't think those methods willl be the best way for a total of words.

The main methods talked about are containsKey, containsValue, count, trygetvalue, and also keyvaluepair which defines a key/value pair that can be retrieved.

Surely KeyValuePair returns a key‑value pair which has already been defined?

. . . I can walk through each pair in the dictionary and get the key/value of the pair . . .

That looks promising. Once you have worked out how to walk the entire dictionary, it is really simple to add up all the individual counts. And, in the same loop, you can also find the word with the highest count. That isn't difficult. I suggest you get a sheet of paper and write on it how you would go through such a dictionary. Decide what you will do if you find two words with the same highest count (take the first‑found???), and then work out how you are going to record all these results.
 
Campbell Ritchie
Marshal
Posts: 79967
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cody Biggs wrote:but I couldnt do that could I? Key is a string and value is an int, so I cant return both

Of course you can get both back. You need a bit of object‑oriented thinking.
 
Campbell Ritchie
Marshal
Posts: 79967
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cody Biggs wrote:Unless I did two methods. Same loop one returns the key, and the other the value? . . . .

nononononononononononono
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from the examples I have looked at KeyValuePair will loop through the pairs in the dictionary

example


So if that works to loop through the pairs then surley I can use that for my dictionary?

Then I would have to find the greatest value

 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
or if you think that is wrong then this way perhaps?

 
Campbell Ritchie
Marshal
Posts: 79967
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All no, because you are not thinking about what you want to do. Please read this. Only after you have worked out what you want to do can you work out how to do it. Only then should you write a single byte of code.
Start by working out how to count all the words in your dictionary, because I think that is the simpler of the two tasks. Write down how you intend to do it, and then simplify the whole process to words of one syllable. The description should then be so straightforward that even I can understand it. Once you have done that, you can probably turn it into code very easily. After you have got that working, add finding the most frequent word. You need the same design procedure, although the end result will look different.
 
Bartender
Posts: 5562
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cody Biggs wrote:but I couldnt do that could I? Key is a string and value is an int, so I cant return both


Of course you can. Just return the KeyValuePair that has the highest value.
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So all the methods I was using worked just fine, as well as walking through the dictionary with the keyValuePair.
Here is the code. Only thing left is to figure out how count every different word

 
Campbell Ritchie
Marshal
Posts: 79967
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your methods GetTotalPairs() and GetCount(String key) are peculiarly named. I don't think line 57 will work correctly. I can't see where you are calculating a total of words in the book, nor how you are working out which word appears most frequently.
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So this is how im getting the most frequent word


then to find the count of said word I use this in main


Both medthods return.
The most frequent word comes back as "the"
and the count for it comes back as 28335.

 
Sheriff
Posts: 28327
96
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is "the" the first word in your dictionary? I ask because your code returns the first item for which the value is greater than zero.
 
Campbell Ritchie
Marshal
Posts: 79967
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't see any other explanation. I have just downloaded Moby Dick from Project Gutenberg and that only has “the” in 14175×, including the preamble and postamble.
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just fixed some stuff. Added the return outside the for loop. Key and count still come back the same. "the" is the first word that is in the dictionary, but after messing with the text file, and replacing the with another word my key is still "the" that gets returned. So its doing what its suppose to.

 
Paul Clapham
Sheriff
Posts: 28327
96
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cody Biggs wrote:"the" is the first word that is in the dictionary, but after messing with the text file, and replacing the with another word my key is still "the" that gets returned. So its doing what its suppose to.



That's a good test, then. There's a whole world of software testing, including the one where you write the tests before the code -- doing this means that you must know in advance what some method is supposed to do. So it forces you to think about your design before you write the code. But I don't expect your course work has reached that world yet...
 
Campbell Ritchie
Marshal
Posts: 79967
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cody Biggs wrote:just fixed some stuff. Added the return outside the for loop. . . .

Now that looks right You could return the pair, in which case you get both word and frequency, as PS told you yesterday.
 
straws are for suckers. tiny ads are for attractive people.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic