I am planning on using this script for a word cloud.
It parses a text file, and spits out individual word counts.
Anybody have any tips / improvements?
Suspect there are a few bits and pieces which could be nicer.
module Lib where
import Data.Map.Strict
import Text.ParserCombinators.Parsec
import Data.List
import Data.Char
mn = do
content <- readFile "big.txt"
case (parseTextFile content) of
Right rs -> print $ sort' $ countWords $ Data.List.map (\s -> Data.List.map toLower s) $ concat rs
e -> print e
countWords :: [String] -> Map String Int
countWords = Prelude.foldl (\b a -> insertWith (+) a 1 b) empty
sort' :: Map String Int -> [(String, Int)]
sort' ts = sortBy (\ (_, a) (_, b) -> compare a b) (toList ts)
textFile = endBy line eol
line = sepBy word (many1 space')
word = many $ noneOf ( eolString ++ ignoreString )
eol = many1 $ oneOf eolString
space' = oneOf ignoreString
eolString = "\n\r"
ignoreString = " \t.,\"'-_#:;()?*!|"
parseTextFile :: String -> Either ParseError [[String]]
parseTextFile input = parse textFile "(unknown)" input