fuzzify
v0.1.0
一个用于模糊搜索的小型轻量级库。
我之所以创建这个库,是因为了解了 Levenshtein Distance 算法,该算法用于计算 Vladimir Levenshtein 将一个单词转换为另一个单词所需的单字符编辑(插入、删除或替换)的最小数量。
笔记
注意:该库还处于非常早期的阶段,因此如果您想帮助改进它,请提出一个问题。
您可以在此处查看演示。
有yarn
yarn add fuzzify
与npm
npm install fuzzify
import Fuzzy from "fuzzify" ;
const countries = [
"Australia" ,
"France" ,
"Germany" ,
"Hungary" ,
"Iceland" ,
"India" ,
"Israel" ,
"Italy" ,
"Japan" ,
"Malawi" ,
"Malaysia" ,
"Maldives" ,
] ;
const fuzzy = new Fuzzy ( countries ) ;
const query = "ala" ;
const results = fuzzy . search ( query ) ;
console . log ( "RESULTS" , results ) ;
search
API 会按照以下格式提供与传递的查询近似匹配的字符串。
属性 | 描述 |
---|---|
文本 | 查询匹配的目标字符串 |
距离 | 将查询转换为目标文本所需的最少编辑次数(插入/删除/替换)。 |
[
{
text : "Malawi" ,
distance : 3 ,
} ,
{
text : "Malaysia" ,
distance : 5 ,
} ,
{
text : "Australia" ,
distance : 6 ,
} ,
{
text : "Italy" ,
distance : 3 ,
} ,
{
text : "Japan" ,
distance : 3 ,
} ,
{
text : "Iceland" ,
distance : 5 ,
} ,
{
text : "Maldives" ,
distance : 6 ,
} ,
{
text : "Israel" ,
distance : 5 ,
} ,
{
text : "India" ,
distance : 4 ,
} ,
{
text : "France" ,
distance : 5 ,
} ,
{
text : "Germany" ,
distance : 6 ,
} ,
{
text : "Hungary" ,
distance : 6 ,
} ,
] ;
includeMatches
includeMatches
- 确定是否应在响应中返回字符匹配的indices
。每个match
元素由两个索引组成 -
例子 ?
query = "ala" , target string = "Australia"
matches : [
[ 0 , 5 ] ,
[ 1 , 6 ] ,
[ 2 , 8 ] ,
] ,
在上面的例子中?找到匹配项
ala
中0th
索引处的字符a
与Australia
5th
索引处的字符a
匹配ala
中1st
索引处的字符l
与Australia
6th
索引处的字符a
匹配ala
中2nd
索引处的字符a
与Australia
8th
索引处的字符a
匹配完整的回应是?
[
{
text : "Malawi" ,
distance : 3 ,
matches : [
[ 0 , 1 ] ,
[ 1 , 2 ] ,
[ 2 , 3 ] ,
] ,
} ,
{
text : "Malaysia" ,
distance : 5 ,
matches : [
[ 0 , 1 ] ,
[ 1 , 2 ] ,
[ 2 , 7 ] ,
] ,
} ,
{
text : "Australia" ,
distance : 6 ,
matches : [
[ 0 , 5 ] ,
[ 1 , 6 ] ,
[ 2 , 8 ] ,
] ,
} ,
{
text : "Italy" ,
distance : 3 ,
matches : [
[ 0 , 2 ] ,
[ 1 , 3 ] ,
] ,
} ,
{
text : "Japan" ,
distance : 3 ,
matches : [
[ 0 , 1 ] ,
[ 2 , 3 ] ,
] ,
} ,
{
text : "Iceland" ,
distance : 5 ,
matches : [
[ 1 , 3 ] ,
[ 2 , 4 ] ,
] ,
} ,
{
text : "Maldives" ,
distance : 6 ,
matches : [
[ 0 , 1 ] ,
[ 1 , 2 ] ,
] ,
} ,
{
text : "Israel" ,
distance : 5 ,
matches : [
[ 0 , 3 ] ,
[ 1 , 5 ] ,
] ,
} ,
{
text : "India" ,
distance : 4 ,
matches : [ [ 2 , 4 ] ] ,
} ,
{
text : "France" ,
distance : 5 ,
matches : [ [ 2 , 2 ] ] ,
} ,
{
text : "Germany" ,
distance : 6 ,
matches : [ [ 2 , 4 ] ] ,
} ,
{
text : "Hungary" ,
distance : 6 ,
matches : [ [ 2 , 4 ] ] ,
} ,
] ;
includeScore
确定是否应在结果中添加分数。 1
分表示完全匹配,但0
分表示不匹配,这些选项将从结果中删除。如果您想获得结果中的所有选项,请打开一个问题并让我们讨论。
caseSensitive
确定查询是否应区分大小写。默认情况下,此选项为false
。
安装包:
yarn
开始开发游乐场:
yarn start
构建命令:
yarn build
请提出一个问题,以便我们开始讨论。非常欢迎任何改进图书馆的帮助:)。