Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to format a While... Wend correctly?
#29
It's an overflow error.  /sigh

Add this one line to the top of the code and try it:

Code: (Select All)
DefLng A-Z

s is a SINGLE by default.  The overall dictionary is larger than a single can hold, so it reads the first 255000 or so words and definition and then says, "Blah!!!"

The sort isn't needed.  It simple needs to not overflow so that it'll load and parse that list properly for us.

Code: (Select All)
dict$ = "Collins.txt"
DefLng A-Z
Screen _NewImage(800, 600, 32)

Type Dict_Type
    As String Word, Definition
End Type

ReDim Shared Dict(1000000) As Dict_Type

load_time1# = Timer
temp$ = _ReadFile$(dict$)
sp = 1
Do
    ep = InStr(sp, temp$, Chr$(10))
    temp1$ = Mid$(temp$, sp, ep - sp)
    If temp1$ = "" Then Exit Do
    l = InStr(temp1$, Chr$(9)) 'tab separated data file
    count = count + 1
    Dict(count).Word = _Trim$(Left$(temp1$, l - 1))
    Dict(count).Definition = _Trim$(Mid$(temp1$, l))
    sp = ep + 1
Loop Until sp >= Len(temp$)
ReDim _Preserve Dict(count) As Dict_Type
load_time2# = Timer

Dim Junk(10) As String
Data cheese,dog,cat,elephant,rootbeer,house,food,drink,mouse,zebra
For i = 1 To 10
    Read Junk(i)
Next


t# = Timer
For k = 1 To 10000
    For i = 1 To 10
        f = FindWord(Junk(i))
        If k = 1 Then 'no need to scroll the screen and print the words repeatedly
            If f = 0 Then
                Print Junk(i), "Word not found"
            Else
                Print Junk(i), Dict(FindWord(Junk(i))).Definition
            End If
        End If
    Next
Next
t1# = Timer

Print Using "###.######## seconds to load dictionary with ###,###,### words."; loadtimer2# - loadtime1#, count
Print Using "###.######## seconds to find #### words and definitions, ###,###,### repeated times."; t1# - t#, i - 1, k - 1

Function FindWord (word$)
    Dim As Long low, hi, test
    low = 1: hi = UBound(Dict)
    While low <= hi
        test = Int((low + hi) / 2)
        Select Case _StriCmp(Dict(test).Word, word$)
            Case 0
                'Print "found"; test
                FindWord = test: Exit Function
            Case -1
                'Print "low"; Dict(test).Word
                low = test + 1
            Case 1
                'Print "high"; Dict(test).Word
                hi = test - 1
        End Select
    Wend
End Function


.txt   Collins.txt (Size: 17.37 MB / Downloads: 41)

Use the dictionary file above with the code above us.  I wanted to simplify things for testing, so this strips out those header lines, and the blank line at the end of the file, and sets it to using CHR$(10) line endings rather than CHR$(13) + CHR$(10).  (Just to reduce overall filesize by a few hundred thousand bytes.)

Things appear to work as advertised now that our sp (starting position) can fully hold values greater than the length of the string without losing precision.  Wink
Reply


Messages In This Thread
RE: How to format a While... Wend correctly? - by SMcNeill - 02-29-2024, 04:54 PM



Users browsing this thread: 1 Guest(s)