fuzzily-0.2.0.0: Filters a list based on a fuzzy string search
Safe HaskellSafe-Inferred
LanguageHaskell2010

Text.Fuzzily

Description

Fuzzy string search in Haskell. Uses TextualMonoid to be able to run on different types of strings.

Synopsis

Documentation

data Fuzzy val prettyText Source #

Included in the return type of match and filter. Contains the original value given, the rendered string and the matching score.

Constructors

Fuzzy 

Fields

Instances

Instances details
(Show val, Show prettyText) => Show (Fuzzy val prettyText) Source # 
Instance details

Defined in Text.Fuzzily

Methods

showsPrec :: Int -> Fuzzy val prettyText -> ShowS #

show :: Fuzzy val prettyText -> String #

showList :: [Fuzzy val prettyText] -> ShowS #

(Eq val, Eq prettyText) => Eq (Fuzzy val prettyText) Source # 
Instance details

Defined in Text.Fuzzily

Methods

(==) :: Fuzzy val prettyText -> Fuzzy val prettyText -> Bool #

(/=) :: Fuzzy val prettyText -> Fuzzy val prettyText -> Bool #

data CaseSensitivity Source #

Constructors

IgnoreCase 
HandleCase 

Instances

Instances details
Show CaseSensitivity Source # 
Instance details

Defined in Text.Fuzzily

Eq CaseSensitivity Source # 
Instance details

Defined in Text.Fuzzily

match Source #

Arguments

:: TextualMonoid text 
=> CaseSensitivity

Handle or ignore case of search text

-> (text, text)

Text to add before and after each match

-> (value -> text)

Function to extract the text from the container

-> text

Pattern

-> value

Value containing the text to search in

-> Maybe (Fuzzy value text)

Original value, rendered string, and score

Returns the rendered output and the matching score for a pattern and a text. Two examples are given below:

>>> match HandleCase ("", "") identity "fnt" "infinite"
Just (Fuzzy
  { original = "infinite"
  , rendered = "infinite"
  , score = 3
  })
>>> match IgnoreCase ("<", ">") fst "hsk" ("Haskell", 1995)
Just (Fuzzy
  { original = ("Haskell", 1995)
  , rendered = "<h>a<s><k>ell"
  , score = 5
  })

filter Source #

Arguments

:: TextualMonoid text 
=> CaseSensitivity

Handle or ignore case of search text

-> (text, text)

Text to add before and after each match

-> (value -> text)

Function to extract the text from the container

-> text

Pattern

-> [value]

List of values containing the text to search in

-> [Fuzzy value text]

List of results, sorted, highest score first

The function to filter a list of values by fuzzy search on the text extracted from them.

>>> langs = [("Standard ML", 1990), ("OCaml", 1996), ("Scala", 2003)]
>>> filter "ML" langs ("<", ">") fst IgnoreCase
[ Fuzzy
  { original = ("Standard ML", 1990)
  , rendered = "standard <m><l>"
  , score = 4}
, Fuzzy
  { original = ("OCaml", 1996)
  , rendered = "oca<m><l>"
  , score = 4
  }
]

simpleFilter Source #

Arguments

:: TextualMonoid text 
=> text

Pattern to look for.

-> [text]

List of texts to check.

-> [text]

The ones that match.

Return all elements of the list that have a fuzzy match against the pattern. Runs with default settings where nothing is added around the matches, as case insensitive.

>>> simpleFilter "vm" ["vim", "emacs", "virtual machine"]
["vim","virtual machine"]

test :: TextualMonoid text => text -> text -> Bool Source #

Returns false if the pattern and the text do not match at all. Returns true otherwise.

>>> test "brd" "bread"
True