306 http://inventwithpython.com/hacking
Email questions to the author: [email protected]
third, put each list of letters in reverse "ETAOIN" order, and then
convert it to a string
- for freq in freqToLetter:
- freqToLetter[freq].sort(key=ETAOIN.find, reverse=True)
- freqToLetter[freq] = ''.join(freqToLetter[freq])
fourth, convert the freqToLetter dictionary to a list of tuple
pairs (key, value), then sort them
- freqPairs = list(freqToLetter.items())
- freqPairs.sort(key=getItemAtIndexZero, reverse=True)
fifth, now that the letters are ordered by frequency, extract all
the letters for the final string
- freqOrder = []
- for freqPair in freqPairs:
- freqOrder.append(freqPair[1])
- return ''.join(freqOrder)
- def englishFreqMatchScore(message):
Return the number of matches that the string in the message
parameter has when its letter frequency is compared to English
letter frequency. A "match" is how many of its six most frequent
and six least frequent letters is among the six most frequent and
six least frequent letters for English.
- freqOrder = getFrequencyOrder(message)
- matchScore = 0
Find how many matches for the six most common letters there are.
- for commonLetter in ETAOIN[:6]:
- if commonLetter in freqOrder[:6]:
- matchScore += 1
Find how many matches for the six least common letters there are.
- for uncommonLetter in ETAOIN[-6:]:
- if uncommonLetter in freqOrder[-6:]:
- matchScore += 1
- return matchScore
How the Program Works..........................................................................................................................................
freqAnalysis.py
Frequency Finder
http://inventwithpython.com/hacking (BSD Licensed)