Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Reader
- ( -- * Functions
- readReviews
- , countWords
- ) where
- import System.Environment
- import System.IO
- import System.IO.Error
- import System.Directory
- import Control.DeepSeq
- import Data.List
- import Data.List.Split
- import Data.Char
- import Types
- main = do
- (pos,neg) <- readReviews "../../resources/dvd/"
- let keys = ["batman"]
- let x = map (countWords keys) pos
- mapM_ print ( zipWith (++) (map show [1..]) (map show x))
- readReviews :: String -> IO RawReview
- readReviews path = do
- pos <- readDir (path ++ "/pos/")
- neg <- readDir (path ++ "/neg/")
- return (pos,neg)
- readDir :: String -> IO [String]
- readDir p = do
- files <- getDirectoryContents p
- let pathAndFiles = map (p ++) . drop 2 $ files
- contents <- mapM (readFile' latin1) pathAndFiles
- return contents
- readFile' :: TextEncoding -> FilePath -> IO String
- readFile' enc path = do
- h <- openFile path ReadMode
- hSetEncoding h enc
- s <- hGetContents h
- deepseq s (hClose h)
- return s
- countWords :: [String] -> String -> [Double]
- countWords ws s = [ genericLength (filter (==w) (filterSuffix (words (filterNonChar s)) )) | w <- ws ]
- {-countWords ws s = [ genericLength (filter (==w) (words (filterNonChar s) )) | w <- ws ]-}
- filterSuffix :: String -> String
- filterSuffix s = do
- head (split (oneOf delimeter) s)
- where "'\\"
- filterNonChar :: String -> String
- filterNonChar s = do
- filter (`notElem` nonChar ) (map toLower s)
- where nonChar = ",.;"
Advertisement
Add Comment
Please, Sign In to add comment