Text Frequency; Trying to Run Some Code



I am trying to run the below code, which I found in a book named Mastering
Visual basic.NET:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.DefaultExt = "TXT"
OpenFileDialog1.Filter = "Text|*.TXT|All Files|*.*"
If OpenFileDialog1.FileName = "" Then Exit Sub
Dim str As StreamReader
Dim txtFile As File
Dim txtLine As String
Dim Words() As String
Dim Delimiters() As Char = {CType(" ", Char), CType(".", Char), _
CType(",", Char), CType("‘", Char), _
CType("!", Char), CType(";", Char), _
CType(":", Char), Chr(10), Chr(13)}
str = File.OpenText(OpenFileDialog1.FileName)
txtLine = str.ReadLine()
txtLine = str.ReadToEnd
Words = txtLine.Split(Delimiters)
Dim iword As Integer, word As String
For iword = 0 To Words.GetUpperBound(0)
word = Words(iword).ToUpper
If IsValidWord(word) Then
If Not WordFrequencies.ContainsKey(word) Then
WordFrequencies.Add(word, 1)
WordFrequencies(word) = CType(WordFrequencies(word),
Integer) + 1
End If
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim wEnum As IDictionaryEnumerator
Dim occurrences As Integer
Dim allWords As New System.Text.StringBuilder()
wEnum = WordFrequencies.GetEnumerator
While wEnum.MoveNext
allWords.Append(wEnum.Key.ToString & vbTab & "—>" & vbTab & _
wEnum.Value.ToString & vbCrLf)
End While
TextBox1.Text = allWords.ToString
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button3.Click
Dim wEnum As IDictionaryEnumerator
Dim Words(WordFrequencies.Count) As String
Dim Frequencies(WordFrequencies.Count) As Double
Dim allWords As New System.Text.StringBuilder()
Dim i, totCount As Integer
wEnum = WordFrequencies.GetEnumerator
While wEnum.MoveNext
Words(i) = CType(wEnum.Key, String)
Frequencies(i) = CType(wEnum.Value, Integer)
totCount = totCount + Frequencies(i)
i = i + 1
End While
For i = 0 To Words.GetUpperBound(0)
Frequencies(i) = Frequencies(i) / totCount
Words.Sort(Frequencies, Words)
For i = Words.GetUpperBound(0) To 0 Step -1
allWords.Append(Words(i) & vbTab & "—>" & vbTab & _
Format(100 * Frequencies(i), "#.000") & vbCrLf)
TextBox1.Text = allWords.ToString
End Sub

End Class

I had a few squiggle lines, so I added this:
Imports System.IO

Some of the squiggles disappeared, but I still have squiggles under the
following: OpenFileDialog1, IsValidWord, WordFrequencies, and Words.Sort

I tried to run the project, and of course it didn’t work. My question is
how do I fix this? Also, and more importantly, how do I figure out what to
do the next time something like this happens? Is there a library or class
that needs to be imported? Is there a reference that needs to be added?
I’ve read a couple books on VB and encounter these types of things from time
to time, but I don’t really know how to resolve the issues myself, I guess
because I don’t have the level of experience of others. So I come to this
discussion group and get help sometimes, which is great!! However, I really
want to be able to resolve these issues by myself.


Armin Zingler

ryguy7272 said:
I am trying to run the below code, which I found in a book named Mastering
Visual basic.NET:

Either you didn't follow the steps described in the book to write a
complete example - it should at least say there has to be an OpenFileDialog
on the Form - or you should buy a better book.

James Hahn

Have you checked whether the author has published corrections on his

Drag an open file dialog control from the toolbox and drop it on the form.

You need to add the following function:
' This function returns False if the word passed as argument is invalid
' Valid words are made up of letters ("vice-versa" is not a valid word,
for example)
Protected Function IsValidWord(ByVal word As String) As Boolean
If Trim(word).Length = 0 Then
Return (False)
End If
Dim ch As Char
Dim iChar As Integer
For iChar = 0 To Len(word) - 1
ch = word.Chars(iChar)
If Not System.Char.IsLetter(ch) Then
Return (False)
End If
Return (True)
End Function

WordFrequencies must be declared as "Dim WordFrequencies As New Hashtable()"
and needs to be filled with the frequency table from the frequencies file
(BIN or XML) in the appropriate routine .

Change "Words.Sort(Frequencies, Words)" to "Array.Sort(Frequencies, Words)"
Also, In Button 3, it should be "totCount = CInt(totCount + Frequencies(i))"

The delimiters line is wrong - it should be: Dim Delimiters() As Char = {"
"c, "."c, ","c, "‘"c, "!"c, ";"c, ":"c, Chr(10), Chr(13)}

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question