Remove characters from String in Haskell -
i creating program reads text file , splits words , stores them in list. have been trying create function takes in string whole text string file , remove punctuation e.g. ";", ",", "." unfortunately haven't had luck yet. program works without punctuation function, not when include (towords filecontents)
please can @ have done , see doing wrong.
here code have far:
main = contents <- readfile "largetextfile.txt" let lowcontents = map tolower contents let outstr = countwords (lowcontents) let finalstr = sortoccurrences (outstr) let reversedstr = reverse finalstr putstrln "word | occurrence " mapm_ (printlist) reversedstr -- counts words. countwords :: string -> [(string, int)] countwords filecontents = countoccurrences (towords (removepunc filecontents)) -- splits words , removes linking words. towords :: string -> [string] towords s = filter (\w -> w `notelem` ["an","the","for"]) (words s) -- remove punctuation text string. removepunc :: string -> string removepunc xs = x | x <- xs, not (x `elem` ",.?!-:;\"\'") -- counts, how each string in given list appears. countoccurrences :: [string] -> [(string, int)] countoccurrences xs = map (\xs -> (head xs, length xs)) . group . sort $ xs -- sort list in order of occurrences. sortoccurrences :: [(string, int)] -> [(string, int)] sortoccurrences sort = sortby (comparing snd) sort -- prints list in format. printlist = putstrln((fst a) ++ " | " ++ (show $ snd a))
you want:
removepunc xs = [ x | x <- xs, not (x `elem` ",.?!-:;\"\'") ]
with brackets.
Comments
Post a Comment