-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.hs
38 lines (26 loc) · 1.18 KB
/
filter.hs
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
import Data.Char
import Data.List
removePunctuation' :: [Char] -> [Char]
removePunctuation' [] = []
removePunctuation' (x:xs) | not (isPunctuation x) = x : removePunctuation' xs
| otherwise = removePunctuation' xs
-- main = print (removePunctuation "g%r,.;[ea]t")
removeEmpty' :: [[a]] -> [[a]]
removeEmpty' [] = []
removeEmpty' (xs:xss) | not (null xs) = xs : removeEmpty' xss
| otherwise = removeEmpty' xss
-- main = print (removeEmpty [[1, 2], [], [6], []])
filter' :: (a -> Bool) -> [a] -> [a]
filter' _ [] = []
filter' p (x:xs) | p x = x : filter' p xs
| otherwise = filter' p xs
removePunctuation = filter (not . isPunctuation)
-- removeEmpty = filter (\x -> length x > 0)
wordFrequencies :: String -> [(String, Int)]
wordFrequencies s =
let cleaned = (filter (\x -> isLetter x || x == ' ') . map toLower) s
groupedWords = (group . sort . words) cleaned
in map (\xs -> (head xs, length xs)) groupedWords
-- main = print (wordFrequencies "It was the best of times, it was the worst of times,")
frequentWordsOnly :: [(String, Int)] -> [String]
frequentWordsOnly = map fst . filter (\x -> snd x >= 5)