-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathac6_solver.py
127 lines (108 loc) · 4.18 KB
/
ac6_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import pandas as pd
import string
import random
import pickle
import numpy as np
from crypt_utils import *
from collections import deque, defaultdict
import copy
with open("ngram_data/word_dictv2.pickle","rb") as f:
word_dict = pickle.load(f)
with open("ngram_data/word_prob_dictv2.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)
def ac6_solver(cipher, word_dict):
word_seq = cipher.split(" ")
domains = {}
supports = {}
queue = deque()
# Initialize domains and supports
for word in word_seq:
pattern = paternify(word)
if pattern in word_dict:
domains[word] = set(word_dict[pattern])
else:
return None # No possible substitutions for this word
# Initialize supports and queue
for xi in word_seq:
supports[xi] = {}
for a in domains[xi]:
supports[xi][a] = {}
for xj in word_seq:
if xi != xj:
supports[xi][a][xj] = set()
for b in domains[xj]:
if is_arc_consistent(xi, a, xj, b):
supports[xi][a][xj].add(b)
if not supports[xi][a][xj]:
# No support for (xi, a) with xj
queue.append((xi, a, xj))
# AC-6 Algorithm
while queue:
xi, a, xj = queue.popleft()
# Remove a from the domain of xi
domains[xi].discard(a)
if not domains[xi]:
return None # Domain wipeout
for xk in word_seq:
if xk != xi:
for c in domains[xk]:
if (xi not in supports[xk][c]) or (a in supports[xk][c][xi]):
supports[xk][c][xi].discard(a)
if not supports[xk][c][xi]:
queue.append((xk, c, xi))
# Prepare the output domain
wd_out = {}
for word in domains:
pattern = paternify(word)
if pattern in wd_out:
wd_out[pattern].update(domains[word])
else:
wd_out[pattern] = domains[word]
return wd_out
def is_arc_consistent(xi, a, xj, b):
# Check if assignment a for xi is consistent with assignment b for xj
return isconsistent({}, xi, a) and isconsistent({}, xj, b) and check_consistency(a, b, gen_ordered_dict(xi, xj))
def solver(cipher, word_dict, ngram_probs):
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)))
print(sol)
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))
if len(solutions) == 0:
return -float("inf")
else:
return max([i[1] for i in solutions])
if __name__ == "__main__":
actual_key = "badcfehgjilkonmrqputsxwvzy"
plaintext = "sentence that definitely has a unique solution"
cipher = encrypt(plaintext, actual_key)
res_domain = ac6_solver(cipher, word_dict)
if res_domain is not None:
print(solver(cipher, res_domain, fivegramprobs))
else:
print(-float("inf"))