---------------------------------------------------------------- -- Purpose: Scaffolding for 433-141 Spell Checker Project -- -- Authors: Martin Sulzmann, sulzmann@cs.mu.oz.au -- -- Alistair Moffat, alistair@cs.mu.oz.au -- -- Students: DO NOT EDIT OR CHANGE THIS FILE -- ---------------------------------------------------------------- module SPCLib where ---------------------- -- Type definitions -- ---------------------- -- MyWord is a list of characters type MyWord = String -- Dictionary is a list of words type Dictionary = [MyWord] -- CorrectionList is a list of tuples, where each tuple includes -- a line number, a word thought to be in error, and a list of -- possible corrected spellings type LineNum = Int type CorrectionList = [(LineNum, MyWord, [MyWord])] -- A WordList is the same thing, but without the list of possible -- correct spellings type WordList = [(LineNum, MyWord)] -- SpellCheck describes a function which takes a dictionary and a string, -- and checks the words in the string against the dictionary and outputs -- a list of possibly misspelt words and possible replacement spellings type SpellCheck = Dictionary -> String -> IO () -------------------------- -- Function definitions -- -------------------------- -- noAnswers is what you say when you can't make any suggestions about -- how an incorrect word should be spelt noAnswers :: [MyWord] noAnswers = ["sorry, no suggestions can be made"] -- prepareWords processes a string into a WordList, by breaking -- into lines and then words, and tagging each word with its line number prepareWords :: (String -> [MyWord]) -> String -> WordList prepareWords getWords string = [(num, w) | (num, line) <- zip [1..] (lines string), w <- getWords line] -- presentNicely takes a CorrectionList and generates a formatted -- output string, ready for putStr presentNicely :: CorrectionList -> String presentNicely [] = "" presentNicely ((num, w, cs) : numwcs) = "on line " ++ show num ++ " word " ++ show w ++ " might in fact be one of:" ++ concat (map ((++) "\n\t") cs) ++ "\n" ++ presentNicely numwcs -- A dictionary for you to use in your testing smalldict :: Dictionary smalldict = [ "a", "all", "and", "as", "bell", "cockle", "contrary", "does", "fleece", "fly", "garden", "glass", "grow", "had", "he", "how", "in", "it", "lamb", "little", "maid", "marigold", "mary", "marry", "pretty", "quite", "row", "said", "she", "shelf", "shell", "silver", "some", "snow", "spy", "the", "they", "upon", "was", "what", "when", "while", "white", "who", "why", "will", "with", "your" ] -- And some sample strings... mary1 = "Mary had a little lamb little lamb little lamb\n" ++ "Mary had a little lamb\nit's fleece was white as snow\n" mary2 = "Mary Mry\nquite contrary,\nhow does your garden grow?\n" ++ "With silver bells and cocle shells,\nand pretty maids alll in a row.\n" mary3 = "Mary spies some little flies, little flies, little flies;\n" ++ "Mary spies sme little flys\nupon the wine glass shelves."