-
Notifications
You must be signed in to change notification settings - Fork 0
/
word.py
99 lines (77 loc) · 3.33 KB
/
word.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
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 29 21:37:48 2017
@author: pg
AlphaSubDecrypt
Copyright (C) 2017 [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import re;
class Word:
index = 0;
length = 0;
encrypted = "";
possibilities = set();
inside = {};
outside = [];
def __init__(self, length):
self.length = length;
self.inside = {};
def updatePossibilitiesWithAlphabet(self, alpha):
# remove the possibilities that aren't actually possible with the curent dictionary of substitution
# alpha is the dictionary of substitution
for cypherChar, charPossibilities in alpha.items():
index = self.encrypted.find(cypherChar);
if index != -1 and charPossibilities != set(): # if the cypher char is used in the cypher word, reduce the number of loop
loopset = self.possibilities.copy();
for wordPossibility in loopset:
if wordPossibility[index] not in charPossibilities: #if the char of the poossible decrypted word isn't in the possibility of the alphabet, then remove the word
self.possibilities.discard(wordPossibility);
def insidePattern(self, alpha):
# not used anymore since Dict implement this for all the dictionary
# get the pattern inside a word
for char in alpha:
if self.encrypted.count(char) >= 2:
self.inside[char] = [x.start() for x in re.finditer(char, self.encrypted)];
def outsidePattern(self, wordList):
# get the pattern of this word with all the word in the array given
# wordList is the array of all the words after this one (including himself)
self.outside = [];
wordList.pop(0); # remove himself from the list
for char in set(self.encrypted): # set exclude redundance
for word in wordList:
res = word.encrypted.find(char);
if res != -1:
# if 2 letter only need to match 1 letter from each word dthank to inside pattern
self.outside.append( {"char" : char, "wordIndex" : word.index, "insidePos" : self.encrypted.find(char), "outsidePos" : res } );
else :
self.outside.append({});
def matchInside(self):
# no used anymore, dict return now all word with same pattern
# remove the possibilities that didn't match with the inside pattern
for key, value in self.inside.items():
for i in range(len(value) - 1): #if 2ocu 1 eq needed, 3 ocu 2 eq needed etc
self.possibilities = set([x for x in self.possibilities if x[value[i]] == x[value[i+1]] ]);
def matchOther(self, otherWord, pos1, pos2):
# no used to... I think ...
validated1 = set();
validated2 = set();
for word1 in self.possibilities:
for word2 in otherWord.possibilities:
if word1[pos1] == word2[pos2]:
validated1.add(word1);
validated2.add(word2);
self.possibilities = validated1;
return validated2;
if __name__ == "__main__":
print("Run tests");
# create some tests here...