liveBook · Manning

Exactly-once word ratio

As explained in Learn AI-Assisted Python Programming, Second Edition

A text statistic equal to the count of words occurring exactly once divided by the total count of words.

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

How Learn AI-Assisted Python Programming, Second Edition covers it

How deep it goes
Defines the ratio, works through a sample, and debugs its set-based implementation without descending into deeper internals.
Examples
Uses a ten-word sample text, a 0.5 expected result, and competing set-based implementations.
What you can do after
Apply it via a Python function that cleans words, tracks exact single occurrences, divides by total words, and tests the result.
How it connects
Connects authorship identification with signatures, make_signature, clean_word, sets, and doctest.

This explainer was generated by Manning's knowledge graph from the text of Learn AI-Assisted Python Programming, Second Edition. The section links point to the parts of the book it draws on.