lowest_score chooses the known signature closest to an unknown signature. It receives a dictionary of known signatures, the unknown signature, and five weights; it compares each candidate and returns the key with the smallest score. section 11.3.1 section 11.5.13
The function follows a simple input–process–output pattern:
- Input: author names mapped to known signatures, one unknown signature, and a list of weights. section 11.5.13
- Process: loop through each known signature, call get_score, and keep the key whose score is lowest. section 11.3.1 section 11.5.13
- Output: return the key associated with the closest known signature. section 11.5.13
Choosing Dan
Given {'Dan': [1, 1, 1, 1, 1]} and {'Leo': [3, 3, 3, 3, 3]}, the unknown signature [1, 0.8, 0.9, 1.3, 1.4], and weights [11, 33, 50, 0.4, 4], lowest_score returns 'Dan'. section 11.5.13
Listing 11.16 Closest known signature
def lowest_score(signatures_dict, unknown_signature, weights):
'''
signatures_dict is a dictionary mapping keys to signatures.
unknown_signature is a signature.
weights is a list of five weights.
Return the key whose signature value has the lowest
score with unknown_signature.
>>> d = {'Dan': [1, 1, 1, 1, 1],\ #1
'Leo': [3, 3, 3, 3, 3]} #1
>>> unknown = [1, 0.8, 0.9, 1.3, 1.4]
>>> weights = [11, 33, 50, 0.4, 4]
>>> lowest_score(d, unknown, weights) #2
'Dan'
'''
lowest = None
for key in signatures_dict: #3
score = get_score(signatures_dict[key], #4
unknown_signature, weights)
if lowest is None or score < lowest[1]: #5
lowest = (key, score) #6
return lowest[0] #7
Inspect the complete Python listing, including the minimum-score tracking logic and its doctest example. section 11.5.13
