Как из текста получить словарь частоты нахождения слов?
import collections
text = "These people had a little window at the back of their house from which " \
"splendid garden could be seen, which was full of the most beautiful " \
"flowers and herbs. It was, however, surrounded by a high wall, and no " \
"one dared to go into it because it belonged to an enchantress, who had " \
"great power and was dreaded by all the world."
Ответы (2 шт):
Автор решения: Victor VosMottor
→ Ссылка
text = "These people had a little window at the back of their house from which a splendid garden could be seen, which was full of the most beautiful flowers and herbs. It was, however, surrounded by a high wall, and no one dared to go into it because it belonged to an enchantress, who had great power and was dreaded by all the world."
l = text.split(' ')
print(*sorted(l, key=l.count))
Автор решения: MaxU
→ Ссылка
from string import punctuation
from collections import Counter
res = dict(Counter(text
.translate(str.maketrans('', '', punctuation))
.split())
.most_common())
In [71]: res
Out[71]:
{'a': 3,
'the': 3,
'was': 3,
'and': 3,
'had': 2,
'of': 2,
'which': 2,
'by': 2,
'to': 2,
'it': 2,
'These': 1,
'people': 1,
'little': 1,
'window': 1,
'at': 1,
'back': 1,
'their': 1,
'house': 1,
'from': 1,
'splendid': 1,
'garden': 1,
'could': 1,
'be': 1,
'seen': 1,
'full': 1,
'most': 1,
'beautiful': 1,
'flowers': 1,
'herbs': 1,
'It': 1,
'however': 1,
'surrounded': 1,
'high': 1,
'wall': 1,
'no': 1,
'one': 1,
'dared': 1,
'go': 1,
'into': 1,
'because': 1,
'belonged': 1,
'an': 1,
'enchantress': 1,
'who': 1,
'great': 1,
'power': 1,
'dreaded': 1,
'all': 1,
'world': 1}