The exactly-once word ratio counts words that appear one time and divides that count by the total number of words. It serves as one of five numerical features in an authorship signature. section 11.3.1
A sample calculation
In “A pearl! Pearl! Lustrous pearl! Rare. What a nice find.”, five words occur exactly once: lustrous, rare, what, nice, and find. With ten total words, the ratio is 5/10 = 0.5. section 11.3.1
A set of all words is not enough: a word seen three times could be removed on its second occurrence and mistakenly added again on its third. Track words seen at least once in one set and words still seen exactly once in another. Add a word to both sets initially, then discard it permanently from the once set when it appears again. 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
The corrected Python implementation uses unique and once sets, ignores empty words, excludes surrounding punctuation, and verifies the sample result with doctest. section 11.5.4
