exactly_once_to_total calculates the proportion of cleaned words appearing exactly once in a text. It divides the count of such words by the total word count. section 11.3.1 section 11.5.4
The function processes each word in three stages:
- Splits the text into words, uses clean_word to remove surrounding punctuation and normalize case, and ignores empty results. section 11.3.1 section 11.5.4
- Tracks every word in unique and tracks words seen exactly once in once; repeated words are discarded from once permanently. section 11.5.4
- Returns len(once) divided by the total number of nonempty words. section 11.5.4
Why the first approach fails
A set that alternately adds and removes repeated words misclassifies a word appearing three times: the third occurrence adds it back. The corrected approach removes repeated words from once without re-adding them. section 11.5.4
Listing 11.6 Fraction of words used just once: Try 2
def exactly_once_to_total(text):
'''
text is a string of text.
Return the number of words that show up exactly once in text
divided by the total number of words in text.
Do not count empty words as words.
Do not include surrounding punctuation.
>>> exactly_once_to_total('A pearl! Pearl! Lustrous pearl! \
Rare. What a nice find.')
0.5
'''
words = text.split()
total = 0
unique = set() #1
once = set() #2
for word in words:
word = clean_word(word)
if word != '':
total += 1
if word in unique: #3
once.discard(word) #4
else:
unique.add(word) #5
once.add(word) #6
return len(once) / total #7
Inspect the corrected Python implementation, including its doctest, word-cleaning flow, two-set strategy, and final division. section 11.5.4
