Seja o texto abaixo:
This is the most favourable period for travelling in Russia. They fly
quickly over the snow in their sledges; the motion is pleasant, and,
in my opinion, far more agreeable than that of an English stagecoach.
The cold is not excessive, if you are wrapped in furs--a dress which
I have already adopted, for there is a great difference between walking
the deck and remaining seated motionless for hours, when no exercise
prevents the blood from actually freezing in your veins. I have no
ambition to lose my life on the post-road between St. Petersburgh
and Archangel. I shall depart for the latter town in a fortnight
or three weeks; and my intention is to hire a ship there, which can
easily be done by paying the insurance for the owner, and to engage
as many sailors as I think necessary among those who are accustomed
to the whale-fishing. I do not intend to sail until the month of June;
and when shall I return? Ah, dear sister, how can I answer this question?
If I succeed, many, many months, perhaps years, will pass before you
and I may meet. If I fail, you will see me again soon, or never.
Farewell, my dear, excellent Margaret. Heaven shower down blessings
on you, and save me, that I may again and again testify my gratitude
for all your love and kindness.
Gostaria de criar uma lista de palavras a partir do texto e exibir a frequência da ocorrência de cada uma. Achei na internet o código abaixo que funciona mas não entendi:
def PegaPalavras(texto):
return ''.join((c if c.isalnum() else ' ') for c in texto).split()
Tentei reescrevê-lo de forma mais simples mas não consegui:
def PegaPalavras(texto):
palavras = []
for c in texto:
if c.isalnum():
c=c
palavras.append("".join(c).split())
else:
c =" "
palavras.append("".join(c).split())
return palavras
Como reescrever o primeiro código de forma mais explícita (sem ser em apenas uma linha), de forma a facilitar o seu entedimento? Alguma outra solução?