String matching for Monkey

Monkey Forums/Monkey Code/String matching for Monkey

consty(Posted 2016) [#1]
This is a very simple library for doing fuzzy string matching in Monkey. You can create a list with your own words and then perform the search to populate the results list. I hope you put it to as many projects as possible because this is a very important feature that is always overlooked.

Strict

Class APIProvider
	Field APIList:List<String> = New List<String>
	Field APIResult:List<String> = New List<String>
	
	Method LoadExample:Void()
		APIList.Clear()
		APIList.AddLast("CreateWindow")
		APIList.AddLast("DefaultWindowHints")
		APIList.AddLast("DestroyWindow")
		APIList.AddLast("ExtensionSupported")
		APIList.AddLast("GetClipboardString")
		APIList.AddLast("GetCurrentContext")
		APIList.AddLast("GetCursorPos")
		APIList.AddLast("GetFramebufferSize")
		APIList.AddLast("GetGammaRamp")
		APIList.AddLast("GetInputMode")
		APIList.AddLast("GetJoystickAxes")
		APIList.AddLast("GetJoystickButtons")
		APIList.AddLast("GetJoystickName")
		APIList.AddLast("GetKey")
		APIList.AddLast("GetMonitorName")
		APIList.AddLast("GetMonitorPhysicalSize")
		APIList.AddLast("GetMonitorPos")
		APIList.AddLast("GetMonitors")
		APIList.AddLast("GetMouseButton")
		APIList.AddLast("GetPrimaryMonitor")
		APIList.AddLast("GetProcAddress")
		APIList.AddLast("GetTime")
		APIList.AddLast("GetVersion")
		APIList.AddLast("GetVersionString")
		APIList.AddLast("GetVideoMode")
		APIList.AddLast("GetVideoModes")
		APIList.AddLast("GetWindowAttrib")
		APIList.AddLast("GetWindowMonitor")
		APIList.AddLast("GetWindowPos")
		APIList.AddLast("GetWindowSize")
		APIList.AddLast("GetWindowUserPointer")
		APIList.AddLast("HideWindow")
		APIList.AddLast("IconifyWindow")
		APIList.AddLast("Init")
		APIList.AddLast("JoystickPresent")
		APIList.AddLast("MakeContextCurrent")
		APIList.AddLast("PollEvents")
		APIList.AddLast("RestoreWindow")
		APIList.AddLast("SetCharCallback")
		APIList.AddLast("SetClipboardString")
		APIList.AddLast("SetCursorEnterCallback")
		APIList.AddLast("SetCursorPos")
		APIList.AddLast("SetCursorPosCallback")
		APIList.AddLast("SetErrorCallback")
		APIList.AddLast("SetFramebufferSizeCallback")
		APIList.AddLast("SetGamma")
		APIList.AddLast("SetGammaRamp")
		APIList.AddLast("SetInputMode")
		APIList.AddLast("SetKeyCallback")
		APIList.AddLast("SetMonitorCallback")
		APIList.AddLast("SetMouseButtonCallback")
		APIList.AddLast("SetScrollCallback")
		APIList.AddLast("SetTime")
		APIList.AddLast("SetWindowCloseCallback")
		APIList.AddLast("SetWindowFocusCallback")
		APIList.AddLast("SetWindowIconifyCallback")
		APIList.AddLast("SetWindowPos")
		APIList.AddLast("SetWindowPosCallback")
		APIList.AddLast("SetWindowRefreshCallback")
		APIList.AddLast("SetWindowShouldClose")
		APIList.AddLast("SetWindowSize")
		APIList.AddLast("SetWindowSizeCallback")
		APIList.AddLast("SetWindowTitle")
		APIList.AddLast("SetWindowUserPointer")
		APIList.AddLast("ShowWindow")
		APIList.AddLast("SwapBuffers")
		APIList.AddLast("SwapInterval")
		APIList.AddLast("Terminate")
		APIList.AddLast("WaitEvents")
		APIList.AddLast("WindowHint")
		APIList.AddLast("WindowShouldClose")
	End
	
	Method Search:Void(term:String)
		APIResult.Clear()
		
		term = term.ToLower()
		
		For Local s:String = Eachin APIList
			If s.ToLower().Contains(term)
				APIResult.AddLast(s)
			End
		Next
	End
	
	Method SearchSoft:Void(term:String)
		Local minAllowedScore:Int = 90
	
		APIResult.Clear()
	
		For Local s:String = Eachin APIList
			Local score:Int = StringSearchSoft(s, term)
			If score > minAllowedScore
				APIResult.AddLast(s)
			End
		Next
	End
	
	Method PrintResult:Void()
		Print("SEARCH RESULTS")
		For Local s:String = Eachin APIResult
			Print(s)
		Next
		Print("")
	End
	
	' Private
	
	Function StringSearchSoft:Int(stringToBeSearched:String, searchTerm:String)
		' Perform string matching test
		Local termMatches:Int = 0
		
		For Local is:Int = 0 Until stringToBeSearched.Length
			Local ss:String = String.FromChar(stringToBeSearched[is])
			Local st:String = String.FromChar(searchTerm[termMatches])
			
			If st.ToLower() = ss.ToLower()
				termMatches += 1
			End
			
			If termMatches = searchTerm.Length
				Exit
			End
		Next
		
		' Perform score calculation
		Local scoreFactor:Int = 100 / searchTerm.Length()
		Local totalScore:Int = termMatches * scoreFactor
		
		Return totalScore
	End
End

Function Main:Int()
	
	Local api:APIProvider = New APIProvider()
	
	api.LoadExample()
	
	api.SearchSoft("gemona")
	api.PrintResult()
	
	api.SearchSoft("gewinpo")
	api.PrintResult()

	api.SearchSoft("winpos")
	api.PrintResult()
	
	api.SearchSoft("secall")
	api.PrintResult()
	
	api.SearchSoft("wsize")
	api.PrintResult()
	
	Return 0
End



consty(Posted 2016) [#2]
Result output
SEARCH RESULTS
GetMonitorName
GetMonitorPhysicalSize

SEARCH RESULTS
GetWindowPos
GetWindowUserPointer

SEARCH RESULTS
GetWindowPos
SetWindowPos
SetWindowPosCallback

SEARCH RESULTS
SetCharCallback
SetCursorEnterCallback
SetCursorPosCallback
SetErrorCallback
SetFramebufferSizeCallback
SetKeyCallback
SetMonitorCallback
SetMouseButtonCallback
SetScrollCallback
SetWindowCloseCallback
SetWindowFocusCallback
SetWindowIconifyCallback
SetWindowPosCallback
SetWindowRefreshCallback
SetWindowSizeCallback

SEARCH RESULTS
GetWindowSize
SetWindowSize
SetWindowSizeCallback



TeaBoy(Posted 2016) [#3]
For string matching I use the Boyer-Moore algorithm, does the job!


consty(Posted 2016) [#4]
I am interested to make a collection of string matching algorithms (like proper Fuzzy algorithm, or Levenstein, etc). This will become a whole new library. Any contributions will be appreciated.