Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
This module provides a showDistance
function showing the differences between 2 pieces of text
using the Levenshtein distance. That distance is defined as the minimum number of edits: insertions, deletions, substitutions
to go from one text to another.
Several options are available to customize this processing: - split size: texts are broken into new lines first. Then if the texts are too large there are split into smaller pieces in order to compute their difference. This is done in order to reduce the size of the edit matrix which is used to compute all the edit costs the default is 200
- separators: opening and closing pieces of text (brackets by default) used to highlight a difference
- shorten size: there is the possibly to display mostly the differences with a bit of context around if the input text is too large. The text gets elided around separators if it gets greater than the shorten size (the default is 20)
- shorten text: the text to use when eliding characters in the original text (the default is "...")
- display edit operations: edit operations, insertdeletesubstitute/keep can be annotated if necessary
Here are some examples:
import Data.Text.Edits -- "between the e and the n the letter i was added" showDistance "kitten" "kittein" === "kitte[+i]n" -- "at the end of the text 3 letters have been deleted" showDistance "kitten" "kit" === "kit[-t-e-n]" -- "between the t and the n 2 letters have been modified" showDistance "kitten" "kitsin" === "kit[~ts~ei]"
Synopsis
- newtype SplitSize = SplitSize Int
- data ShortenOptions = ShortenOptions {
- _shortenSize :: Int
- _shortenText :: Text
- data Separators = Separators {}
- data DisplayOptions = DisplayOptions {}
- data EditOperation a
- = Insert a
- | Delete a
- | Substitute a a
- | Keep a
- data Color
- colorAs :: Color -> Text -> Text
- showDistance :: Text -> Text -> Text
- showDistanceColored :: Text -> Text -> Text
- showDistanceWith :: SplitSize -> DisplayOptions -> Text -> Text -> Text
- levenshteinOperations :: Text -> Text -> [EditOperation Char]
- defaultDisplayOptions :: DisplayOptions
- defaultDisplayEditOperations :: EditOperation Char -> Text
- coloredDisplayEditOperation :: EditOperation Char -> Text
- defaultSplitSize :: SplitSize
- parensSeparators :: Separators
- bracketsSeparators :: Separators
- makeCharSeparators :: Char -> Char -> Separators
Documentation
Size to use when splitting a large piece of text
data ShortenOptions Source #
Size used to decide if a piece of text needs to be shortened
Instances
Show ShortenOptions Source # | |
Defined in Data.Text.Shorten showsPrec :: Int -> ShortenOptions -> ShowS # show :: ShortenOptions -> String # showList :: [ShortenOptions] -> ShowS # | |
Eq ShortenOptions Source # | |
Defined in Data.Text.Shorten (==) :: ShortenOptions -> ShortenOptions -> Bool # (/=) :: ShortenOptions -> ShortenOptions -> Bool # |
data Separators Source #
Separators are used to highlight a difference between 2 pieces of text for example
Instances
Show Separators Source # | |
Defined in Data.Text.Difference showsPrec :: Int -> Separators -> ShowS # show :: Separators -> String # showList :: [Separators] -> ShowS # | |
Eq Separators Source # | |
Defined in Data.Text.Difference (==) :: Separators -> Separators -> Bool # (/=) :: Separators -> Separators -> Bool # |
data DisplayOptions Source #
Options to use for displaying differences
data EditOperation a Source #
Atomic operation required to edit a piece of text at a given position in the EditMatrix
Insert a | |
Delete a | |
Substitute a a | |
Keep a |
Instances
Show a => Show (EditOperation a) Source # | |
Defined in Data.Text.EditOperation showsPrec :: Int -> EditOperation a -> ShowS # show :: EditOperation a -> String # showList :: [EditOperation a] -> ShowS # | |
Eq a => Eq (EditOperation a) Source # | |
Defined in Data.Text.EditOperation (==) :: EditOperation a -> EditOperation a -> Bool # (/=) :: EditOperation a -> EditOperation a -> Bool # |
Available ASCII colors
colorAs :: Color -> Text -> Text Source #
Surround a piece of text with ASCII control characters to color it
showDistanceColored :: Text -> Text -> Text Source #
Show the distance between 2 pieces of text with colors instead of symbols
showDistanceWith :: SplitSize -> DisplayOptions -> Text -> Text -> Text Source #
Show the distance between 2 pieces of text and specify splitting / display options
levenshteinOperations :: Text -> Text -> [EditOperation Char] Source #
Return the list of operations necessary to go from one piece of text to another using the Levenshtein distance
defaultDisplayOptions :: DisplayOptions Source #
Default display options
defaultDisplayEditOperations :: EditOperation Char -> Text Source #
Display an edit operation by prepending a symbol showing which operation is used
coloredDisplayEditOperation :: EditOperation Char -> Text Source #
Display an edit operation using ascii colors: green = added, red = removed, blue = substituted
defaultSplitSize :: SplitSize Source #
Default split size
parensSeparators :: Separators Source #
Make parens separators
bracketsSeparators :: Separators Source #
Make brackets separators
makeCharSeparators :: Char -> Char -> Separators Source #
Make separators with simple Chars