Sentence complexity measures how many punctuation-delimited phrases sentences contain on average. It helps describe an author’s writing style as part of a larger text signature. section 11.1 section 11.3.1
Calculate the measure with a simple rule of thumb:
- Treat commas, semicolons, and colons as boundaries separating phrases. section 11.3.1
- Count the phrases across the sentences in a text. section 11.3.1
- Divide the total phrase count by the total sentence count. section 11.3.1 section 11.5.9
A short calculation
Suppose two sentences contain three phrases altogether. The sentence complexity is 3/2, or 1.5. A sentence without commas, semicolons, or colons counts as one phrase. section 11.3.1
Listing 11.11 Average number of phrases per sentence
def average_sentence_complexity(text):
'''
text is a string of text.
Return the average number of phrases per sentence in text.
>>> average_sentence_complexity('A pearl! Pearl! Lustrous \
pearl! Rare. What a nice find.')
1.0
>>> average_sentence_complexity('A pearl! Pearl! Lustrous \
pearl! Rare, what a nice find.')
1.25 #1
'''
sentences = get_sentences(text) #2
total = 0
for sentence in sentences: #3
phrases = get_phrases(sentence) #4
total += len(phrases) #5
return total / len(sentences) #6
The implementation separates sentence detection, phrase detection, counting, and averaging, making each step easier to inspect and test. section 11.5.9
