-- Test function definition (your calculateStringSimilarity function goes here) local prefix_weight = 0.1 local threshold = 0.8 local function test(str1, str2) local similarity = calculateStringSimilarity(str1, str2) local result = string.format('Comparing "%s" with "%s": %.3f', str1, str2, similarity) if threshold then local passes = similarity >= threshold result = result .. string.format(' [Threshold %.2f: %s]', threshold, passes and "PASS" or "FAIL") end print(result) return similarity end function calculateStringSimilarity(str1, str2) str1, str2 = str1:lower(), str2:lower() if str1 == str2 then return 1.0 end local len1, len2 = #str1, #str2 local str1_matches, str2_matches = {}, {} local match_distance = math.floor(math.max(len1, len2) / 2) - 1 local matches = 0 -- Count the number of matching characters for i = 1, len1 do local start = math.max(1, i - match_distance) local ends = math.min(i + match_distance, len2) for j = start, ends do if str2:sub(j,j) == str1:sub(i,i) and not str2_matches[j] then str1_matches[i] = true str2_matches[j] = true matches = matches + 1 break end end end if matches == 0 then return 0.0 end -- Count how many characters needed to change str1 into str2 local k = 1 local transpositions = 0 for i = 1, len1 do if str1_matches[i] then while not str2_matches[k] do k = k + 1 end if str1:sub(i,i) ~= str2:sub(k,k) then transpositions = transpositions + 1 end k = k + 1 end end -- With those two numbers, calculate the similary -- score with Jaro string comparison formula local distance = ( matches / len1 + matches / len2 + (matches - transpositions/2) / matches ) / 3 -- Set up for adjusting the score by prefix weight local prefix_length = 0 for i = 1, math.min(4, math.min(len1, len2)) do if str1:sub(i,i) == str2:sub(i,i) then prefix_length = prefix_length + 1 else break end end return distance + (prefix_length * prefix_weight * (1 - distance)) end -- Test here test("Elephant", "elephant") test("Elephant", "élephant") test("Elephant", "olephant") test("Elephant", "esephant")
Write, Run & Share Lua code online using OneCompiler's Lua online compiler for free. It's one of the robust, feature-rich online compilers for Lua language, running the latest Lua version 5.4. Getting started with the OneCompiler's Lua editor is easy and fast. The editor shows sample boilerplate code when you choose language as Lua and start coding.
OneCompiler's Lua online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Lua program which takes name as input and prints hello message with your name.
name = io.read("*a")
print ("Hello ", name)
Lua is a light weight embeddable scripting language which is built on top of C. It is used in almost all kind of applications like games, web applications, mobile applications, image processing etc. It's a very powerful, fast, easy to learn, open-source scripting language.
-- global variables
a = 10
-- local variables
local x = 30
Value Type | Description |
---|---|
number | Represents numbers |
string | Represents text |
nil | Differentiates values whether it has data or not |
boolean | Value can be either true or false |
function | Represents a sub-routine |
userdata | Represents arbitary C data |
thread | Represents independent threads of execution. |
table | Can hold any value except nil |
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition)
do
--code
end
Repeat-Until is also used to iterate a set of statements based on a condition. It is very similar to Do-While, it is mostly used when you need to execute the statements atleast once.
repeat
--code
until( condition )
For loop is used to iterate a set of statements based on a condition.
for init,max/min value, increment
do
--code
end
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increase re-usuability and modularity.
optional_function_scope function function_name( argument1, argument2, argument3........, argumentn)
--code
return params with comma seperated
end