liveBook · Manning

Make_signature

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

make_signature is a book-specific Python function that computes five numerical authorship features from text and returns them as a list.

make_signature takes a text string and returns a five-number signature for authorship identification. It coordinates smaller feature functions instead of mixing all calculations into one large procedure. section 11.3 section 11.5.10

The signature contains these features:

A numeric signature

Calling make_signature on “A pearl! Pearl! Lustrous pearl! Rare, what a nice find.” returns [4.1, 0.7, 0.5, 2.5, 1.25]. The function delegates each value to its corresponding feature function. section 11.5.10

Listing 11.12 Numeric signature for the text
def make_signature(text):
    '''
    The signature for text is a list of five elements:
    average word length, different words divided by total words, 
    words used exactly once divided by total words,
    average sentence length, and average sentence complexity.
    
    Return the signature for text.    
    
    >>> make_signature('A pearl! Pearl! Lustrous pearl! \
Rare, what a nice find.')
    [4.1, 0.7, 0.5, 2.5, 1.25]
    '''
    return [average_word_length(text),            #1
            different_to_total(text),             #1
            exactly_once_to_total(text),          #1
            average_sentence_length(text),        #1
            average_sentence_complexity(text)]    #1

Inspect the complete function definition and its doctest example. section 11.5.10

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

How deep it goes
Defines the five returned features and orchestration, while delegating individual calculations to helper functions.
Examples
Uses pearl-text doctests and a phrase-counting example to demonstrate complete signatures and sentence complexity.
What it assumes
Assumes helper functions for word, sentence, phrase, and feature calculations, while the surrounding design decomposes these tasks explicitly.
What you can do after
Build it from scratch by composing five feature functions into a readable numeric-signature function.
How it connects
Connects top-down design, feature functions, sentence complexity, known signatures, and authorship identification.

Related concepts

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.