whats the most popular word?

VBnetster

Active member
Joined
Feb 15, 2008
Messages
32
Programming Experience
Beginner
hi,

been trying all day to make a function to find the most popular word in a string. so say if i had a string "lala xxx lalala xxx la xxx la" then "xxx" would be the popular word. I thought i figured it out once but i fooled myself and im ashamed to post it lol.:(

can i have some help with this?

thx

and i know this has allot to do with arrays, i just made a mess.
 
Try a Dictionary(Of String, Integer) where the string is the word and the integer where you count occurences of each word, when you have counted all words you can look through the dictionary to find the highest count.
 
sounds like a good idea.. but im stuck in updating the count :/ it tells me the value is already there i cant use:
VB.NET:
openDict.Add(myAry(i), count)
 
You only add to dictionary when you have a new word, later you access the dictionary by its key and is returned the integer, which you can add to. ContainsKey method is very important when working with dictionaries, because each key must be unique.
VB.NET:
Dim words() As String = {"word", "is", "word"}
Dim d As New Dictionary(Of String, Integer)
For Each word As String In words
    If Not d.ContainsKey(word) Then
        d.Add(word, 1)
    Else
        d(word) += 1
    End If
Next

Dim bestword = (From kv In d Where kv.Value = d.Values.Max Select kv.Key)(0)

' or:
' bestword = d.Where(Function(kv) kv.Value = d.Values.Max)(0).Key
 
ohhhh i was doing it like this

VB.NET:
      Dim txt As String = "a xxx aa xxx xxx aa xxx aaa"
        Dim dict As New Dictionary(Of String, Integer)
        Dim myAry() As String = txt.Split(" ")
        Dim count As Integer = 0

        For i = 0 To UBound(myAry)
            count = 0
            If dict.ContainsKey(myAry(i)) Then
                count += 1
                dict.Add(myAry(i), count)
            Else
                dict.Add(myAry(i), 0)
            End If
        Next

I'm pretty useless :p thanks
 
That was not too bad, it was the opposite. Kind of.
 
hmm how can i delete all the keys? when i call the function a second time the data from the last use is still in the dict. i tried a few things.

thanks.

and i was working backwards, that is good? :p
 
dictionary.Clear method clears it.
 
Try a Dictionary(Of String, Integer) where the string is the word and the integer where you count occurences of each word, when you have counted all words you can look through the dictionary to find the highest count.

John, what would be the advantage of using a dictionary for this over using hashtable, with the count as the value, and incrimenting that for each you hit? Or isn't there really any advantage?
 
ah yeah i tried dict.clear() before posting. but i had a little mistake, i was comparing the same text boxes so it would give the same result for both :p

now another problem i cant seem to solve, its a bit weird :/

Dim result As Single = 0 - (Anew.Count / totalnew)
i check to see the difference via:
If result > 0.1 Then
even if its 100% different i still get my message "not good enough" so i tried
If result < 0.1 Then
now i get the same problem apart from it will always be good even if its 0.0% different. i know that would work anyway but i tried both just to make sure heh..

I just want it to say its good if its anything of 40%
 
John, what would be the advantage of using a dictionary for this over using hashtable, with the count as the value, and incrimenting that for each you hit? Or isn't there really any advantage?
A Hashtable is basically a Dictionary(Of Object, Object). It is an older class in .Net, from before generics were introduced with .Net 2.0.
 
Dim result As Single = 0 - (Anew.Count / totalnew)
This is always a negative number. (or 0)

(Anew.Count / totalnew) is the match percentage value 0 to 1 (=0% to 100%)
 
Converting to string and doing string manipulations, then convert back to integer is silly. How about multiplying the value with 100 since you can't understand 0.2 is 20% ?
 
John, what would be the advantage of using a dictionary for this over using hashtable, with the count as the value, and incrimenting that for each you hit? Or isn't there really any advantage?

Dictionary is a typed Hashtable. Try using a hashtable; it'll work but you'll have to do a lot of casting.

Dictionary(Of String, Integer) <-- thats the good bit: you state youre using strings for the keys and ints for the values

myDict("myKey") += 1 'ok
myHashTable("myKey") += 1 'error: Object has no overload for +=
myHashTable("myKey") = DirectCast(myHashTable("myKey"), Int32) + 1 'yawn

Alternately, if you want to turn off Option Strict and Option Explicit and be jsut another regular (i.e. not very skilled) VB programmer, youy might not need to cast.
 
Back
Top