Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dictionary Builder Part 4 Loading Files.

Similar presentations


Presentation on theme: "Dictionary Builder Part 4 Loading Files."— Presentation transcript:

1 Dictionary Builder Part 4 Loading Files

2 Step 8 – Loading Files Note that there are 2 distinct types of files that the program needs to read: Dictionary files one word on each line Text files multiple words on multiple lines

3 Step 8 – Loading Files However, in both cases, many of the same commands must be executed. Looks like a job for sub-programs!

4 Step 8a – Setting the stream
Private Sub setStream(ByRef reader As IO.StreamReader, ByRef theFile As IO.FileStream) ' Local declarations ' Set properties of the OpenFileDialog object ' and show the dialog. ' If the user selected a file and pressed Open ' Create the FileStream and StreamReader objects

5 Step 8b – Closing the stream
Private Sub closeStream(ByVal reader, ByVal theFile) ' Close the reader ' Close the file ' Set the flag End Sub

6 Step 8c.1 – Reading a dictionary
To read the words from a dictionary file (one created by the program) the code needs to loop through the file one line at a time and add the line to the listBox. Do Until reader.EndOfStream lstWords.Items.Add(reader.ReadLine()) Loop

7 Step 8c.2 – Reading text To read a text file, the program must take in the whole file as a string and then parse it into words, and decide if each word should be added to the list. Dim newText As String newText = LCase(reader.ReadToEnd)

8 Step 9 – Extracting words
Once the text file has been read into the string variable… the string needs to be parsed, and each word tested to be added to the list.

9 Step 9 – Parsing the string
Private Sub extractFrom(ByVal theText) ' Local declarations ' Initialise the flag and thisWord ' Loop through the string ' Isolate the next character ' Is it a word ender? ' Does it really end a word? ' set the flag ' send the word for further testing ' reset the variable to empty ' Not a string ender ' Concatenate the thisChar onto thisWord ' If it's the first letter of a word End Sub

10 Step 9 – Parsing the string
' Local declarations Const enders = "_ & vbNewLine & vbTab Dim c As Integer Dim scanningWord As Boolean Dim thisWord As String ' Initialise the flag and thisWord …

11 Step 9 – Parsing the string
' Local declarations Const enders = "_ & vbNewLine & vbTab Dim c As Integer Dim scanningWord As Boolean Dim thisWord As String ' Initialise the flag and thisWord scanningWord = False thisWord = vbNullString …

12 Step 9 – Parsing the string
' Loop through the string ' Isolate the next character ' Is it a word ender? ' Does it really end a word? ' set the flag ' send the word for further testing ' reset the variable to empty ' Not a string ender ' Concatenate the thisChar onto thisWord ' If it's the first letter of a word

13 Step 9 – Parsing the string
' Loop through the string For c = 1 To Len(theText) ' Isolate the next character ' Is it a word ender? ' Does it really end a word? ' set the flag ' send the word for further testing ' reset the variable to empty ' Not a string ender ' Concatenate the thisChar onto thisWord ' If it's the first letter of a word Next

14 Step 9 – Parsing the string
' Loop through the string For c = 1 To Len(theText) ' Isolate the next character Dim thisChar = Mid(theText, c, 1) ' Is it a word ender? ' Does it really end a word? ' set the flag ' send the word for further testing ' reset the variable to empty ' Not a string ender ' Concatenate the thisChar onto thisWord ' If it's the first letter of a word Next

15 Step 9 – Parsing the string
' Loop through the string For c = 1 To Len(theText) ' Isolate the next character Dim thisChar = Mid(theText, c, 1) ' Is it a word ender? If InStr(enders, thisChar) > 0 Then ' Does it really end a word? ' set the flag ' send the word for further testing ' reset the variable to empty ' Not a string ender Else ' Concatenate the thisChar onto thisWord ' If it's the first letter of a word End If Next

16 Step 9 – Parsing the string
' Loop[ing] through the string ' and finding a word-ending character… … ' Does it really end a word? If scanningWord Then ' set the flag ' send the word for further testing ' reset the variable to empty End If ' Not a string ender Else ' Concatenate the thisChar onto thisWord ' If it's the first letter of a word

17 Step 9 – Parsing the string
' Loop[ing] through the string ' and finding a word-ending character… … ' Does it really end a word? If scanningWord Then ' set the flag scanningWord = False ' send the word for further testing submit(thisWord) ' reset the variable to empty thisWord = vbNullString End If ' Not a string ender Else …

18 Step 9 – Parsing the string
' Loop[ing] through the string ' NOT finding a word-ending character… … ' Does it really end a word? … ' (on previous slide) ' Not a string ender Else ' Concatenate the thisChar onto thisWord thisWord &= thisChar ' If it's the first letter of a word ' set the flag End If

19 Step 9 – Parsing the string
' Loop[ing] through the string ' NOT finding a word-ending character… … ' Not a string ender Else ' Concatenate the thisChar onto thisWord thisWord &= thisChar ' If it's the first letter of a word If Not scanningWord Then ' set the flag End If

20 Step 9 – Parsing the string
' Loop[ing] through the string ' NOT finding a word-ending character… … ' Not a string ender Else ' Concatenate the thisChar onto thisWord thisWord &= thisChar ' If it's the first letter of a word If Not scanningWord Then ' set the flag scanningWord = True End If

21 Step 9 – Parsing the string
Private Sub extractFrom(ByVal theText) Const enders = " & vbNewLine & vbTab Dim c As Integer, scanningWord As Boolean, thisWord As String scanningWord = False thisWord = vbNullString For c = 1 To Len(theText) Dim thisChar = Mid(theText, c, 1) If InStr(enders, thisChar) > 0 Then If scanningWord Then submit(thisWord) End If Else thisWord &= thisChar If Not scanningWord Then scanningWord = True Next End Sub

22 Step 10 – testing the word The tests that a word needs to pass are the same as those imposed on those entered in the txtWord. Hurray for sub-programs!

23 Step 10 – testing the word Private Sub submit(ByVal aWord As String) If Len(aWord) <= 11 And Len(aWord) >= 7 _ And isWord(aWord) And isUnique(aWord) Then lstWords.Items.Add(aWord) End If End Sub Note the added condition that words have at least 7 letters.


Download ppt "Dictionary Builder Part 4 Loading Files."

Similar presentations


Ads by Google