dcsimg
Results 1 to 17 of 17

Thread: [RESOLVED] Another Dictionary Question

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    31

    Resolved [RESOLVED] Another Dictionary Question

    Hey all,

    I am trying to use a dictionary to store words and keep a count of how many times the word gets used in a file.

    I open the .txt file and read everything into a string and then split the string. I am then looping thru the words and would like to check each word with the dictionary.
    If the word is not in the dictionary I would like to add the word and give it a value of 1 being that the word has been seen 1 time.
    If the word is found in the dictionary I would like to update the count to 2 being it has been seen twice, 3 then 4, for as many times as the word is seen.

    When the loop is thru the dictionary should have each word it found and how many times it is found.

    For example I read the txt (The actual files will be much longer. This example is just for brevity)

    The one here is not the same as the one there.

    The dictionary should be

    The,3
    one,2
    here,1
    is,1
    not,1
    same,1
    as,1
    there.,1

    Here is what I have so far.

    Code:
            Dim WordTotal As New Dictionary(Of String, Integer)
            Dim CountWords As String = IO.File.ReadAllText(OriginalTextFile)
    
            Dim CountWord() As String = CountWords.Split(" "c)
            Dim SingleWord As String
            For Each SingleWord In CountWord
                If WordTotal.ContainsKey(SingleWord) Then
    
                End If
            Next
    Also if there is a better way to do what I am trying to do please feel free to share the options.
    My skill set with VB is limited and I learn a lot from people here.
    Last edited by bwquestion; Sep 1st, 2014 at 09:04 PM.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,145

    Re: Another Dictionary Question

    ContainsKey is going to return True if the word is already in the Dictionary and False otherwise. If it's True, simply increment the existing value. You know how to get and set a value in a Dictionary, right? You know how to increment an Integer, right? If it's False then add the new key to the Dictionary with a value of 1. You know how to add an item to a Dictionary, right?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    31

    Re: Another Dictionary Question

    If I knew the information to the questions you are asking I would not have had to make the post, right?

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,145

    Re: Another Dictionary Question

    Quote Originally Posted by bwquestion View Post
    If I knew the information to the questions you are asking I would not have had to make the post, right?
    You don't have to make the post anyway because that's information you could find on the web in a matter of minutes, or you could even use the Help menu on your IDE to view the documentation for the Dictionary class. Given those facts, I thought that perhaps you thought that you had to do more than you actually do or maybe there was something about the problem that I was missing. As it turns out, it's just as it seems, i.e. you thought it would be easier to get someone else to tell you what to do rather than look for yourself.

    Add item to Dictionary:
    Code:
    myDictionary.Add(key, value)
    Get value from Dictionary:
    Code:
    value = myDictionary(key)
    Set value in Dictionary:
    Code:
    myDictionary(key) = value
    I'm going to assume that an example of adding 1 to an Integer is not required.

  5. #5
    Lively Member elielCT's Avatar
    Join Date
    Jun 2011
    Posts
    89

    Re: Another Dictionary Question

    Quote Originally Posted by bwquestion View Post
    If I knew the information to the questions you are asking I would not have had to make the post, right?
    LMAO!!

    first i would say, your dictionary looks dangerous.. unless your absolutely sure the sentence you're going to use is never going to have a duplicate word..

    your using items from a sentence as keys in a dictionary.. keys have to be unique..

    jmcilhinney responded while i was writing this.. and just like he said, you should check out either msdn for small details like that or just explore what is available when you are using Visual Studio.. you should have intellisense pop up options..

    myDictionary. (intellisense pops up with available properties and methods here as you type)

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    31

    Re: Another Dictionary Question

    jmcilhinney. You make way to many assumptions. In order to search for things you have to know the terminology of what to search for. If you know what someone is looking for and you know where or what to look for then offer a link or at least the terminology that would bring up the requested results. Putting garbage answers on peoples questions is not helpful or needed. If that's the best you can do just skip over mine when you see it.

    elielCT. Based on your reply you didn't understand the question.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,145

    Re: Another Dictionary Question

    Quote Originally Posted by bwquestion View Post
    jmcilhinney. You make way to many assumptions. In order to search for things you have to know the terminology of what to search for. If you know what someone is looking for and you know where or what to look for then offer a link or at least the terminology that would bring up the requested results. Putting garbage answers on peoples questions is not helpful or needed. If that's the best you can do just skip over mine when you see it.
    The assumptions I make are two:

    1. Anyone who is capable of writing software is capable of clicking the Help menu in Visual Studio and typing the name of the type or member they want to know about into the index/search box.

    2. Anyone who is capable of writing software is capable of determining the correct keywords to use in a web search to find information on a specific subject.

    In the case of point 1, I've no doubt that it's true in your case but, like so many others, you just didn't bother. If you had then it would have taken less than a minute to find the documentation for the Dictionary(Of TKey, TValue) class and you then could have read about the class and all it's members, which would have led quickly to the Add method and Item property.

    In the case of point 2, I certainly hope that it's true in your case. I said:
    You know how to add an item to a Dictionary, right?
    If you genuinely can't work out that a Google search for vb.net add item dictionary or the like will lead to relevant information then I have grave concerns for any software that you might create.

    Programming is hard, no doubt, and I'm more than happy to help with genuine programming issues. We shouldn't have to teach general Windows software usage like using a Help menu or simple web searching techniques.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    31

    Re: Another Dictionary Question

    jmcilhinney. Again you make another useless post. Nothing in your last post contributes in any way to finding a solution to the problem. just move on. When you see my name on a question just skip it. As I also said you make too many assumptions and they have been wrong every time. The problem I am having was described in the best way I could figure to write it. You do not have to try and teach anything. In fact it would be best you didn't. Your ability to teach something would be considered remedial at best. Again just move on. You are not contributing anything positive to this post.

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,729

    Re: Another Dictionary Question

    Quote Originally Posted by bwquestion View Post
    jmcilhinney. Again you make another useless post. Nothing in your last post contributes in any way to finding a solution to the problem. just move on. When you see my name on a question just skip it. As I also said you make too many assumptions and they have been wrong every time. The problem I am having was described in the best way I could figure to write it. You do not have to try and teach anything. In fact it would be best you didn't. Your ability to teach something would be considered remedial at best. Again just move on. You are not contributing anything positive to this post.
    I'm not sure what more you want. You were shown the code to 1) Add to the dictionary, 2) Get a value FROM the dictionary and 3) How to put the value back into the dictionary...

    Quote Originally Posted by jmcilhinney View Post
    Add item to Dictionary:
    Code:
    myDictionary.Add(key, value)
    Get value from Dictionary:
    Code:
    value = myDictionary(key)
    Set value in Dictionary:
    Code:
    myDictionary(key) = value
    I'm going to assume that an example of adding 1 to an Integer is not required.
    What more do you want? Believe it or not, you've already done the hard part - creating the dictionary, reading the file and splitting it up. Clearly you must have read up on Dictionary to know how to work, most people try to do this with some kind of an array. Which means you probably ended up here on MSDN - the Dictionary (Of TKey, TValue) entry ... so surely you looked at the examples, right?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,145

    Re: Another Dictionary Question

    Quote Originally Posted by bwquestion View Post
    jmcilhinney. Again you make another useless post. Nothing in your last post contributes in any way to finding a solution to the problem. just move on. When you see my name on a question just skip it. As I also said you make too many assumptions and they have been wrong every time. The problem I am having was described in the best way I could figure to write it. You do not have to try and teach anything. In fact it would be best you didn't. Your ability to teach something would be considered remedial at best. Again just move on. You are not contributing anything positive to this post.
    So, you're not capable of doing the search for yourself and yet even when I do the search for you and provide a link to the results you still don't consider that to be positive input? So, it's either write your code for you or move one? I think I'll do myself and my profession that favour.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    31

    Re: Another Dictionary Question

    Thank you. Also please do that for any future post you see my name on.

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    31

    Re: Another Dictionary Question

    techgnome. I am very new to this language. Before I come to this forum I look up every thing I can find on my own. I looked up information on dictionaries. I know the the statements listed above. When I originally posted this last night I was hoping for some help with the logic involved in how to implement what I was doing, thus the example and what I had so far. Instead I was only given condescending comments and nothing that actually contributed to the question. I looked at some other threads where he has made comments and most of them are the same on those threads also. I don't need people like that around. There are other users that have been very helpful and to those I am grateful. I believe I have it working now and will know in a little while. The ultimate irony in this whole situation is that he was the person on my very first post that turned me on to the dictionary function in VB.

  13. #13
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,744

    Re: Another Dictionary Question

    Quote Originally Posted by bwquestion View Post
    techgnome. ... When I originally posted this last night I was hoping for some help with the logic involved in how to implement what I was doing, thus the example and what I had so far. ...
    I think the logic was given in post #2, and the statements to use to implement the code was given in post #4.
    I guess perhaps we were assuming given the logic, you could code it. Sorry about that.
    Here's the logic from post#2 inserted into the code you provided.
    Code:
    '
            Dim WordTotal As New Dictionary(Of String, Integer)
            Dim CountWords As String = IO.File.ReadAllText(OriginalTextFile)
    
            Dim CountWord() As String = CountWords.Split(" "c)
            Dim SingleWord As String
            For Each SingleWord In CountWord
                If WordTotal.ContainsKey(SingleWord) Then
                    'Get Value from Dictionary  'See post #4
                    'Increment Value  'We're all assuming you can add 1 to an integer
                    'Set Value in Dictionary 'See post #4
                Else 'Word not in Dictionary
                    'Add item to Dictionary   'With the value = 1 since it is the first time added, again see post #4
                End If
            Next

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    31

    Re: Another Dictionary Question

    Thank you passel. That is a helpful post.

  15. #15
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,744

    Re: Another Dictionary Question

    The three discreet steps in the "ContainsKey" block can be combined to a single line.
    Dictionary(Key) returns the value, so Dictionary(Key) += 1 will increment the value associated with that key.
    Here's an example, testing it
    Code:
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim d As New Dictionary(Of String, Integer)
        Dim s() As String = {"this", "is", "the", "test", "of", "the", "dictionary", "of", "words", "the", "program", "will", "add", "to", "the", "dictionary"}
    
        Dim SingleWord As String
        For Each SingleWord In s             'For each string in our array
          If d.ContainsKey(SingleWord) Then  '  If the string is in the dictionary
            d(SingleWord) += 1               '    Increment the value associated with it
          Else                               '  Else
            d.Add(SingleWord, 1)             '    Add the word, with a count of 1, to the dictionary
          End If
        Next
    
        For Each pair As KeyValuePair(Of String, Integer) In d   'For each pair (key and value) in the dictionary
          Debug.Print("{0},{1}", pair.Key, pair.Value)           '  Print word,count
        Next
      End Sub
    The window where you have Debug output go (usually the Output windows), should show:
    Code:
    this,1
    is,1
    the,4
    test,1
    of,2
    dictionary,2
    words,1
    program,1
    will,1
    add,1
    to,1

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    31

    Re: [RESOLVED] Another Dictionary Question

    That is the format I used. It is working good. I tested it with several files and it did exactly what is in your output window. I really do appreciate you stepping in and working with me on this. I'm not really sure why the other guy always gives me a hard time.

  17. #17
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,145

    Re: [RESOLVED] Another Dictionary Question

    Quote Originally Posted by bwquestion View Post
    I'm not really sure why the other guy always gives me a hard time.
    Because you were waiting for someone to write your code for you instead of using the information at your disposal to at least make an effort to write for yourself.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width