/
PAmiableWordsUploadInterface.java
393 lines (359 loc) · 13 KB
/
PAmiableWordsUploadInterface.java
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import java.awt.Component;
import java.awt.Container;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class PAmiableWordsUploadInterface extends JPanel {
private static final long serialVersionUID = 1L;
public static JFrame frame;
public static JFileChooser chooser;
public static JFileChooser chooserSave;
public static int iteration;
public static int[] pset;
public static int[] word;
public static int enteredAlphabet;
public static Scanner scanner;
public PAmiableWordsUploadInterface(int[] word, int alphabet, String savePath, int it, int[] pset) {
try { //checking if word input is valid
ParikhMatrixCalculator pM = new ParikhMatrixCalculator();
int prev = pset[0];
int[] uniqueset = new int[pset.length];
uniqueset[0] = prev;
int setcount = 1;
for (int i=0;i<pset.length;i++) {
if (!(prev==pset[i])) {
uniqueset[setcount]=pset[i];
setcount = setcount+1;
}
prev = pset[i];
}
int[] setP = Arrays.copyOfRange(uniqueset, 0, setcount);
int maxProjLetter = max(setP);
int projInWord = 0;
for (int i=0;i<word.length;i++) {
for (int j=0;j<setP.length;j++) {
if (word[i]==setP[j]) {
projInWord=projInWord+1;
break;
}
}
}
if (maxProjLetter>=alphabet) {
throw new Exception("Entered projection not possible - alphabet size.");
}
else if (projInWord==0) {
throw new Exception("Entered projection not possible - projection not in word.");
}
String convertinputWord = new String("");
for (int j=0;j<word.length;j++) {
ParikhMatrixCalculator cPM = new ParikhMatrixCalculator();
convertinputWord = convertinputWord + cPM.getCharForNumber(word[j]);
}
//Open save dialog and create file
String defaultTitle = "Words P-amiable With " + convertinputWord + ".txt";
chooserSave = new JFileChooser();
if (it==1) {
chooserSave.setSelectedFile(new File("P-Parikh Matrix.txt"));
}
if (it==0) {
File outputfile = new File(savePath,defaultTitle);
outputfile.createNewFile();
FileWriter write = new FileWriter(outputfile, false);
FileWriter append = new FileWriter(outputfile, true);
PrintWriter print_line = new PrintWriter(write);
PrintWriter add_line = new PrintWriter(append);
int[][] parikhMatrix = pM.ParikhMatrix(word,alphabet+1);
String start = new String("The Parikh matrix of your word ");
print_line.printf("%s", start);
print_line.printf("%s"+":"+"%n", convertinputWord);
print_line.close();
for (int i=0;i<parikhMatrix.length;i++) {
for (int j=0;j<parikhMatrix.length;j++) {
add_line.printf("%4d", parikhMatrix[i][j]);
}
add_line.printf("%n");
}
add_line.printf("%n");
int[] finalword = word;
//now find all permutations of firstWord
PermuteArrayWithDuplicates perm = new PermuteArrayWithDuplicates();
List<List<Integer>> permute = perm.permute(Arrays.copyOf(word, word.length));
//remove any words with incorrect matrix
int numWords = permute.size();
int[][] results = new int[numWords][finalword.length];
int counter = 0;
int[][] matchIndex = new int [numWords][2];
for (int i=0;i<numWords;i++) {
List<Integer> cW = permute.get(i);
int[] currentWord = cW.stream().mapToInt(j->j).toArray();
ParikhMatrixCalculator cPM = new ParikhMatrixCalculator();
int[][] currentParikhMatrix = cPM.ParikhMatrix(currentWord,alphabet+1);
if (Arrays.deepEquals(parikhMatrix,currentParikhMatrix)) {
results[counter] = currentWord;
counter = counter+1;
matchIndex[i][0]=1;
}
else {
matchIndex[i][0]=0;
}
}
String start2 = new String("The projection of your word ");
add_line.printf("%s", start2);
add_line.printf("%s"+" with your projection set {", convertinputWord);
for (int i=0;i<pset.length;i++) {
if (i==pset.length-1) {
add_line.printf("%s}: %n", pM.getCharForNumber(pset[i]));
}
else {
add_line.printf("%s,", pM.getCharForNumber(pset[i]));
}
}
Projection p = new Projection();
int[] pword = p.ProjectionCalculator(word,setP,alphabet);
int[][] pparikhMatrix = pM.ParikhMatrix(pword,setP.length+1);
int[] mappedWord = p.InverseProjection(pword,setP); //map back to original set
String convertpinputWord = new String("");
for (int j=0;j<pword.length;j++) {
convertpinputWord = convertpinputWord + pM.getCharForNumber(mappedWord[j]);
}
add_line.printf("%s", convertpinputWord);
add_line.printf("%n");
add_line.printf("%n");
String start3 = new String("The P-Parikh matrix of your word ");
add_line.printf("%s", start3);
add_line.printf("%s"+":"+"%n", convertinputWord);
//add_line.close();
for (int i=0;i<pparikhMatrix.length;i++) {
for (int j=0;j<pparikhMatrix.length;j++) {
add_line.printf("%4d", pparikhMatrix[i][j]);
}
add_line.printf("%n");
}
add_line.printf("%n");
//remove any words with incorrect matrix
int pnumWords = permute.size();
int[][] presults = new int[pnumWords][pword.length];
int pcounter = 0;
ParikhMatrixCalculator pcPM = new ParikhMatrixCalculator();
for (int i=0;i<pnumWords;i++) {
List<Integer> pcW = permute.get(i);
int[] currentWord = pcW.stream().mapToInt(j->j).toArray();
int[] pcurrentWord = p.ProjectionCalculator(currentWord,setP,alphabet);
int[][] pcurrentParikhMatrix = pcPM.ParikhMatrix(pcurrentWord,setP.length+1);
if (Arrays.deepEquals(pparikhMatrix,pcurrentParikhMatrix)) {
presults[pcounter] = pcurrentWord;
pcounter = pcounter+1;
matchIndex[i][1]=1;
}
else {
matchIndex[i][1]=0;
}
}
int amiablecounter = 0;
int pamiablecounter = 0;
if (counter>0) {
for (int i=0;i<numWords;i++) {
if (matchIndex[i][0]==1) {
amiablecounter = amiablecounter + 1;
}
if (matchIndex[i][0]==1 && matchIndex[i][1]==1) {
pamiablecounter = pamiablecounter+1;
}
}
}
if (amiablecounter>0 && pamiablecounter>0 && counter>0) {
if (pamiablecounter==1) {
add_line.printf("There is %d word that shares a Parikh matrix and P-Parikh matrix with your word %s: %n", pamiablecounter,convertinputWord);
} else {
add_line.printf("There are %d words that share a Parikh matrix and P-Parikh matrix with your word %s: %n", pamiablecounter,convertinputWord);
}
if (counter>0) {
for (int i=0;i<numWords;i++) {
if (matchIndex[i][0]==1) {
String convertWord = new String("");
List<Integer> cW = permute.get(i);
int[] currentWord = cW.stream().mapToInt(j->j).toArray();
for (int j=0;j<finalword.length;j++) {
convertWord = convertWord + pM.getCharForNumber(currentWord[j]);
}
}
}
}
for (int i=0;i<numWords;i++) {
if (matchIndex[i][0]==1 && matchIndex[i][1]==1) {
String convertWord2 = new String("");
String pconvertWord = new String("");
List<Integer> cW = permute.get(i);
int[] currentWord = cW.stream().mapToInt(j->j).toArray();
int[] pcurrentWord = p.ProjectionCalculator(currentWord,setP,alphabet);
int[] mappedWord2 = p.InverseProjection(pcurrentWord,setP); //map back to original set
for (int j=0;j<finalword.length;j++) {
convertWord2 = convertWord2 + pcPM.getCharForNumber(currentWord[j]);
}
for (int j=0;j<pword.length;j++) {
pconvertWord = pconvertWord + pcPM.getCharForNumber(mappedWord2[j]);
}
add_line.printf("%s (%s)" + "%n", convertWord2, pconvertWord);
}
}
}
else {
add_line.printf("Your word %s is uniquely described by its P-Parikh matrix. %n", convertinputWord);
}
add_line.printf("%n");
if (!(amiablecounter==pamiablecounter)) {
int distinctCount = counter - pamiablecounter;
if (distinctCount==1){
add_line.printf("There is %d word that shares a Parikh matrix with your word %s but does not share a P-Parikh matrix with it:"+"%n", distinctCount,convertinputWord);
}
else {
add_line.printf("There are %d words that share a Parikh matrix with your word %s but do not share a P-Parikh matrix with it:"+"%n", distinctCount,convertinputWord);
}
for (int i=0;i<numWords;i++) {
if (matchIndex[i][0]==1 && matchIndex[i][1]==0) {
String notconvertWord = new String("");
String notpconvertWord = new String("");
List<Integer> cW = permute.get(i);
int[] notcurrentWord = cW.stream().mapToInt(j->j).toArray();
int[] pcurrentWord = p.ProjectionCalculator(notcurrentWord,setP,alphabet);
int[] mappedWord3 = p.InverseProjection(pcurrentWord,setP); //map back to original set
for (int j=0;j<finalword.length;j++) {
notconvertWord = notconvertWord + pcPM.getCharForNumber(notcurrentWord[j]);
}
for (int j=0;j<mappedWord3.length;j++) {
notpconvertWord = notpconvertWord + pcPM.getCharForNumber(mappedWord3[j]);
}
add_line.printf("%s (%s)" + "%n", notconvertWord, notpconvertWord);
}
}
}
else {
if (counter==1) {
add_line.printf("Your word is described uniquely by its Parikh matrix.");
}
else {
add_line.printf("The Parikh matrix of your entered word is not P-distinguishable with your given projection set.");
}
}
add_line.close();
}
if (!scanner.hasNext()) {
Home.dialog.dispose();
}
}
catch (Exception e1){
e1.printStackTrace();
Home.dialog.dispose();
}
}
public static void actioned() {
try {
File uploadedFile = Home.uploadedFile;
Home.dialog.setVisible(false);
chooserSave = new JFileChooser();
//Create file chooser for user to select a location to save the output files to (default to same location as uploaded file)
chooserSave.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
disableTextField(chooserSave); //prevent user from altering file name as these are auto generated
chooserSave.setDialogTitle("Select Location To Save Output Files");
chooserSave.setSelectedFile(new File("Words P-Amiable With..."));
chooserSave.setCurrentDirectory(uploadedFile);
int returnVal = chooserSave.showSaveDialog(frame);
String path = chooserSave.getCurrentDirectory().getAbsolutePath();
//if user selects a location
if (returnVal == JFileChooser.APPROVE_OPTION) {
scanner = new Scanner(uploadedFile);
scanner.useDelimiter("\\n");
while (scanner.hasNext()){
if (Home.mySwingWorker.isCancelled()) {
return;
}
iteration++;
if (iteration%2==0) {
String set = scanner.next();
set = set.replaceAll("[^a-zA-Z0-9]", ""); //remove special characters
set = set.toLowerCase();
pset = new int[set.length()];
ParikhMatrixCalculator pM = new ParikhMatrixCalculator();
for (int i=0;i<set.length();i++) {
if (Character.isLetter(set.charAt(i))){
String character = Character.toString(set.charAt(i));
pset[i] = pM.getNumberForChar(character)-1;
}
else if (Character.isDigit(set.charAt(i))) {
pset[i] = Character.getNumericValue(set.charAt(i));
}
}
Arrays.sort(pset);
new PAmiableWordsUploadInterface(word,enteredAlphabet, path, 0, pset);
}
if (iteration%2==1) {
String enteredWord = scanner.next();
word = new int[enteredWord.length()];
ParikhMatrixCalculator pM = new ParikhMatrixCalculator();
//convert next word in file into int[] for handling
if (!isNumber(enteredWord)) {
for (int i=0;i<enteredWord.length();i++) {
String character = Character.toString(enteredWord.charAt(i));
int l = pM.getNumberForChar(character)-1;
if (l>=0){
word[i] = l;
}
}
}
else {
for (int i=0;i<enteredWord.length();i++) {
int l = Character.getNumericValue(enteredWord.charAt(i));
word[i] = l;
}
}
//calculate these every iteration to allow different alphabet sizes
int maxLetter = max(word);
enteredAlphabet = maxLetter+1;
}
}
scanner.close();
Home.dialog.dispose();
}
}
catch (Exception e) {
e.printStackTrace();
Home.dialog.dispose();
}
}
private static boolean isNumber(String s)
{
for (int i = 0; i < s.length(); i++)
if (Character.isDigit(s.charAt(i)) == false)
return false;
return true;
}
private static int max(int[] t) {
int maximum = t[0]; // start with the first value
for (int i=1; i<t.length; i++) {
if (t[i] > maximum) {
maximum = t[i]; // new maximum
}
}
return maximum;
}
public static boolean disableTextField(Container container) {
Component[] comps = container.getComponents();
for (Component comp : comps) {
if (comp instanceof JTextField) {
((JTextField) comp).setEnabled(false);
return true;
}
if (comp instanceof Container) {
if (disableTextField((Container) comp)) return true;
}
}
return false;
}
}