18 lines
No EOL
424 B
Text
18 lines
No EOL
424 B
Text
def count_words(text):
|
|
words = text.lower().split()
|
|
word_count = {}
|
|
|
|
for word in words:
|
|
word = word.strip('.,!?')
|
|
if word in word_count:
|
|
word_count[word] = word_count[word] + 1
|
|
else:
|
|
word_count[word] = 1
|
|
|
|
return word_count
|
|
|
|
text = "Hello world hello there world hello"
|
|
counts = count_words(text)
|
|
|
|
print(f"Text: {text}")
|
|
print(f"Word counts: {counts}") |