ECDICT icon indicating copy to clipboard operation
ECDICT copied to clipboard

输入错误单词后出现“你是不是要找……”的算法

Open redstoneleo opened this issue 7 years ago • 7 comments

比如我在必应在线词典查询senario https://cn.bing.com/dict/search?q=senario&go=搜索&qs=ds&form=Z9LH5 这个单词拼写不对,然后它给出了“你是不是要找……”的如下内容,请问这是什么算法?本项目里有用到吗?

您要找的是不是 音近词 scenario (可能发生的)情况, 前景; 剧情概要, 剧本;方案,方式,规划 scenarios (可能发生的)情况, 前景; 剧情概要, 剧本;方案,方式,规划 stereo 立体(声)的 stereos 立体(声)的 scenery 风景;景色;风光;景致;背景;风景画;舞台面;scenery布景 形近词 scenario (可能发生的)情况, 前景; 剧情概要, 剧本;方案,方式,规划

redstoneleo avatar Jun 14 '18 04:06 redstoneleo

是不是startdict.py里的match()就是这个功能? 我用的是mdx格式词典,所以这个函数不通用了吧?

redstoneleo avatar Jun 14 '18 04:06 redstoneleo

startdict.py里的match()不符合我的预期,也不知道有啥用……

https://github.com/OpenGov/cython_hunspell 最终这个就是我要找的

redstoneleo avatar Jun 15 '18 14:06 redstoneleo

这个在php里用levenshtein函数即可。

function didyoumean($str){ // 输入拼写错误的单词 $input = strtolower($str); // 要检查的单词数组 $file_path = "Journalist_dic.txt"; if(file_exists($file_path)){ $file_into_str = file_get_contents($file_path);//将整个文件内容读入到一个字符串中 $words = explode("\n", $file_into_str); } // 目前没有找到最短距离 $shortest = -1; // 遍历单词来找到最接近的 foreach ($words as $word) { // 计算输入单词与当前单词的距离 $lev = levenshtein($input, $word); // 检查完全的匹配 if ($lev == 0) { // 最接近的单词是这个(完全匹配) $closest = $word; $shortest = 0; // 退出循环;我们已经找到一个完全的匹配 break; } // 如果此次距离比上次找到的要短 或者还没找到接近的单词 if ($lev <= $shortest || $shortest < 0) { // 设置最接近的匹配以及它的最短距离 $closest = $word; $shortest = $lev; } } return $closest; }

效果如图: image

dalianblue avatar Jun 15 '18 15:06 dalianblue

你如果要模糊匹配,那么可以把所有单词做成一个文本文件,然后用 fzf 进行模糊匹配。 你还可以把 fzf 做成一个模糊匹配的service。

你如果想开发,可以自己实现 fzf 的功能。

skywind3000 avatar Jun 15 '18 15:06 skywind3000

match 后面有个参数,默认是 false,可以允许看看。

skywind3000 avatar Jun 15 '18 15:06 skywind3000

https://github.com/jhawthorn/fzy 还有这个

skywind3000 avatar Jun 15 '18 15:06 skywind3000

你们如果用字符串近似一个个比较的话430万单词,要计算废掉的,进一步优化还可以把词头分区,比如: 包含:a, aa, ab, ac, ..., az, 包含:b, ba, bb, bc, ... bz, ... 包含:z, za, zb, zc, ..., zz,

一共26x27个文件,根据用户输入的头1到两个字母先确定索引,送到fzf里匹配头20个,然后再用字符串相似法按相似度排序,就是你想要的

skywind3000 avatar Jun 15 '18 15:06 skywind3000