Basic Search Engine Algorithm

The code below is a basic Lua text extractor and comparing algorithm. It uses tables and for loops to cycle through all words a GUI object may contain, then compare them to the input you’ve typed in the TextBox.

The search functionality relies on a GUI (Graphical User Interface) system, which is included in the open-source game. The pages, blocks, text box elements and the script are part of a GUI layout in the game.

How does it work? Step-by-step
  1. The script sets up a list called IgnoredWords, which contains various common English words that will be ignored during the search process. These words are often called “stop words” as they don’t carry significant meaning and are typically excluded from text analysis. (Lines 4-42)

  2. The Search function is defined, which is the main search logic for this script. It is triggered when the FocusLost event of the textBox occurs, indicating that the user has entered some text and moved the focus away from the text box. (Lines 45 & 107)

  3. If the player enters 2 or less lone words/numbers, then the script doesn’t continue. This is optional and can be removed without the script breaking. (Lines 2 & 47)

  4. Inside the Search function, the entered text from the textBox is extracted, converted to lowercase, and split into individual words, which are stored in the Keywords table. (Line 48)

  5. The script iterates over all the pages (blocks of text) located under the Pages object. For each page, it retrieves all descendant objects that are either TextLabel or TextButton and extracts the text content from them. The text content is then converted to lowercase and split into individual words, which are stored in the ComparisonTable. (Lines 56-71)

  6. The script then proceeds to remove any stop words (words listed in IgnoredWords) from the ComparisonTable. (Lines 73-79)

  7. It calculates the number of matches between the keywords entered by the user (Keywords) and the words found in the page’s text (ComparisonTable). Each occurrence of a matching word increments the Matches counter. (Lines 81-87)

  8. Depending on the number of matches (Matches), the script determines whether the page’s corresponding Block should be visible or hidden. The Block appears to be a graphical element associated with the page, and its visibility and layout order are adjusted based on the number of matches. (Lines 89-95)

  9. The script outputs information about the search results and the process via warn and print statements, providing details about the page name, block heading, the number of matches found, and the words found on the page. This is optional and can be removed without the script breaking. (Lines 54, 55, 96-100)

Script

:warning: If you arent familiar with advanced lua coding, I suggest you thoroughly read the Step-by-step description above.

textBox = script.Parent.TextBox
local MinWordsToSearch = 3

local IgnoredWords = {
	"this",
	"for",
	"neither",
	"and",
	"not",
	"nor",
	"whether",
	"but",
	"so",
	"or",
	"such",
	"yet",
	"as",
	"also",
	"while",
	"still",
	"until",
	"too",
	"unless",
	"only",
	"since",
	"however",
	"which",
	"otherwise",
	"where",
	"who",
	"than",
	"after",
	"because",
	"either",
	"whoever",
	"nevertheless",
	"though",
	"else",
	"although",
	"if",
	"till"
}


local function Search(enterPressed, _inputObject)
	if enterPressed then
		if textBox.Text:gsub("%p", ""):len() > MinWordsToSearch - 1 then
			local Keywords = textBox.Text:lower():gsub("%p", ""):split(" ")
			for key, value in pairs(Keywords) do
				if not value:match("%S+") then
					Keywords[key] = nil
				end
			end
			warn("Search Report")
			print("Searched keywords: ", Keywords)
			for i, page in pairs(script.Parent.Pages:GetChildren()) do
				local ComparisonTable = {}
				local Matches = 0
				for i, v in pairs(page:GetDescendants()) do
					if v:IsA("TextLabel") or v:IsA("TextButton") then
						local words = v.Text:lower():gsub("%p", ""):split(" ")
						for key, value in pairs(words) do
							if not value:match("%S+") then
								words[key] = nil
							end
						end
						for index, TableChild in pairs(words) do
							table.insert(ComparisonTable, words[index])
						end
					end
				end

				for ind, obj in pairs(ComparisonTable) do
					for ind2, obj2 in pairs(IgnoredWords) do
						if obj == obj2 then
							table.remove(ComparisonTable, ind)
						end
					end
				end

				for I, V in pairs(ComparisonTable)do 
					for II, VV in pairs(Keywords) do 
						if V == VV then 
							Matches += 1
						end
					end
				end

				local Block = page.BlockRefference.Value
				if Matches > 0 then
					Block.Visible = true
					Block.LayoutOrder = Matches*-1	
				else
					Block.Visible = false
				end
				warn("Page name: " .. page.Name)
				warn("Block heading: " .. Block.Redirect.Text)
				warn("№ of matches found: " .. Matches)
				print("Words found on page: ", ComparisonTable)
				warn("———————————————————————————————————————————")
			end
		end
	end
end


textBox.FocusLost:Connect(Search)

Useful pages if you wish to modify the script for your needs:

If you have any feedback feel free to reply, it really helps!

Was this useful to you?
  • Yes
  • No
  • Maybe someday

0 voters

This search engine will be improved and used in my upcoming games, mostly in the one below:

FOS Public Testing Release V2 - Roblox

It will also be included in the upcoming sequel of Libervian Republic, a real-world simulation.

If you wish to help and become apart of the Libervian Development Team feel free to write me a message, I’m available on TalentHub or generally Roblox itself.

6 Likes

This is very good and well optimised code, I feel like this would be better as a module where it returns the data as an array of items so then the developer can help form their own UI or systems.

a small example

local searchingModule = require(paththing) -- your module
local results = searchingModule.query('a')
-- and then me or another developer can use these results to our own UI.

I like the use of the ignoreWord array though.

[quote="Calaty, post:1, topic

Way this ignored words

Way this warm massage with ---------------

1 Like