News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

WordHeap - Used to keep a Dictionary of words / strings

Started by kevin, March 24, 2022, 07:53:19 PM

Previous topic - Next topic

kevin

     WordHeap - Used to keep a Dictionary of words / strings


    The Example test code (bellow)  loads the PlayBASIC keywords file and adds all the PB commands to a heap (a static dictionary) and then does a search for each word.  The idea head is that we can build a dictionary of known words and then search if they exist or not.    While the test assumes we're cataloging words (in this case PlayBASIC command names), it also support none alphanumeric characters too.  


PlayBASIC Code: [Select]
; PROJECT : PlayBASIC - KeyWord - Bucket
; AUTHOR : Kev PIcone - PlayBASIC TUTOR - Http://playbasic.com
; CREATED : 24/03/2022
; EDITED : 25/03/2022
; ---------------------------------------------------------------------


// Compute the location of the playBASIC command listing
// so we have a big list of words for something to load and test
file$=GetPlayBASICKeyWordsPath$()

// Load the keywords textfile to a string
All_KeyWords$=LoadFileToString(file$)


// Scan this block of text looking for the section named [Commands]
// and then return it
Command_KEyWords$ = GetKeywordsUnderHeading(All_KeyWords$,"[Commands]")


// Test adding playbasic keywords to our WordHeap
Test_KeyWord_Heap(Command_KeyWords$)


//
print "Test Complete - Press Space To End"

; refresh display and wait for a key press before ending
sync
waitkey
end



Function Test_KeyWord_Heap(KeyWords$)


; Dim a string array called WORDS with an initial size of 1000
dim Words$(1000)

Result$ =replace$(KeyWords$,Chr$(13)+Chr$(10),",")
count =splittoarray(Result$,",",Words$())


// --------------------------------------------------
// Add each word to the Word Heap / Dictionary
// --------------------------------------------------
For lp=0 to count-1
word$ =words$(lp)
WordHeap_Add(Word$)
next


// --------------------------------------------------
// Do a search for all the added keywords
// --------------------------------------------------
print " Searching For #"+Str$(Count)+" Keywords"
starttime=timer()
For lp=0 to count-1
word$ =words$(lp)
Status=WordHeap_FIND(Word$)
#print digits$(Status,2)+">>>"+word$
Matches+=Status
next
StartTime=Timer()-StartTime
print "Found Keywords #"+Str$(Matches)
print ""
print "Search Time #"+Str$(StartTime)+" milliseconds"
print ""
print ""


EndFunction





Function GetKeywordsUnderHeading(KeyWords$,Heading$)

cr$=Chr$(13)+chr$(10)
Tag$=Heading$+cr$

// Lookgfor the TAG plus the linefeed / end of line within the string
StartPOS=instring(keywords$,tag$)
if StartPos

// If the tag is found, we step the found position to the end
// of the found location plus the tag size in characters
StartPos+=Len(tag$)

// search for tbhe first empty line beyond where the starting tag
// was founf
EndPos =instring(KeyWords$,Cr$+cr$,StartPos)

// Check if the closing tag position was indeed after the start?
if EndPos>StartPOs
// use MID$() to return this block of text from the keyword string
Result$ =mid$(Keywords$,StartPos,EndPos-StartPOs)
endif
endif

EndFunction Result$





Function LoadFileToString(file$)
if FIleexist(file$)
local size=filesize(file$)
local f=readnewfile(file$)
result$=readchr$(f,size)
closefile f
endif
EndFunction Result$




LinkDll "shell32"
zPriv_SHGetFolderPath(hWndOwner,nFolder,hToken,dwFlags,pszPath) Alias "SHGetFolderPathA" as integer
EndLinkDll




Function zPriv_Highlighter_GetSpecialFolderPath(nFolderID)
// Alloc a bank of 1024 bytes for the function to return the path in
local Size=1024
local ThisBank=newbank(Size)
local Ptr=GetBankPtr(thisBank)
local Status=zPriv_SHGetFolderPath(0,nFolderID,0,0,ptr)
if Status=0
// if status is 0 then the function worked
Path$=PeekString(ptr,0) ; peek a null termed string
else
#print "error polling GetSpeicalFolderpath"
endif
Deletebank ThisBank
EndFunction path$




PSUB GetPlayBASICKeyWordsPath$()
KeyWordsFile$=""

Login required to view complete source code



  Download

       Attached to post bellow