Python/strings/word_occurrence.py
Rohan R Bharadwaj e95ecfaf27
Add missing type annotations for strings directory (#5817)
* Type annotations for `strings/autocomplete_using_trie.py`

* Update autocomplete_using_trie.py

* Update detecting_english_programmatically.py

* Update detecting_english_programmatically.py

* Update frequency_finder.py

* Update frequency_finder.py

* Update frequency_finder.py

* Update word_occurrence.py

* Update frequency_finder.py

* Update z_function.py

* Update z_function.py

* Update frequency_finder.py
2022-05-13 13:55:53 +08:00

28 lines
897 B
Python

# Created by sarathkaul on 17/11/19
# Modified by Arkadip Bhattacharya(@darkmatter18) on 20/04/2020
from collections import defaultdict
from typing import DefaultDict
def word_occurence(sentence: str) -> dict:
"""
>>> from collections import Counter
>>> SENTENCE = "a b A b c b d b d e f e g e h e i e j e 0"
>>> occurence_dict = word_occurence(SENTENCE)
>>> all(occurence_dict[word] == count for word, count
... in Counter(SENTENCE.split()).items())
True
>>> dict(word_occurence("Two spaces"))
{'Two': 1, 'spaces': 1}
"""
occurrence: DefaultDict[str, int] = defaultdict(int)
# Creating a dictionary containing count of each word
for word in sentence.split():
occurrence[word] += 1
return occurrence
if __name__ == "__main__":
for word, count in word_occurence("INPUT STRING").items():
print(f"{word}: {count}")