-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrypCiphertextWordMapping.java
More file actions
122 lines (110 loc) · 4.46 KB
/
CrypCiphertextWordMapping.java
File metadata and controls
122 lines (110 loc) · 4.46 KB
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
package crypSwing;
import java.util.*;
/**
* This class contains one ciphertext word and every plaintext word to which we might guess it maps.
* @author Bitman
* @version 2.0 01/20/21
* @version 3.0 11/30/22
*/
public class CrypCiphertextWordMapping implements Comparable<CrypCiphertextWordMapping> {
private String ciphertext;
private List<String> plaintextCandidates = new ArrayList<String>();
/**
* This constructor takes a ciphertext word and stores all the initial guesses of the plaintext translations.
* @param ciphertextWord The ciphertext word
* @param alphabetMap The mapping of every possible plaintext translation of each ciphertext letter
*/
public CrypCiphertextWordMapping(String ciphertextWord, CrypAlphabetMapping alphabetMap) {
ciphertext = ciphertextWord;
CrypKey dummy = new CrypKey();
plaintextCandidates = CrypCodeBreaker.breakWord(ciphertext, false, dummy);
// This code assumes that if the code breaker finds nothing, it returns a message with at least one space in it.
if (plaintextCandidates.get(0).contains(" ")) plaintextCandidates.clear();
// Now eliminate all the plaintext translations that don't conform to the alphabet map.
pareDown(alphabetMap);
}
/**
* Get the ciphertext
* @return The ciphertext
*/
String getCiphertext() {
return ciphertext;
}
/**
* Return the number of candidate plaintext words for this ciphertext word.
* @return The number of candidates
*/
int numberOfCandidates() {
return plaintextCandidates.size();
}
/**
* Return the candidate at the given index.
* @param index The index
* @return The candidate plaintext translation
*/
String candidate(int index) {
if (numberOfCandidates() <= index) return "";
return plaintextCandidates.get(index);
}
/**
* This method eliminates all the candidate plaintext words that don't conform to a given alphabet mapping.
* @param alphabetMap The alphabet mapping
*/
public void pareDown(CrypAlphabetMapping alphabetMap) {
int wordIndex = 0;
int letterIndex;
// Loop through all the candidate words.
wordLoop:
while (wordIndex < plaintextCandidates.size()) {
// Given one candidate word, loop through all the characters in the ciphertext word AND candidate plaintext word.
for (letterIndex = 0; letterIndex < ciphertext.length(); letterIndex++)
// If the mapping doesn't allow this plaintext character for this ciphertext letter...
if (!alphabetMap.allows(ciphertext.charAt(letterIndex), plaintextCandidates.get(wordIndex).substring(letterIndex, letterIndex+1))) {
// ...then eliminate the plaintext translation...
plaintextCandidates.remove(wordIndex);
// ...and resume the list with the same index, which should be the next plaintext translation.
continue wordLoop;
}
// If we didn't eliminate the word (that is, the possible plaintext translation), increment to the next word.
wordIndex++;
}
}
/**
* Defines the sorting order of word-mapping entries
* @param other The second entry
* @return 1 if the second entry should come first, -1 if the first entry should come first
*/
@Override
public int compareTo(CrypCiphertextWordMapping other) {
// The first criterion is the number of possible plaintexts; the lower number comes first.
// If that number is tied, the second criterion is length; the higher number comes first.
// If both of those criteria are tied, it doesn't matter, except that we want consistent
// results, so make it alphabetical.
if (this.plaintextCandidates.size() > other.plaintextCandidates.size())
return 1;
if (this.plaintextCandidates.size() < other.plaintextCandidates.size())
return -1;
if (this.ciphertext.length() < other.ciphertext.length())
return 1;
if (this.ciphertext.length() > other.ciphertext.length())
return -1;
if (this.ciphertext.compareTo(other.ciphertext) > 0)
return 1;
return -1;
}
/**
* The Comparator
*/
public static Comparator<CrypCiphertextWordMapping> CrypCiphertextWordMappingComparator
= new Comparator<>() {
/**
* @param entry1 The first entry to be compared
* @param entry2 The second entry to be compared
* @return <code>1</code> if the second entry should come first
* <code>-1</code> if the first entry should come first
*/
public int compare(CrypCiphertextWordMapping entry1, CrypCiphertextWordMapping entry2) {
return entry1.compareTo(entry2);
}
};
}