module TFwH.CommonWords where
import Data.Char (toLower)
import Data.List (sort,sortBy,group)
import Data.Ord (comparing)
import Prelude hiding (Word)
type Text = [Char]
type Word = [Char]
commonWords :: Int -> Text -> String
commonWords n = unlines
. map showRun
. take n
. sortRuns
. countRuns
. sortWords
. words
. map toLower
sortWords :: [Word] -> [Word]
sortWords = sort
countRuns :: [Word] -> [(Int, Word)]
countRuns = map addCount . group
where
addCount ws = (length ws, head ws)
sortRuns :: [(Int, Word)] -> [(Int, Word)]
sortRuns = sortBy (flip (comparing fst))
showRun :: (Int, Word) -> String
showRun (n, w) = w ++ ": " ++ show n