Book Image

Haskell Data Analysis Cookbook

By : Nishant Shukla
Book Image

Haskell Data Analysis Cookbook

By: Nishant Shukla

Overview of this book

Table of Contents (19 chapters)
Haskell Data Analysis Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Finding strings within one-edit distance


This recipe will demonstrate how to find strings that are one-edit distance away from a specified string. This function can be used to correct spelling.

Getting ready

The algorithm in this recipe is based heavily on Peter Norvig's spell corrector algorithm described at http://norvig.com/spell-correct.html. Take a look at and study the edits1 Python function implemented there.

How to do it...

  1. Import a couple of character and list functions as follows:

    import Data.Char (toLower)
    import Data.List (group, sort)
  2. Define a function to return strings that are one-edit distance away, as shown in the following code snippet:

    edits1 :: String -> [String]
    
    edits1 word = unique $ 
                  deletes ++ transposes ++ replaces ++ inserts
      where splits   = [ (take i word', drop i word') | 
        i <- [0..length word']]
  3. Create a list of strings with one character deleted, as follows:

      deletes = [ a ++ (tail b) | 
        (a,b) <- splits, (not.null) b]
  4. Create a list of...