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
請打開一個問題,以便我們開始討論。非常歡迎任何改善圖書館的幫助:)。