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
65
66
67
68
69
70
71
72
73
|
local log = require('telescope.log')
local util = require('telescope.utils')
local sorters = {}
local Sorter = {}
Sorter.__index = Sorter
---@class Sorter
--- Sorter sorts a list of results by return a single integer for a line,
--- given a prompt
---
--- Lower number is better (because it's like a closer match)
--- But, any number below 0 means you want that line filtered out.
--- @field scoring_function function Function that has the interface:
-- (sorter, prompt, line): number
function Sorter:new(opts)
opts = opts or {}
return setmetatable({
state = {},
scoring_function = opts.scoring_function,
}, Sorter)
end
function Sorter:score(prompt, line)
return self:scoring_function(prompt, line)
end
function sorters.new(...)
return Sorter:new(...)
end
sorters.Sorter = Sorter
sorters.get_ngram_sorter = function()
return Sorter:new {
scoring_function = function(_, prompt, line)
if prompt == "" or prompt == nil then
return 1
end
local ok, result = pcall(function()
local ngram = util.new_ngram { N = 4 }
ngram:add(line)
local score = ngram:score(prompt)
if score == 0 then
return -1
end
-- return math.pow(math.max(score, 0.0001), -1)
return score
end)
print(prompt, line, result)
return ok and result or 1
end
}
end
sorters.get_levenshtein_sorter = function()
return Sorter:new {
scoring_function = function(_, prompt, line)
local result = require('telescope.algos.string_distance')(prompt, line)
log.info("Sorting result for", prompt, line, " = ", result)
return result
end
}
end
return sorters
|