模糊搜索目录名的最佳算法

时间:2013-12-09 14:25:58

标签: algorithm fuzzy-search

模糊搜索目录名称的最佳/适当算法是什么?我想实现一个bash完成,它使用模糊搜索完成目录/文件名,但似乎算法依赖于要匹配的字符串集。

1 个答案:

答案 0 :(得分:1)

嗯......这是一个有趣的建议。我会做这样的事情:

首先,解析文件路径以获取最后一个斜杠后面的文本

IFS='/' read -a filepath <<< '$string'
dirname=${filepath[${#filepath[@] - 1]}

接下来,使用find获取当前路径中的所有直接子目录,并将它们添加到bash完成选项中。您可以使用=~运算符代替模糊搜索,如in this answer

所述
for i in 'find . -type d -maxdepth 1'; do
  if [[ i =~ $dirname ]]; then
    //add to bash completion option, unsure how to do this part
  fi
done

但是,请注意=~是一个仅限bash的运算符。