module Main where import List import Char main :: IO () main = interact translate translate :: String -> String translate = display . reverse . sort . freqs . group . sort . words freqs :: [[a]] -> [(Int,a)] freqs xs = [(length x, head x) | x <- xs] display :: [(Int,String)] -> String display xs = unlines [snd x ++ " " ++ show (fst x) | x <- xs] -- additions -- remove "hapax legomena", i.e. words that occur only once notHapax :: [(Int,String)] -> [(Int,String)] notHapax xs = [x | x <- xs, fst x > 1] -- histogram from requency table histogram :: [(Int,String)] -> String histogram xs = unlines [snd x ++ "\t\t"++ replicate (proportion xs (fst x)) '*' | x <- xs] -- a number in proportion to maximal frequency, in percent proportion :: [(Int,String)] -> Int -> Int proportion xs i = div (50 * i) (fst (head xs)) -- leave only letter and spaces normalize :: String -> String normalize s = [toLower x | x <- s, isAlpha x || isSpace x] -- remove HTML tags unHTML :: String -> String unHTML [] = [] unHTML s = let (s1,s2) = span (/= '<') s (_, s3) = span (/= '>') s2 in s1 ++ unHTML (drop 1 s3)