Yggdroot / LeaderF

An efficient fuzzy finder that helps to locate files, buffers, mrus, gtags, etc. on the fly for both vim and neovim.
Apache License 2.0
2.14k stars 180 forks source link

[feature-request] 基于 frecency 的 mru 排序 #1030

Closed skywind3000 closed 1 year ago

skywind3000 commented 1 year ago

frecency 是 firefox 发明的算法:frequency + recent 同时参考了访问次数(frequency)和最近访问时间(recent),

具体分数计算如下:

function data_update_frecent(M)
    local current = os.time()
    local i
    for i = 1, #M do
        local item = M[i]
        local dx = current - item.time
        if dx < 3600 then
            item.frecent = item.rank * 4
        elseif dx < 86400 then
            item.frecent = item.rank * 2
        elseif dx < 604800 then
            item.frecent = item.rank * 0.5
        else
            item.frecent = item.rank * 0.25
        end
    end
    return M
end

解释:

然后按照 frecent 排序,这样即便访问次数较少的文件,如果是最近访问过,它的 frecent 分数也可以迅速提升,排到前面。

而即便访问次数高的文件,如果一段时间不访问,也会随着时间推移逐步下移。

Firefox 靠这个算法来做 url 匹配的历史排序,不少其他软件也用了,比如 z.lua 比如

https://github.com/nvim-telescope/telescope-frecency.nvim

skywind3000 commented 1 year ago
/mnt/c/Users/Linwei|3|1690796392
/etc|1|1690794920
/etc/ssh|1|1690795171
/home/skywind/.local|5|1693453413
/home/skywind/.local/etc|6|1693455843
/home|1|1690795322
/home/data|3|1693453583
/home/data/python|1|1690795381
/home/data/python/bin|1|1690795385
/etc/supervisor/conf.d|1|1690795475

z.lua 的数据库就三个字段:路径,访问次数,最后访问时间戳。

不过这个数据为了兼容老的 z.sh 才写成这样,路径中出现竖线就挂了。

查看计算结果:

$ z -l  
1.75       /home/skywind/github/bbnet/example/build
2          /home/skywind/.config
2          /home/skywind/.config/nvim
2          /usr/local/opt
2          /home/skywind/.local/share/nvim
2          /home/skywind/.local/share
2          /home/skywind/.local/bin
2          /home/data/app/clangd_16.0.2/bin
2          /home/data/app/clangd_16.0.2
2.25       /home/skywind/github/bbnet
2.25       /home/skywind/github
2.25       /home/skywind/software
4.5        /home/skywind/software/vim
6          /home/data
8          /usr/local/app
10         /home/skywind/.local
12         /home/skywind/.local/etc
20         /home/data/app
Yggdroot commented 1 year ago

好了。 https://github.com/Yggdroot/LeaderF/blob/6aebb6c6b1019499aed2815e86b59b831915af82/doc/leaderf.txt#L1091-L1095

skywind3000 commented 1 year ago

多谢

Yggdroot commented 1 year ago

np