Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
41
Task/Word-frequency/Haskell/word-frequency-1.hs
Normal file
41
Task/Word-frequency/Haskell/word-frequency-1.hs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
module Main where
|
||||
|
||||
import Control.Category -- (>>>)
|
||||
import Data.Char -- toLower, isSpace
|
||||
import Data.List -- sortBy, (Foldable(foldl')), filter -- '
|
||||
import Data.Ord -- Down
|
||||
import System.IO -- stdin, ReadMode, openFile, hClose
|
||||
import System.Environment -- getArgs
|
||||
|
||||
-- containers
|
||||
import Data.Map.Strict (Map)
|
||||
import qualified Data.Map.Strict as M
|
||||
import qualified Data.IntMap.Strict as IM
|
||||
|
||||
-- text
|
||||
import Data.Text (Text)
|
||||
import qualified Data.Text as T
|
||||
import qualified Data.Text.IO as T
|
||||
|
||||
frequencies :: Ord a => [a] -> Map a Integer
|
||||
frequencies = foldl' (\m k -> M.insertWith (+) k 1 m) M.empty -- '
|
||||
{-# SPECIALIZE frequencies :: [Text] -> Map Text Integer #-}
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
args <- getArgs
|
||||
(n,hand,filep) <- case length args of
|
||||
0 -> return (10,stdin,False)
|
||||
1 -> return (read $ head args,stdin,False)
|
||||
_ -> let (ns:fp:_) = args
|
||||
in fmap (\h -> (read ns,h,True)) (openFile fp ReadMode)
|
||||
T.hGetContents hand >>=
|
||||
(T.map toLower
|
||||
>>> T.split isSpace
|
||||
>>> filter (not <<< T.null)
|
||||
>>> frequencies
|
||||
>>> M.toList
|
||||
>>> sortBy (comparing (Down <<< snd)) -- sort the opposite way
|
||||
>>> take n
|
||||
>>> print)
|
||||
when filep (hClose hand)
|
||||
38
Task/Word-frequency/Haskell/word-frequency-2.hs
Normal file
38
Task/Word-frequency/Haskell/word-frequency-2.hs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
module Main where
|
||||
|
||||
import Control.Monad (foldM, when)
|
||||
import Data.Char (isSpace, toLower)
|
||||
import Data.List (sortOn, filter)
|
||||
import Data.Ord (Down(..))
|
||||
import System.IO (stdin, IOMode(..), openFile, hClose)
|
||||
import System.Environment (getArgs)
|
||||
import Data.IORef (IORef(..), newIORef, readIORef, modifyIORef') -- '
|
||||
|
||||
-- containers
|
||||
import Data.HashMap.Strict (HashMap)
|
||||
import qualified Data.HashMap.Strict as M
|
||||
|
||||
-- text
|
||||
import Data.Text (Text)
|
||||
import qualified Data.Text as T
|
||||
import qualified Data.Text.IO as T
|
||||
|
||||
frequencies :: [Text] -> IO (HashMap Text (IORef Int))
|
||||
frequencies = foldM (flip (M.alterF alter)) M.empty
|
||||
where
|
||||
alter Nothing = Just <$> newIORef (1 :: Int)
|
||||
alter (Just ref) = modifyIORef' ref (+ 1) >> return (Just ref) -- '
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
args <- getArgs
|
||||
when (length args /= 1) (error "expecting 1 arg (number of words to print)")
|
||||
let maxw = read $ head args -- no error handling, to simplify the example
|
||||
T.hGetContents stdin >>= \contents -> do
|
||||
freqtable <- frequencies $ filter (not . T.null) $ T.split isSpace $ T.map toLower contents
|
||||
counts <-
|
||||
let readRef (w, ref) = do
|
||||
cnt <- readIORef ref
|
||||
return (w, cnt)
|
||||
in mapM readRef $ M.toList freqtable
|
||||
print $ take maxw $ sortOn (Down . snd) counts
|
||||
13
Task/Word-frequency/Haskell/word-frequency-3.hs
Normal file
13
Task/Word-frequency/Haskell/word-frequency-3.hs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import qualified Data.Text.IO as T
|
||||
import qualified Data.Text as T
|
||||
|
||||
import Data.List (group, sort, sortBy)
|
||||
import Data.Ord (comparing)
|
||||
|
||||
frequentWords :: T.Text -> [(Int, T.Text)]
|
||||
frequentWords =
|
||||
sortBy (flip $ comparing fst) .
|
||||
fmap ((,) . length <*> head) . group . sort . T.words . T.toLower
|
||||
|
||||
main :: IO ()
|
||||
main = T.readFile "miserables.txt" >>= (mapM_ print . take 10 . frequentWords)
|
||||
Loading…
Add table
Add a link
Reference in a new issue