MCPcopy
hub / github.com/TheAlgorithms/Python / word_occurrence

Function word_occurrence

strings/word_occurrence.py:6–21  ·  view source on GitHub ↗

>>> 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_occurrence(SENTENCE) >>> all(occurence_dict[word] == count for word, count ... in Counter(SENTENCE.split()).items()) True >>> dict(word_occurrence

(sentence: str)

Source from the content-addressed store, hash-verified

4
5
6def word_occurrence(sentence: str) -> dict:
7 """
8 >>> from collections import Counter
9 >>> SENTENCE = "a b A b c b d b d e f e g e h e i e j e 0"
10 >>> occurence_dict = word_occurrence(SENTENCE)
11 >>> all(occurence_dict[word] == count for word, count
12 ... in Counter(SENTENCE.split()).items())
13 True
14 >>> dict(word_occurrence("Two spaces"))
15 {'Two': 1, 'spaces': 1}
16 """
17 occurrence: defaultdict[str, int] = defaultdict(int)
18 # Creating a dictionary containing count of each word
19 for word in sentence.split():
20 occurrence[word] += 1
21 return occurrence
22
23
24if __name__ == "__main__":

Callers 1

word_occurrence.pyFile · 0.85

Calls 1

splitMethod · 0.80

Tested by

no test coverage detected