import BinarySearchTree wordlistfile = open("wordlist.txt") print "File open...\n" words = wordlistfile.read() wordlistfile.close() print "Words read in...\n" wordlist = words.split("\n") print "Words split up...\n" wordlist.sort() tree = BinarySearchTree.BinarySearchTree() tree.addSortedList(wordlist) print "BinarySearchTree constructed.\n" criptofile = open("plaintexts.txt") plaintext = criptofile.read() criptofile.close() plaintextlist = plaintext.split("\n") globals()["count"] = 0 def countWords(string): if not string: return 0 for i in range(len(string)): for j in range(i+1,len(string)+1): #globals()["count"] += 1 if (tree.inside(string[i:j])): print "Found word: ",string[i:j] return 1+countWords(string[j:]) if (not tree.startswith(string[i:j])): #globals()["count"] += 1 break return 0 def sigProb(string): if not string: return 0 for i in range(len(string)): for j in range(i+1,len(string)+1): #globals()["count"] += 1 #print "looking at:",string[i:j] if (tree.inside(string[i:j])): #print "starting on",string[i:j] while (tree.startswith(string[i:j]) and j < len(string)+1): j += 1 #print "ok, I'm up to",string[i:j] while (not tree.inside(string[i:j])): j -= 1 if (not tree.inside(string[i:j])): print "!!!!!!!!!!!!!!damn, it doesn't work!!!!!!!!!!!" print "Found word: ",string[i:j],"with value,",pow(4,j-i-1) return (pow(4,j-i-1)+sigProb(string[j:])) if (not tree.startswith(string[i:j])): #globals()["count"] += 1 break return 0 class Rankable: def __init__(self, string, value): self.item = string self.score = value def __cmp__(self, other): return cmp(self.score, other.score) def __eq__(self, other): return self.item == other.item def __str__(self): return str(self.it268435456em) def __getitem__(self, y): return self.item.__getitem__(y) list = [] i = 0 count = 0 stats = {} for item in plaintextlist: i += 1 print "Ok, gonna try",item[0:6] list.insert(0,Rankable(item, sigProb(item))) #count += int(globals()["count"]) #if (not stats[list[0].score]): stats[list[0].score] = [int(globals()["count"])] #else: stats[list[0].score].insert([int(globals()["count"])]) print item[0:6],"done in",globals()["count"],"comparisons." print #globals()["count"] = 0 #print count,"comparisons total." #for key in stats: # if (stats[key]): # i = 0 # for item in stats[key]: # i += item # avg = i / len(stats[key]) # print key,"words in a string gives an average of",avg,"comparisons." list.sort() list.reverse() for item in list: print item[:10],"has a score of ",str(item.score) #print "Done in",globals()["count"],"comparisons."