-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcordance.java
412 lines (373 loc) · 14.6 KB
/
concordance.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
// Team#8, Cong Tu, Yan Chen, Yung Lam
package finalproject;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Document;
import javax.swing.text.Highlighter;
import java.awt.*;
import java.awt.event.*;
public class concordance {
private HashMap<String,LinkedList> hashmap1=new HashMap<String,LinkedList>();
private HashMap<String,LinkedList> hashmap2=new HashMap<String,LinkedList>();
private HashMap<String,LinkedList> hashmap3=new HashMap<String,LinkedList>();
private HashMap<String,LinkedList> hashmap4=new HashMap<String,LinkedList>();
private ArrayList lineoftoken;
private int lineNumber;
static private JTextArea scene1=new JTextArea();
static private JTextArea scene2=new JTextArea();
static private JTextArea scene3=new JTextArea();
static private JTextArea scene4=new JTextArea();
JButton search = new JButton("Search");
JButton scene1b = new JButton("Scene 1");
JButton scene2b = new JButton("Scene 2");
JButton scene3b = new JButton("Scene 3");
JButton scene4b = new JButton("Scene 4");
static private final String[] allCharacterNamesAbbre={"Glo.", "Clar.", "Brak.", "Hast.",
"Anne.", "Riv.", "Grey.", "Q. Eliz.", "Buck.", "Stan.", "Q. Mar.", "Dors.", "Cates",
"First Murd", "Sec Murd"};
//private ArrayList<String> RemoveNarr=new ArrayList<String>();
private JTextField textsearched=new JTextField("Enter a word to search...");
private JTextArea result=new JTextArea("Concordance of The Tragedy of King Richard the Third\nAct 1 Scene 1 to 4 of the edited versions...");
public void ReadAndBuild(String filename1,String filename2,String filename3,String filename4)
{
this.ReadTheScript(filename1, scene1);
this.HashToTable(hashmap1);
this.ReadTheScript(filename2, scene2);
this.HashToTable(hashmap2);
this.ReadTheScript(filename3, scene3);
this.HashToTable(hashmap3);
this.ReadTheScript(filename4, scene4);
this.HashToTable(hashmap4);
}
public void ReadTheScript(String filename, JTextArea scene)
{
try
{
InputStreamReader fileIN = new InputStreamReader(getClass().getResourceAsStream(filename));
BufferedReader buffIN = new BufferedReader(fileIN);
String lineRead;
lineoftoken=new ArrayList();
while ((lineRead=buffIN.readLine())!=null) // if there is still something to read in
{
scene.append(lineRead+"\n");
if(lineRead=="\n" || lineRead=="" || lineRead.isEmpty())
continue;
StringTokenizer toker=new StringTokenizer(lineRead," \t\n\r\f,.?!;:\"[](){}()");
lineoftoken.add(Collections.list(toker)); // this add each separated words into the listarray
// doesn't care what the length of the word is now
}
buffIN.close();
fileIN.close();
}
catch (IOException event)
{
System.out.println("Exception Occurs: "+event);
}
}
public void HashToTable(HashMap<String,LinkedList> hashmap)
{// now lineoftoken should be an arraylist of "line", each line contain all the word on the line
// the size of lineoftoken will be equal to the total line of the text file
for(lineNumber=0; lineNumber<lineoftoken.size();lineNumber++) //line by line
{
ArrayList currentLine=(ArrayList)lineoftoken.get(lineNumber); // convert line of string to arraylist
for(int wordIndex=0;wordIndex<currentLine.size();wordIndex++) // now from word to word
{
String temp=((String)currentLine.get(wordIndex)).toLowerCase(); // convert to lower case first since the hashtable keys are all lowercase
if(temp.length()<4) // catalog words of 4 or more chars only
continue; // this will skip most of the character name abbreviation
// since some are less than 4 chars after tokenized
LinkedList<Integer> placesOfOccurrence;
if(!hashmap.containsKey(temp)) // if first time showing up
{
placesOfOccurrence=new LinkedList<Integer>(); // make new linked list
hashmap.put(temp, placesOfOccurrence);
}
else // already is a key and has linked list as value?
placesOfOccurrence=(LinkedList)hashmap.get(temp); // get the old linked list
if(!placesOfOccurrence.contains(lineNumber+1))
{placesOfOccurrence.addLast(lineNumber+1);} // refresh it with new link
}
}
}
public void listOccurrence(String word)
{
String ind;
String total=""; // clear up the textarea everytime a new search result found
word=word.toLowerCase();
if (hashmap1.containsKey(word))
{
ind=hashmap1.get(word).toString();
total=total+"Act 1 Scene 1, line: "+ind+"\n\n";
}
if (hashmap2.containsKey(word))
{
ind=hashmap2.get(word).toString();
total=total+"Act 1 Scene 2, line: "+ind+"\n\n";
}
if (hashmap3.containsKey(word))
{
ind=hashmap3.get(word).toString();
total=total+"Act 1 Scene 3, line: "+ind+"\n\n";
}
if (hashmap4.containsKey(word))
{
ind=hashmap4.get(word).toString();
total=total+"Act 1 Scene 4, line: "+ind+"\n\n";
}
if (total.isEmpty())
{
total="No result is found! Please try another word!...";
}
result.setText(total);
}
public void tester()
{
JFrame tester=new JFrame("Spring 2009 CSC 221 Final Project by Team 8 ( Cong Tu, Yan Chen, Yung Lam )");
tester.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); // closes all frame at one time
tester.setSize(607,560);
tester.setVisible(true);
tester.setLocation(150, 100);
tester.setLayout(null); // set this layout so we can add component wherever we want
tester.setResizable(false);
textsearched.setFont(new Font(textsearched.getFont().getFamily(), Font.BOLD, 16));
JLabel imglabel1 = new JLabel();
JLabel imglabel2 = new JLabel();
JLabel imglabel3 = new JLabel();
JLabel click2view=new JLabel("Click to view each scene");
click2view.setFont(new Font(textsearched.getFont().getFamily(), Font.BOLD, 14));
JScrollPane scrollPane=new JScrollPane(result); // the scrollpane add scroll to the textarea
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
imglabel1.setIcon(new ImageIcon(getClass().getResource("1.jpg")));
imglabel2.setIcon(new ImageIcon(getClass().getResource("2.jpg")));
imglabel3.setIcon(new ImageIcon(getClass().getResource("3.jpg")));
click2view.setForeground(Color.RED);
tester.add(textsearched); //add every component we have
tester.add(imglabel1);
tester.add(imglabel2);
tester.add(imglabel3);
tester.add(click2view);
tester.add(search);
tester.add(scene1b);
tester.add(scene2b);
tester.add(scene3b);
tester.add(scene4b);
tester.add(scrollPane);
imglabel1.setBounds(0, 0, 200, 299); // set components' location inside the frame
imglabel2.setBounds(200, 0, 200, 299);
imglabel3.setBounds(400, 0, 200, 299);
textsearched.setBounds(0,300, 400, 30);
search.setBounds(401, 300, 200, 29);
click2view.setBounds(5, 330, 195, 29);
scene1b.setBounds(198, 330, 100, 29);
scene2b.setBounds(299, 330, 100, 29);
scene3b.setBounds(400, 330, 100, 29);
scene4b.setBounds(501, 330, 100, 29);
scrollPane.setBounds(0,360, 602, 170);
textsearched.setBackground(Color.YELLOW);
search.setFont(new Font(textsearched.getFont().getFamily(), Font.BOLD, 14));
scene1b.setFont(new Font(textsearched.getFont().getFamily(), Font.BOLD, 14));
scene2b.setFont(new Font(textsearched.getFont().getFamily(), Font.BOLD, 14));
scene3b.setFont(new Font(textsearched.getFont().getFamily(), Font.BOLD, 14));
scene4b.setFont(new Font(textsearched.getFont().getFamily(), Font.BOLD, 14));
result.setBackground(Color.LIGHT_GRAY);
result.setFont(new Font(textsearched.getFont().getFamily(), Font.BOLD, 14));
result.setLineWrap(true);
result.setEditable(false);
scene1.setBackground(Color.LIGHT_GRAY);
scene1.setFont(new Font(textsearched.getFont().getFamily(), Font.BOLD, 12));
scene1.setEditable(false);
scene2.setBackground(Color.LIGHT_GRAY);
scene2.setFont(new Font(textsearched.getFont().getFamily(), Font.BOLD, 12));
scene2.setEditable(false);
scene3.setBackground(Color.LIGHT_GRAY);
scene3.setFont(new Font(textsearched.getFont().getFamily(), Font.BOLD, 12));
scene3.setEditable(false);
scene4.setBackground(Color.LIGHT_GRAY);
scene4.setFont(new Font(textsearched.getFont().getFamily(), Font.BOLD, 12));
scene4.setEditable(false);
textsearched.addFocusListener(new FocusListener()
{
public void focusGained (FocusEvent e){textsearched.setText(null);}
public void focusLost (FocusEvent e) {textsearched.setText(textsearched.getText().trim());}
});
search.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) // override old method
{
String wordSearch=textsearched.getText().trim();
if (wordSearch.isEmpty())
{result.setText("Please enter a word to search...");}
else if (wordSearch.length()<4)
{result.setText("You know you will get nothing if the word searched is less than 4 characters...");}
else
{listOccurrence(wordSearch);}
}
});
textsearched.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String wordSearch=textsearched.getText().trim();
if (wordSearch.isEmpty())
{result.setText("Please enter a word to search...");}
else if (wordSearch.length()<4)
{result.setText("You know you will get nothing if the word searched is less than 4 characters...");}
else
{listOccurrence(wordSearch);}
}
});
scene1b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String wordSearch=textsearched.getText().trim();
if (wordSearch.isEmpty())
{
result.setText("Please enter a word to search...");
view(scene1, "xyz"); // search a non-existed string to highlight nothing
}
else if (wordSearch.length()<4)
{result.setText("You know you will get nothing if the word searched is less than 4 characters...");}
else
{
view(scene1, wordSearch);
}
}
});
scene2b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String wordSearch=textsearched.getText().trim();
if (wordSearch.isEmpty())
{
result.setText("Please enter a word to search...");
view(scene2, "xyz");
}
else if (wordSearch.length()<4)
{result.setText("You know you will get nothing if the word searched is less than 4 characters...");}
else
{
view(scene2, wordSearch);
}
}
});
scene3b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String wordSearch=textsearched.getText().trim();
if (wordSearch.isEmpty())
{
result.setText("Please enter a word to search...");
view(scene3, "xyz");
}
else if (wordSearch.length()<4)
{result.setText("You know you will get nothing if the word searched is less than 4 characters...");}
else
{
view(scene3, wordSearch);
}
}
});
scene4b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String wordSearch=textsearched.getText().trim();
if (wordSearch.isEmpty())
{
result.setText("Please enter a word to search...");
view(scene4, "xyz");
}
else if (wordSearch.length()<4)
{result.setText("You know you will get nothing if the word searched is less than 4 characters...");}
else
{
view(scene4, wordSearch);
}
}
});
}
/*private class Actioner implements ActionListener
{
private JTextArea scene;
private String wordsearch;
public Actioner(JTextArea s, String w){scene=s;wordsearch=w;}
public void actionPerformed(ActionEvent e) {view(scene, wordsearch);}
}*/
public void highlight(JTextArea textarea, String word)
{ // remove all old highlights
removeOldHighlights(textarea);
try
{
Highlighter light = textarea.getHighlighter();
Document doc = textarea.getDocument();
String text = doc.getText(0, doc.getLength()).toLowerCase(); // convert to lowercase to include first character is capitalized, like "lord" and "Lord"
int index=0;
while ((index=text.indexOf(word,index))>= 0) // keep finding the position of the word matched
{
light.addHighlight(index, index+word.length(), new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW)); //add highlighter at position just found
index += word.length();
}
} catch (BadLocationException e) {System.out.println("Exception Occurs: "+e);}
}
public void removeOldHighlights(JTextArea textarea) {
Highlighter light = textarea.getHighlighter();
Highlighter.Highlight[] high = light.getHighlights();
for (int i=0; i<high.length; i++) { //remove highlighter from each position
if (high[i].getPainter() instanceof DefaultHighlighter.DefaultHighlightPainter) {light.removeHighlight(high[i]);}
}
}
/*public class ImagePanel extends JPanel {
Toolkit images = Toolkit.getDefaultToolkit();
private Image img1 = images.getImage(getClass().getResource("1.jpg"));
private Image img2 = images.getImage(getClass().getResource("2.jpg"));
private Image img3 = images.getImage(getClass().getResource("3.jpg"));
public ImagePanel()
{
super();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(img1, 0, 0, null);
g.drawImage(img2, 200, 0, null);
g.drawImage(img3, 400, 0, null);
}
}*/
public void view(JTextArea scene, String searchword)
{
JFrame viewer=new JFrame("Team 8 ( Cong Tu, Yan Chen, Yung Lam )"); // default
viewer.setSize(400,400);
viewer.setVisible(true);
viewer.setLocation(0,0);
viewer.setLayout(null);
viewer.setResizable(false);
if (scene.equals(scene1))
{viewer.setTitle("Act 1, Scene 1");viewer.setLocation(0, 0);}
else if (scene.equals(scene2))
{viewer.setTitle("Act 1, Scene 2");viewer.setLocation(50, 50);}
else if (scene.equals(scene3))
{viewer.setTitle("Act 1, Scene 3");viewer.setLocation(100, 100);}
else
{viewer.setTitle("Act 1, Scene 4");viewer.setLocation(150, 150);}
//viewer.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
JScrollPane viewscroll=new JScrollPane(scene);
viewscroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
viewscroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
viewer.add(viewscroll);
viewscroll.setBounds(0, 0, 395, 371);
// add highlighter to the searched word if there is any
highlight(scene, searchword);
}
public static void main(String args[]) throws IOException
{
concordance shakespear = new concordance();
shakespear.ReadAndBuild("Act1Scene1_edited.txt", "Act1Scene2_edited.txt", "Act1Scene3_edited.txt", "Act1Scene4_edited.txt");
shakespear.tester();
}
}