-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchNoteList.module.lua
More file actions
64 lines (53 loc) · 1.62 KB
/
Copy pathSearchNoteList.module.lua
File metadata and controls
64 lines (53 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
local SearchNoteList = {}
local function newMatrix(N, M)
local mt = {}
for i = 0, N do
mt[i] = {}
for j = 0, M do
mt[i][j] = 0
end
end
return mt
end
local function LevenshteinDistance(strA: string, strB: string): number
local rows = string.len(strA)
local cols = string.len(strB)
if rows == 0 then --Length of a is 0? The distance is just the length of b, because it would take that many substitutions to make b
return cols
elseif cols == 0 then
return rows
elseif strA == strB then --Same string? The distance is 0
return 0
end
local matrix = newMatrix(rows, cols) --Create a matrix or 2D array
--Initialize Matrix
for i = 1, rows do
matrix[i][0] = i
end
for j = 1, cols do
matrix[0][j] = j
end
for i = 1, rows do --If you initialized your matrix with 1s instead of 0s, start at 2 instead of 1 here
for j = 1, cols do
if (strA:byte(i) == strB:byte(i)) then
--If the two characters in both strings are the same
matrix[i][j] = matrix[i-1][j-1]
else
local insertion = matrix[i][j-1] + 1
local deletion = matrix[i - 1][j] + 1
local subsitution = matrix[i - 1][j - 1] + 1
matrix[i][j] = math.min(insertion, deletion, subsitution)
end
end
end
return matrix[rows][cols]--, matrix
end
function SearchNoteList:SearchList(stringToSearch,stringList : {{string}})
local probabilities = {}
for i,v in pairs(stringList) do
table.insert(probabilities,{v.id,LevenshteinDistance(stringToSearch, v.title)})
end
table.sort(probabilities, function(a,b) return a[2] < b[2] end)
return probabilities
end
return SearchNoteList