average_word_length computes the average number of letters per word. It supports authorship identification by turning word length into one feature of an author's writing style. section 11.3.1
The function processes text in a small sequence of steps:
- Split the text into candidate words. section 11.5.2
- Pass each candidate through clean_word so surrounding punctuation is removed. section 11.5.2
- Ignore empty results, including candidates containing no letters. section 11.5.2
- Add the remaining word lengths, count those words, and return total letters divided by word count. section 11.5.2
Worked example
For “A pearl! Pearl! Lustrous pearl! Rare. What a nice find.”, punctuation is not counted. The text contains 41 letters and 10 words, so average_word_length returns 41 / 10 = 4.1. section 11.3.1 section 11.5.2
Listing 11.3 Average word length
def average_word_length(text):
'''
text is a string of text.
Return the average word length of the words in text.
Do not count empty words as words.
Do not include surrounding punctuation.
>>> average_word_length('A pearl! Pearl! Lustrous pearl! \
Rare. What a nice find.')
4.1
'''
words = text.split() #1
total = 0 #2
count = 0 #3
for word in words: #4
word = clean_word(word) #5
if word != '': #6
total += len(word) #7
count += 1 #8
return total / count #9
The listing shows the function's inputs, counters, cleaning step, empty-word check, accumulation, and final division. section 11.5.2
