Created
April 21, 2015 01:27
-
-
Save DerekV/ccc229203b4d8cc784ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Map (Map) | |
import Data.MultiSet (fromList, toOccurList) | |
import Control.Monad | |
-- http://lpaste.net/131001 | |
data Pint something = Pint something | EmptyPint | |
deriving (Show, Eq) | |
data Drink = Water | Milk | Tea | Beer | |
deriving (Show, Eq) | |
derek :: Pint Drink -> Pint Drink | |
derek EmptyPint = EmptyPint | |
derek (Pint Beer) = EmptyPint | |
derek (Pint x) = Pint x | |
-- main :: IO () | |
-- main = do | |
-- putStrLn "Hello, World!" | |
-- putStrLn $ "Derek given a " ++ show (Pint Milk) ++ " gives you " ++ | |
-- show ( derek (Pint Milk)) | |
-- putStrLn $ "Derek given a " ++ show (Pint Beer) ++ " gives back a " ++ | |
-- show ( derek (Pint Beer)) | |
-- showEach . findMostFrequent 10 . countWordFrequency $ source | |
-- countWordFrequency :: String -> FreqCount String | |
-- countWordFrequency = map (\l -> (length l, head l)) . group . sort . words | |
-- findMostFrequent :: Int -> FreqCount String -> FreqCount String | |
-- findMostFrequent howmany counts = | |
-- take howmany $ | |
-- (sortBy (comparing (Down . fst))) $ | |
-- counts | |
type FreqCount x = [(x,Int)] | |
showEach :: Show x => [x] -> IO () | |
showEach = mapM_ (putStrLn . show) | |
normalizeText :: String -> String | |
normalizeText = filter (not . isPunctuation ) . map toLower | |
getWords :: String -> [String] | |
getWords = words . normalizeText | |
getWordPairs :: String -> [(String,String)] | |
getWordPairs source = [ (a, b) | a:b:_ <- tails allWords ] | |
where allWords = getWords source | |
countFrequency :: Ord x => [x] -> FreqCount x | |
countFrequency = toOccurList . fromList | |
sortFrequencies :: (Ord x) => FreqCount (w,x) -> FreqCount (w,x) | |
sortFrequencies = sortBy (comparing (Down .snd)) | |
countWordPairs :: String -> FreqCount (String,String) | |
countWordPairs = sortFrequencies . countFrequency . getWordPairs | |
main :: IO() | |
main = do | |
source <- getContents | |
showEach | |
$ take 15 | |
$ countWordPairs source |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment