public
Created

Populating map from text data

  • Download Gist
HMTTest.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
module HMTTest where
 
--{ imports
import Control.Monad (forM_)
 
import qualified Data.HashMap.Strict as HM
 
import Data.Hashable (Hashable)
import Data.Hashable
 
import Data.Bits (shiftL)
 
import qualified Data.Text as T
import qualified Data.Text.Array as TA
import qualified Data.Text.Internal as T
import qualified Data.Text.IO as T
 
import System (getArgs)
--}
 
-- got from: http://hackage.haskell.org/packages/archive/hashable/1.1.1.0/doc/html/src/Data-Hashable.html
instance Hashable T.Text where
hash (T.Text arr off len) = hashByteArray (TA.aBA arr)
(off `shiftL` 1) (len `shiftL` 1)
 
hashWithSalt salt (T.Text arr off len) =
hashByteArrayWithSalt (TA.aBA arr) (off `shiftL` 1) (len `shiftL` 1)
salt
 
{-buildDict words = HM.fromListWith (+) words-}
 
{-getWords file = do contents <- T.readFile file-}
{-return (buildDict $-}
{-map (\w -> (w,1)) (T.lines contents))-}
 
getWords file = do contents <- T.readFile file
return (foldl (\ a e -> HM.insertWith (+) e 1 a) HM.empty (T.lines contents))
 
main = do args <- getArgs
words <- getWords (args !! 0)
case HM.lookup (T.pack (args !! 1)) words of
Nothing -> return ()
Just v -> print v
HTTTest.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
module HTTTest where
 
import Control.Monad (forM_)
import qualified Data.HashTable as HT
 
import Data.Hashable (Hashable)
import Data.Hashable
 
import Data.Bits (shiftL)
 
import qualified Data.Text as T
import qualified Data.Text.Array as TA
import qualified Data.Text.Internal as T
import qualified Data.Text.IO as T
 
import System (getArgs)
 
-- got from: http://hackage.haskell.org/packages/archive/hashable/1.1.1.0/doc/html/src/Data-Hashable.html
instance Hashable T.Text where
hash (T.Text arr off len) = hashByteArray (TA.aBA arr)
(off `shiftL` 1) (len `shiftL` 1)
 
hashWithSalt salt (T.Text arr off len) =
hashByteArrayWithSalt (TA.aBA arr) (off `shiftL` 1) (len `shiftL` 1)
salt
 
sizeHint = 1500000
 
getWords file = do contents <- T.readFile file
return (T.lines contents)
 
type HTTextInt = HT.HashTable T.Text Int
 
emptyHTTextInt :: () -> IO HTTextInt
emptyHTTextInt _ = do HT.new (==) (fromIntegral . hash) >>= return
 
main =
{-do ht <- HT.newHint (==) (fromIntegral . hashByteString) sizeHint-}
do args <- getArgs
words <- getWords (args !! 0)
ht <- emptyHTTextInt ()
forM_ words $ \word ->
HT.insert ht word 1
maybeVal <- HT.lookup ht (T.pack $ args !! 1)
case maybeVal of
Nothing -> print "nothing"
Just v -> print v

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.