lylyly
lylyly

Reputation: 93

Haskell: how to get the common words

I am trying to do drop common words function, but I do not know how to get or find the common word list. Do I need to create a common words list? Thank you

The question :

Takes a list of strings and drops any word that is within the top 20  
most commonly used in English. Returns a list of strings without those words.

the result like :

dropCommonWords ["the","planet","of","the","apes"]
["planet","apes"]

here my dropletters code

dropletters xs = filter (\x -> x `elem` ['a'..'z'] ) xs

Upvotes: 0

Views: 534

Answers (2)

RoadRunner
RoadRunner

Reputation: 26315

You can also use basic recursion for this:

import Data.Char

dropCommonWords :: [String] -> [String]
dropCommonWords [] = []
dropCommonWords (x:xs)
    | map toLower x `notElem` commonWords = x : dropCommonWords xs
    | otherwise = dropCommonWords xs
    where commonWords = ["the", "be", "to", "of", "and", "a", "in", "that", "have", "I", "it", "for", "not", "on", "with", "he", "as", "you", "do", "at"]

Which also converts every word to lowercase before hand with map toLower x, since you could get strings such as "THE", and that counts as a common word, just a different case.

Here is the behavior of the above code:

*Main> dropCommonWords ["the","planet","of","the","apes"]
["planet","apes"]
*Main> dropCommonWords ["THE","planet","of","the","apes"]
["planet","apes"]

Note: Using filter is better here, I didn't post it since the above answer already mentioned it.

Upvotes: 0

mdatsev
mdatsev

Reputation: 3879

You will need a list of the common words and then you filter those which are not element of that list:

dropCommonWords xs = filter (\x -> x `notElem` common ) xs
  where common = ["the", "be", "to", "of", "and", "a", "in", "that", "have", "I", "it", "for", "not", "on", "with", "he", "as", "you", "do", "at"]

Result:

Prelude> dropCommonWords ["the","planet","of","the","apes"]
["planet","apes"]

Upvotes: 2

Related Questions