24 lines
554 B
Text
24 lines
554 B
Text
def __contains__(container, item)
|
|
if container.is_a?(Hash)
|
|
container.key?(item)
|
|
else
|
|
container.include?(item)
|
|
end
|
|
end
|
|
def count_words(text)
|
|
words = text.downcase().split()
|
|
word_count = {}
|
|
for word in words
|
|
word = word.delete('.,!?')
|
|
if __contains__(word_count, word)
|
|
word_count[word] = word_count[word] + 1
|
|
else
|
|
word_count[word] = 1
|
|
end
|
|
end
|
|
return word_count
|
|
end
|
|
text = "Hello world hello there world hello"
|
|
counts = count_words(text)
|
|
print("Text: #{text}", "\n")
|
|
print("Word counts: #{counts}", "\n")
|