Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function IsWord%(test$)
#4
And with the dictionary bplus used for his:

Code: (Select All)
Screen _NewImage(1024, 720, 32)
$Color:32

ReDim dict(0) As String
t1 = Timer(0.001)
LoadWordList "Collins.txt", dict(), 300000
t2 = Timer(0.001)

Print Using "#.##### seconds to load ###,### words."; t2 - t1, UBound(dict)
For i = 1 To 10
    Print dict(i),
Next
Print
For i = UBound(dict) - 10 To UBound(dict)
    Print dict(i),
Next
Print

'and just to showcase how quick arrays are, let's do a random search of the array from start to bottom
For i = 1 To 100 'let's look for 100 random words
    word$ = Chr$(65 + Rnd * 26) + Chr$(65 + Rnd * 26) + Chr$(65 + Rnd * 26)
    match = _FALSE
    For j = 1 To UBound(dict)
        Color Yellow
        If word$ = dict(j) Then Print "MATCH:"; word$,: match = _TRUE
        Color White
    Next
    If _Negate (match) Then Print "No match:"; word$,
Next

'note that this is doing nothing to binary search, nor is it exiting after finding a match, nor doing anything else to speed up the process
'this is simply testing the entire word array one by one looking for a match for our words.
'and it takes... no noticable time at all.

Sub LoadWordList (file$, WordList() As String, Limit As Long) 'this sub loads a list of words for use later
    $Checking:Off
    Dim As String temp, t1
    Dim As Long count, p, p1
    ReDim WordList(Limit) As String 'let's make a nice large array to told the words.  Set the limit you want for yourself
    If _FileExists(file$) Then 'then we have a found word list.  Let's load and parse it
        temp = _ReadFile$(file$)
        p = 1
        Do
            p1 = InStr(p, temp, Chr$(10)) 'look for a chr$(10) end of line marker
            If p1 = 0 Then p1 = InStr(p, temp, Chr$(13)) 'if no chr$(10) then look for a chr$(13) for odd files with it as the CRLF
            If p1 Then 'then we have a delimiter
                t1 = _Trim$(Mid$(temp, p, p1 - p))
                If Right$(t1, 1) = Chr$(13) Then t1 = Left$(t1, Len(t1) - 1) 'if CRLF then strip off chr$(13)
                If t1 <> "" Then 'don't add blank lines to the list
                    count = count + 1
                    WordList(count) = t1
                End If
                p = p1 + 1
            End If
        Loop Until p1 = 0
        If p < Len(temp) Then 'if there's no CRLF for the end of file, we want to last word here
            t1 = Mid$(temp$, p)
            If t1 <> "" Then 'again, don't add if it's a blank line
                count = count + 1
                WordList(count) = Mid$(temp, p)
            End If
        End If
    End If
    ReDim _Preserve WordList(count) As String
    $Checking:On
End Sub

less than a second or so from start to finish for me, and this is without trying to optimize anything or make it speedy.  Load and parse 270k words into an array.  Search that array from top to bottom 100 times... and the whole process is less than a second.  For most applications with words, it's going to be more than fast enough for whatever type of lookup you might be doing.
Reply


Messages In This Thread
Function IsWord%(test$) - by bplus - 02-25-2026, 08:58 PM
RE: Function IsWord%(test$) - by SMcNeill - 02-25-2026, 09:27 PM
RE: Function IsWord%(test$) - by SMcNeill - 02-25-2026, 10:16 PM
RE: Function IsWord%(test$) - by SMcNeill - 02-25-2026, 10:25 PM
RE: Function IsWord%(test$) - by bplus - 02-25-2026, 11:34 PM
RE: Function IsWord%(test$) - by mdijkens - 02-26-2026, 02:51 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Zeller's congruence pass 3: test day-of-week calculation algorythms for accuracy TDarcos 0 1,100 10-23-2024, 05:04 PM
Last Post: TDarcos
  Test sorting algorithms eoredson 3 973 05-04-2023, 09:38 PM
Last Post: eoredson
  Long Date Function AtomicSlaughter 2 977 05-24-2022, 08:22 PM
Last Post: bplus

Forum Jump:


Users browsing this thread: 1 Guest(s)