lorcs1

readR

Apr 5th, 2013
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Reader
  2.        ( -- * Functions
  3.          readReviews
  4.        , countWords
  5.        ) where
  6.  
  7.  
  8. import System.Environment  
  9. import System.IO  
  10. import System.IO.Error  
  11. import System.Directory
  12. import Control.DeepSeq
  13. import Data.List  
  14. import Data.List.Split
  15. import Data.Char
  16. import Types
  17.  
  18. main = do  
  19.     (pos,neg) <- readReviews "../../resources/dvd/"
  20.     let keys = ["batman"]
  21.     let x = map (countWords keys) pos
  22.     mapM_ print  ( zipWith (++) (map show [1..]) (map show x))
  23.  
  24. readReviews :: String -> IO RawReview
  25. readReviews path = do
  26.     pos <- readDir (path ++ "/pos/")
  27.     neg <- readDir (path ++ "/neg/")
  28.     return (pos,neg)
  29.  
  30.  
  31. readDir :: String -> IO [String]
  32. readDir p = do
  33.     files <- getDirectoryContents p
  34.     let pathAndFiles = map (p ++) . drop 2 $ files
  35.     contents <- mapM (readFile' latin1) pathAndFiles
  36.    return contents
  37.  
  38.  
  39. readFile' :: TextEncoding -> FilePath -> IO String
  40. readFile' enc path = do
  41.    h <- openFile path ReadMode
  42.    hSetEncoding h enc
  43.    s <- hGetContents h
  44.    deepseq s (hClose h)
  45.    return s
  46.    
  47.  
  48. countWords :: [String] -> String -> [Double]
  49. countWords ws s = [ genericLength (filter (==w) (filterSuffix (words (filterNonChar s)) )) | w <- ws ]
  50. {-countWords ws s = [ genericLength (filter (==w) (words (filterNonChar s) )) | w <- ws ]-}
  51.  
  52. filterSuffix :: String -> String
  53. filterSuffix s = do
  54.    head (split (oneOf delimeter) s)
  55.    where "'\\"
  56.  
  57. filterNonChar :: String -> String
  58. filterNonChar s = do
  59.    filter (`notElem` nonChar ) (map toLower s)
  60.    where nonChar = ",.;"
Advertisement
Add Comment
Please, Sign In to add comment