tripokey / elm-fuzzy / Fuzzy

This is library for performing fuzzy string matching.

Customization

addPenalty : Basics.Int -> Config

Create a penalty configuration that is applied to each additional character in hay.

removePenalty : Basics.Int -> Config

Create a penalty configuration that is applied to each additional character in needle.

movePenalty : Basics.Int -> Config

Create a penalty configuration that is applied to each out of order character in hay.

insertPenalty : Basics.Int -> Config

Create a penalty configuration that is applied to each character between two keys.


type Config

Represents a configuration element for customization.

Matching

match : List Config -> List String -> String -> String -> Result

Perform fuzzy matching between a query String (needle) and a target String (hay). The order of the arguments are significant. Lower score is better. Specifying some separators will allow for partial matching within a sentence. The default configuration is addPenalty = 10, movePenalty = 1000, removePenalty = 10000, insertPenalty = 1.

let
    simpleMatch config separators needle hay =
        match config separators needle hay |> .score
in
simpleMatch [] [] "test" "test"
    == 0
        simpleMatch
        []
        []
        "tes"
        "test"
    == 10
        simpleMatch
        [ addPenalty 10000 ]
        []
        "tes"
        "test"
    == 10000
        simpleMatch
        []
        []
        "tst"
        "test"
    == 11
        simpleMatch
        []
        []
        "test"
        "tste"
    == 1000
        simpleMatch
        []
        []
        "test"
        "tst"
    == 10000
        simpleMatch
        []
        [ "/" ]
        "/u/b/s"
        "/usr/local/bin/sh"
    == 50
        simpleMatch
        []
        []
        "/u/b/s"
        "/usr/local/bin/sh"
    == 2116
        List.sortBy
        (simpleMatch [] [] "hrdevi")
        [ "screen", "disk", "harddrive", "keyboard", "mouse", "computer" ]
    == [ "harddrive", "keyboard", "disk", "screen", "computer", "mouse" ]


type alias Result =
{ score : Basics.Int
, matches : List Match 
}

Represents the result of a match. score is the total score of the result. matches is a list of matching words within the hay.


type alias Match =
{ score : Basics.Int
, offset : Basics.Int
, length : Basics.Int
, keys : List Key 
}

Represents a matching word in hay. score is the score that this Match contributes to the total score in a Result. offset is the index where this match starts in the hay. length is the length of the match. keys is a list of matching indexes within the word. The keys are relative to the offset.


type alias Key =
Basics.Int

Represents a matching character in a Match.