p.p1 function computes the score of the text

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4bd156; background-color: #000000}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #ffffff; background-color: #000000}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #ffffff; background-color: #000000; min-height: 13.0px}
p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; background-color: #000000; min-height: 14.0px}
p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #de38a5; background-color: #000000}
p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #8b87ff; background-color: #000000}
p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #ff4647; background-color: #000000}
span.s1 {color: #de38a5}
span.s2 {color: #8b87ff}
span.s3 {color: #ffffff}

#allows scoring of text using n-gram probabilities
from math import log10
class ngram_score(object):
    def __init__(self,ngramfile,sep=’ ‘):
        #load a file containing ngrams and counts and calculate log probabilities
        self.ngrams = {}
        for line in open(ngramfile):
            key,count = line.split(sep)
            self.ngramskey = int(count)
        self.L = len(key)
        self.N = sum(self.ngrams.values())
        #calculate log probabilities
        for key in self.ngrams.keys():
            self.ngramskey = log10(float(self.ngramskey)/self.N)
        self.floor = log10(0.01/self.N)
    
    #this function computes the score of the text
    def score(self,text):
        score = 0
        ngrams = self.ngrams.__getitem__
        for i in range(len(text)-self.L+1):
            if (texti:i+self.L in self.ngrams):
                score += ngrams(texti:i+self.L)
            else:
                score += self.floor
    return score

We Will Write a Custom Essay Specifically
For You For Only $13.90/page!


order now

#importing relevant modules to run the code
from pycipher import SimpleSubstitution as SimpleSub
import random
import re

#loads our quadgram statistics
fitness = ngram_score(‘english_quadgrams.txt’)

text= input(“Enter text to be deciphered : “)
#deals with bad user input by converting all of the input to upper case
text = re.sub(‘^A-Z’,”,text.upper())

def break_cipher(text):
    maxkey = list(‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’)
    maxscore = -99e9
    parentscore,parentkey = maxscore,maxkey:
    print (“Substitution Cipher solver, you may have to wait several iterations”)
    print (“for the correct result. Press ctrl+c to exit program.”)
    #keep going until we are killed by the user
    i = 0
    while (1):
        i = i+1
        #using in-built functions from pycipher modules
        random.shuffle(parentkey)
        deciphered = SimpleSub(parentkey).decipher(text)
        parentscore = fitness.score(deciphered)
        count = 0
        while (count parentscore):
                parentscore = score
                parentkey = child:
                count = 0
            count = count+1
        #keep track of best score seen so far
        if (parentscore>maxscore):
            maxscore,maxkey = parentscore,parentkey:
            ss = SimpleSub(maxkey)
            print (‘    best key: ‘+”.join(maxkey))
            print (‘    plaintext: ‘+ss.decipher(text))

break_cipher(text)

x

Hi!
I'm Barry!

Would you like to get a custom essay? How about receiving a customized one?

Check it out