-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoring_based_iterative_word_replacement_solver.py
65 lines (48 loc) · 2.05 KB
/
scoring_based_iterative_word_replacement_solver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import pandas as pd
import string
import random
import pickle
import numpy as np
from crypt_utils import *
with open("ngram_data/word_dict.pickle","rb") as f:
word_dict = pickle.load(f)
with open("ngram_data/word_prob_dict.pickle","rb") as f:
word_prob_dict = pickle.load(f)
word_freq=pd.read_csv("ngram_data/word_freq.csv").set_index("word")["frequency"].to_dict()
bigram=pd.read_csv("ngram_data/2gram.csv").set_index("2-gram")["frequency"].to_dict()
trigram=pd.read_csv("ngram_data/3gram.csv").set_index("3-gram")["frequency"].to_dict()
with open("ngram_data/5gram_probs.pickle", "rb") as f:
fivegramprobs = pickle.load(f)
actual_key = "badcfehgjilkonmrqputsxwvzy"
# plaintext = "key quick brown jumps over lazy dog the fox"
plaintext = "sentence that definitely has a unique solution"
# plaintext = "world science technology"
cipher = encrypt(plaintext,actual_key)
def solver(cipher, word_dict, ngram_probs):
key = string.ascii_lowercase
word_seq = cipher.split(" ")
solutions = []
partial_solutions=[]
que = []
que.append((0,{}))
while len(que)!=0:
index, assignment = que.pop()
if index >= len(word_seq):
cor_assign = assignment_trans(assignment)
sol = cipher.translate(cor_assign)
solutions.append((sol, get_score(sol,ngram_probs, word_freq)))
else:
values = word_dict[paternify(word_seq[index])]
was_assign = False
for sub_word in values:
if isconsistent(assignment,word_seq[index],sub_word):
cassignment = dict()
cassignment.update(assignment)
cassignment.update(get_assignment(word_seq[index],sub_word))
que.append((index+1,cassignment))
was_assign=True
if was_assign==False:
cor_assign = assignment_trans(assignment)
partial_solutions.append(cipher.translate(cor_assign))
return (solutions,partial_solutions)
print(solver(cipher,word_dict,fivegramprobs))