use trigrams, works much better
inspired by soloution from previous year :P
This commit is contained in:
@@ -1,4 +1,36 @@
|
|||||||
from random import randint as rand
|
from random import randint as rand
|
||||||
|
from math import log10
|
||||||
|
|
||||||
|
class Scoring:
|
||||||
|
floor = 0
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.trigrams = {}
|
||||||
|
# read trigrams from file
|
||||||
|
with open(os.path.dirname(os.path.realpath(__file__)) + '/english_trigrams.txt', 'r') as trigram_file:
|
||||||
|
for line in trigram_file:
|
||||||
|
trigram, score = line.split(' ')
|
||||||
|
self.trigrams[trigram.lower()] = int(score)
|
||||||
|
|
||||||
|
score_sum = sum(self.trigrams.values())
|
||||||
|
self.floor = log10(0.1 / score_sum)
|
||||||
|
|
||||||
|
for i in list(self.trigrams.keys()):
|
||||||
|
self.trigrams[i] = log10(float(self.trigrams[i]) / score_sum)
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_score(self, text):
|
||||||
|
score = 0
|
||||||
|
|
||||||
|
for k in range(len(text) - 3):
|
||||||
|
trigram = text[k:k + 3]
|
||||||
|
if trigram in self.trigrams:
|
||||||
|
score += self.trigrams[trigram]
|
||||||
|
else:
|
||||||
|
score += self.floor
|
||||||
|
return score
|
||||||
|
|
||||||
|
|
||||||
def randomize_key(key):
|
def randomize_key(key):
|
||||||
a_index = rand(0, 25)
|
a_index = rand(0, 25)
|
||||||
b_index = rand(0, 25)
|
b_index = rand(0, 25)
|
||||||
@@ -34,17 +66,21 @@ if __name__ == "__main__":
|
|||||||
import re
|
import re
|
||||||
import mono
|
import mono
|
||||||
|
|
||||||
best_score = 0
|
best_score = -10000
|
||||||
best_key = key
|
best_key = key
|
||||||
|
|
||||||
|
scoring = Scoring()
|
||||||
|
|
||||||
trys = 0
|
trys = 0
|
||||||
while trys < 1000:
|
while trys < 20000:
|
||||||
score = 0
|
score = 0
|
||||||
tmp = randomize_key(key)
|
tmp = randomize_key(key)
|
||||||
plain = mono.mono_decrypt(t, tmp)
|
plain = mono.mono_decrypt(t, tmp)
|
||||||
for word in words:
|
#for word in words:
|
||||||
if re.search(word, plain):
|
# search_res = re.search(word, plain, re.IGNORECASE)
|
||||||
score += 1
|
# if search_res:
|
||||||
|
# score += len(word)
|
||||||
|
score = scoring.get_score(plain)
|
||||||
if score > best_score:
|
if score > best_score:
|
||||||
trys = 0
|
trys = 0
|
||||||
best_score = score
|
best_score = score
|
||||||
|
|||||||
17556
src/mono/english_trigrams.txt
Normal file
17556
src/mono/english_trigrams.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user