-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmainWindow.java
515 lines (489 loc) · 19.8 KB
/
mainWindow.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package lab1;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Path;
import java.util.*;
import javax.naming.spi.DirStateFactory.Result;
import org.omg.CORBA.FloatHolder;
public class mainWindow {
private static String fileName = null;
private static final int infinite = 1000;
private static int randomAuto = 0;
//private String filePath;
public static void main(String[] args) throws InterruptedException {
Scanner in = new Scanner(System.in);
System.out.println("Choose the input mode: 0 for file, 1 for console");
//int option = in.nextInt();
int option = 0;
if (option == 0) {
System.out.println("Input file name(suffixed included):");
do {
//fileName = in.next();
fileName = "data.txt";
} while (!fileName.contains(".txt") && !fileName.contains(".bin"));
}
Graph G = createDirectedGraph(fileName);
//show graph
// System.out.println("vertex:");
// for(int i =0;i<G.getVertexNum();i++){
// System.out.println(G.getNodes().get(i).getWord() + " ");
// }
// for(int i =0;i<G.getVertexNum();i++){
// System.out.println(G.getNodeList().get(i));
// }
// //test bridge word
// String word1, word2;
// word1 = in.next();word2 = in.next();
// System.out.println(queryBridgeWords(G, word1, word2));
//showDirectedGraph(G);
//test shortest path
// String w1, w2;
// //while(true){
// w1 = in.next();
// w2 = in.next();
// String path = calcShortestPath(G, w1, w2);
// System.out.println(path);
// String[] Words = path.split("\n");
// color(G, Words[0], 1);
// showDirectedGraph(G);
// //}
//test random walk
while(true){
System.out.println(randomWalk(G));
}
//
// //test create new text
// String newText;
// newText = "Seek to explore new and exciting synergies";
// newText = generateNewText(G, newText);
// System.out.println(newText);
}
//find bridge word in the sentence
public static String queryBridgeWords(Graph G, String w1, String w2) {
String s = null;
if (G.getNodeSet().containsKey(w1) && G.getNodeSet().containsKey(w2)) {
s = getBridgeWord(G, w1, w2);
if (s == null) {
s = ("No bridge words from \"" + w1 + "\"to \"" + w2 + "\"!");
} else {
String[] words = s.split(" ");
StringBuffer tmp = new StringBuffer();
for (int i = 0; i < words.length - 1; i++) {
tmp.append(words[i] + ", ");
}
for (String w : words) {
color(G, w1 + " " + w + " " + w2, 1);
}
tmp.append(((words.length > 1) ? "and " : " ") + words[words.length - 1] + ".");
s = "The bridge words from \"" + w1 + "\" to \"" + w2 + "\" " + ((words.length > 1) ? "are:" : "is:") + tmp.toString();
}
} else if (G.getNodeSet().containsKey(w2)) {
s = "No \"" + w1 + "\" in the graph!";
} else if (G.getNodeSet().containsKey(w1)) {
s = "No \"" + w2 + "\" in the graph!";
} else {
s = "No \"" + w1 + "\" and \"" + w2 + "\" in the graph!";
}
return s;
}
//create new sentence based on the bridge words
public static String generateNewText(Graph G, String inputText) {
//return mode: new sentence do not need modify to the standard format
String[] words = inputText.split("[^a-zA-Z]+");
String temp = null;
String[] bridgeWords;
StringBuffer newSentence = new StringBuffer();
Random rand = new Random();
StringBuffer colorVertex = new StringBuffer();;
for (int i = 0; i < words.length - 1; i++) {//traverse the new text
temp = getBridgeWord(G, words[i], words[i + 1]);//find bridge words
newSentence.append(words[i] + " ");
if (temp != null) {//if there is any bridge word exists
bridgeWords = temp.split(" ");
String w = bridgeWords[rand.nextInt(bridgeWords.length)];
colorVertex.append(w + " ");
newSentence.append(w + " ");
}
}
if (colorVertex.length() != 0) {
//System.out.println(colorVertex.toString());
color(G, colorVertex.toString(), 0);
}
newSentence.append(words[words.length - 1]);
//System.out.println(newSentence.toString());
return newSentence.length() == 0 ? "" : newSentence.toString();
}
//generate new graph
public static Graph createDirectedGraph(String filename) {
String temp = null;
if (filename == null) {//read from console
System.out.println("Input the sentence:");
Scanner in = new Scanner(System.in);
temp = in.nextLine();
in.close();
} else {//read from file
File file = new File(filename);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
StringBuffer tmp = new StringBuffer();
while ((temp = reader.readLine()) != null) {
tmp.append(temp);
}
temp = tmp.toString();
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
//create graph
String[] words = temp.split("[^a-zA-Z]+");
//establish the Graph
Graph G = new Graph();
//transform the words to lower case
for (int i = 0; i < words.length; i++) {
words[i] = words[i].toLowerCase();
}
//find same words
Words testWord = new Words();
String[] newWord = new String[words.length];
int[] mark = new int[words.length];
newWord = words.clone();
for (int i = 0; i < words.length; i++) {
mark[i] = i;
}
for (int i = 0; i < words.length - 1; i++) {
for (int j = 0; j < words.length; j++) {
if (mark[j] != j) {
continue;
}
testWord.setWord1(words[i]);
testWord.setWord2(words[j]);
if (testWord.isSame()) {
newWord[i] = testWord.getOriginWord();
newWord[j] = testWord.getOriginWord();
mark[j] = i;
}
}
}
//add words
for (int i = 0; i < words.length; i++) {
G.addNode(newWord[mark[i]]);
}
//add edge
for (int i = 0; i < words.length - 1; i++) {
G.addEdge(newWord[mark[i]], newWord[mark[i + 1]]);
}
return G;
}
//create the graph based on the data
public static void showDirectedGraph(Graph G) {
//white black blue bluestart blueend
List<String> color = Arrays.asList("#F5F5F5", "#424242", "#84FFFF", "#82B1FF", "#B9F6CA", "#0091EA");
GraphViz gv = new GraphViz();
gv.addln(gv.start_graph());
Map<String, HashMap<String, Edge>> g = G.getNodeList();
for (Node a : G.getNodes()) {
gv.addln(a.getWord() + "[style=filled] " + " [fillcolor =\"" + color.get(a.getColor()) + "\"];");
// gv.addln(a.getWord() + " [fillcolor =" + color.get(a.getColor())+ "];");
}
for (String i : g.keySet()) {
HashMap<String, Edge> k = g.get(i);
for (String j : k.keySet()) {
gv.addln(i + "->" + j + "[color=\"" + color.get(k.get(j).getColor())
+ "\",label=\"" + k.get(j).getWeight() + "\"];");
}
}
gv.addln(gv.end_graph());
String type = "png";
File out = new File("out." + type);
gv.writeGraphToFile(gv.getGraph(gv.getDotSource(), type), out);
G.resetColor();
}
//search a path randomly
public static String randomWalk(Graph G) throws InterruptedException {
//return mode : w1->w2->w3...
//define some variable and initialize them at the same time
Random random = new Random();
//generate a word randomly
String tempPath = G.getNodes().get(random.nextInt(G.getVertexNum())).getWord();//get first word
String nextWord = tempPath;
Scanner in = new Scanner(System.in);
randomAuto = in.nextInt();
String result = null;
if(randomAuto == 0){
MyThread tempThread = new MyThread(G, nextWord);
MyThread tempThread2 = new MyThread(true, tempThread.getThread());
tempThread.getThread().join();
result = tempThread.getPath();
}
else{
MyThread tempThread = new MyThread(G, nextWord);
tempThread.getThread().join();
result = tempThread.getPath();
}
System.out.println("finish");
return result;
}
public static String moveOneStep(Graph G, String presentWord, String path){
Random random = new Random();
HashMap<String, Edge> map = G.getNodeList().get(presentWord);
if (map == null)
return null; //if there is no road then exit
String[] tempList = map.keySet().toArray(new String[0]);
String nextWord = (tempList.length >= 1) ? tempList[random.nextInt(tempList.length)] : null;
if(nextWord!= null){
color(G, path, 1);
//System.out.println(path);
}
return nextWord;
}
//calculate the shortest path two (all possible solutions)
public static String calcShortestPath1(Graph G, String word1, String word2) {
int minDistance = infinite;//the shortest distance
PriorityQueue<treeNodes> candidate = new PriorityQueue<treeNodes>(G.getVertexNum(), myCompare);
Map<String, HashMap<String, Edge>> tempMap = G.getNodeList();
treeNodes prNode = null;
//initialize the tree
treeNodes startPoint = new treeNodes(null, 0, word1);
for (String a : tempMap.get(word1).keySet()) {
int tempWeight = tempMap.get(word1).get(a).getWeight();
treeNodes tempNode = new treeNodes(startPoint, tempWeight, a);
startPoint.getChildren().add(tempNode);
candidate.add(tempNode);
}
//expand the tree
while (true) {
prNode = candidate.poll();
if (prNode == null || prNode.getWord().equals(word2)) {
break;
}
if (!tempMap.containsKey(prNode.getWord())) {
candidate.remove(prNode);
continue;
}
for (String b : tempMap.get(prNode.getWord()).keySet()) {
boolean find = false;
int newDistance = prNode.getWeiht() + tempMap.get(prNode.getWord()).get(b).getWeight();
for (treeNodes tNode : candidate) {
//if the new distance is shorter then update the tree
if (tNode.getWord() == b) {
find = true;
if (tNode.getWeiht() > newDistance) {
candidate.remove(tNode);
}
candidate.add(new treeNodes(prNode, newDistance, b));
break;
}
}
if (!find) {
candidate.add(new treeNodes(prNode, newDistance, b));//add when the node never appear in the tree
}
}
}
//trace back one route
if (prNode == null) {
return ("Inaccessible form \"" + word1 + "\" to \"" + word2 + "\"");
}
minDistance = prNode.getWeiht();
StringBuffer result = new StringBuffer();
// do{
// result.append(prNode.getWord() + "<-");
// prNode = prNode.getFather();
// }while(prNode.getWord() != word1);
// result.append(word1 + "\n");
//find other same distance route
candidate.add(prNode);
for (treeNodes dNodes : candidate) {
if (dNodes.getWeiht() == minDistance && dNodes.getWord().equals(word2)) {
//save route
prNode = dNodes;
List<String> tempList = new ArrayList<String>();
do {
tempList.add(prNode.getWord());
prNode = prNode.getFather();
} while (prNode.getWord() != word1);
result.append(word1);
Collections.reverse(tempList);
for (String aString : tempList) {
result.append("->" + aString);
}
result.append("\n");
}
}
return result.toString().substring(0, result.length() - 1);
}
//calculate the shortest path one to all
public static String calcShortestPath2(Graph G, String word1) {
int[] minDistance = new int[G.getVertexNum()];
int word1Sub = -1, word2Sub = -1;
Set<Integer> mark = new HashSet<Integer>();
int[] path = new int[G.getVertexNum()];
HashMap<String, Edge> tempMap = G.getNodeList().get(word1);
String tempWord;
if (tempMap == null)//the word has no connection with others
{
return null;
}
//initialize all the variable
for (int i = 0; i < G.getVertexNum(); i++) {
tempWord = G.getNodes().get(i).getWord();
if (tempMap.containsKey(tempWord)) {
minDistance[i] = tempMap.get(tempWord).getWeight();
} else if (!tempWord.equals(word1)) {
minDistance[i] = infinite;
} else {
word1Sub = i;
}
path[i] = -1;
mark.add(i);
}
mark.remove(word1Sub);
for (int i = 0; i < G.getVertexNum() - 1; i++) {//traverse all the vertex and calculate the minimal distance
//find minimal distance
int min = infinite;
int sub = -1;
for (int a : mark) {
if (min > minDistance[a]) {
sub = a;
min = minDistance[a];
}
}
if (sub == -1) {
break;
}
//remove minimal edge
mark.remove(sub);
//update the distance
HashMap<String, Edge> localMap = G.getNodeList().get(G.getNodes().get(sub).getWord());
if (localMap != null) {
for (int a : mark) {
String tWord = G.getNodes().get(a).getWord();
int pastDistance = minDistance[a];
int newDistance = (localMap.containsKey(tWord)) ? (localMap.get(tWord).getWeight() + min) : (infinite + 1);
if (newDistance < pastDistance) {
minDistance[a] = newDistance;
path[a] = sub;
}
}
}
}
StringBuffer result = new StringBuffer();
int subscript;
StringBuffer tmp = new StringBuffer();
Set<Integer> destination = new HashSet<Integer>();
for (int i = 0; i < G.getVertexNum(); i++) {
if (i != word1Sub) {
destination.add(i);
}
}
for (int a : destination) {
if (a == -1) {
continue;
}
subscript = a;
//tmp.setLength(0);
List<String> tempList = new ArrayList<String>();
while (subscript != -1) {
tempList.add(G.getNodes().get(subscript).getWord());
//tmp.append(G.getNodes().get(subscript).getWord() + "<-");
subscript = path[subscript];
}
if (a != -1) {
//result.append(tmp + word1);
result.append(word1);
Collections.reverse(tempList);
for (String aString : tempList) {
result.append("->" + aString);
}
result.append("\n");
}
}
return result.toString().substring(0, result.length() - 1);
}
public static String calcShortestPath(Graph G, String word1, String word2) {
//return mode
//1. warning message
//2. w1<-w2<-...(attention! the path is reversed)
//3. w1<-w2<-... \n w3<-w4 ...
String result = null;
if (!G.getNodeSet().containsKey(word1)) {
if (!G.getNodeSet().containsKey(word2)) {
result = "No \"" + word1 + "\"" + " and \"" + word2 + "\" in the graph!";
} else {
result = "No \"" + word1 + "\" in the graph!";
}
} else if (!G.getNodeSet().containsKey(word2)) {
result = "No \"" + word2 + "\" in the graph!";
} else {
if (word2.length() == 0) {
result = calcShortestPath2(G, word1);
} else {
result = calcShortestPath1(G, word1, word2);
}
color(G, result.split("\n")[0], 1);
}
return result;
}
public static Comparator<treeNodes> myCompare = new Comparator<treeNodes>() {
@Override
public int compare(treeNodes c1, treeNodes c2) {
return (int) (c1.getWeiht() - c2.getWeiht());
}
};
//get the bridge according to the graph
public static String getBridgeWord(Graph G, String w1, String w2) {
ArrayList<String> result = new ArrayList<String>();
HashMap<String, Edge> e = G.getNodeList().get(w1);
if (e == null) {
return null;
}
for (String i : e.keySet()) {
if (G.getNodeList().get(i) != null && G.getNodeList().get(i).containsKey(w2)) {
result.add(i);
}
}
StringBuffer tmp = new StringBuffer();
for (String aString : result) {
tmp.append(aString + " ");
}
return (result.size() == 0) ? null : tmp.toString();
}
//set color to the line and point
public static void color(Graph G, String path, int mode) {
if(path.isEmpty())return;
ArrayList<Node> vertex = G.getNodes();
Map<String, HashMap<String, Edge>> edge = G.getNodeList();
String[] words = path.split("->| |//n");
if (mode == 0) {//just vertexs
for (String aString : words) {
if (G.getNodeSet().get(aString) != null) {
vertex.get(G.getNodeSet().get(aString)).setColor(2);
}
}
} else {//start ->paths ->end
int len = words.length;
for (int i = 0; i < len - 1; i++) {
if (edge.get(words[i]) != null && edge.get(words[i]).get(words[i + 1]) != null) {
edge.get(words[i]).get(words[i + 1]).setColor(5);
}
if (G.getNodeSet().get(words[i + 1]) != null) {
vertex.get(G.getNodeSet().get(words[i + 1])).setColor(2);
}
}
if (G.getNodeSet().get(words[0])!=null)
vertex.get(G.getNodeSet().get(words[0])).setColor(4);
if (G.getNodeSet().get(words[len - 1])!=null)
vertex.get(G.getNodeSet().get(words[len - 1])).setColor(3);
}
}
}