Hi everybody,

I'm new at Python and this forum has been a great help so far, so I hope someone is willing to help me on the following problem.
It can count the number of sentences and words in a text, but after I have split up a text into sentences, I do not know how to count the words in each sentence separately so I can calculate the standard deviation.

Grts

You can use a regex for words

import re
word_re = re.compile(r"[A-Za-z]+")

def count_words(sentence):
    return word_re.subn('', sentence)[1]

print(count_words("Give me bacon and eggs, said the other man."))

Thanks for your help! I have been able to calculate the average word and sentence length and their standard deviation, but now I am stuck on calculating the sd of the type-token ratio :-s

Grts

This question has already been answered. Start a new discussion instead.