Count Word Occurrences in String
Counts the occurrences of each word in a given string.
- string
- occurrences
- word-count
Code
def count_word_occurrences(text)
words = text.downcase.scan(/\w+/)
occurrences = Hash.new(0)
words.each { |word| occurrences[word] += 1 }
occurrences
endUsage
main :: IO ()
main = do
let text = "haskell is awesome and haskell is fun"
print $ countWordOccurrences text -- Output: [("and",1),("awesome",1),("fun",1),("haskell",2),("is",2)]