This repository has been archived by the owner on Jul 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuildRecSys.java
397 lines (333 loc) · 10 KB
/
BuildRecSys.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
package edu.cmu.sphinx.demo.buildrecsyst10team4;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.LayoutManager;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.Timer;
public class BuildRecSys extends JFrame {
private int wordColor = 0;
private int charPosition = 0;
private int noOfTrials = 0;
private String moveOn;
private static int POINTER = 0;
private int SCORE = 0;
private int progressBarCtr = 0;
private final static Color backgroundColor = new Color(0xFF, 0xFF, 0xFF);
private JTextField messageTextField;
private JButton speakButton;
private JLabel wordToBeShownLabel;
private JLabel scoreLabel;
private JLabel messageToUser;
private JProgressBar progressBar;
private JButton nextButton;
private JLabel wowLabel;
private BuildRecSysRecognizer recognizer;
public ArrayList<String> germanDictionary;
public BuildRecSys() throws IOException {
super("Mini Project One - By Team04");
setSize(400, 400);
setDefaultLookAndFeelDecorated(true);
getContentPane().add(createMainPanel(), BorderLayout.CENTER);
germanDictionary = fillData();
recognizer = new BuildRecSysRecognizer();
recognizer.startup();
speakButton.addMouseListener(new MouseListener() {
@Override
public void mousePressed(MouseEvent e) {
System.out.println("MousePressed+++");
System.out.println("Start speaking. Press Ctrl-C to quit.\n");
recognizer.run();
System.out.println(recognizer.getTheCharacter());
micHelper();
}
private void micHelper() {
wordToBeShownLabel.setText(""); // clears the word
String currentWord = germanDictionary.get(POINTER);
int currentWordSize = germanDictionary.get(POINTER).length();
int correctLettersSoFar = 0;
char c = recognizer.getTheCharacter();
if (c == currentWord.charAt(charPosition) && noOfTrials < 3) {
// System.out.println("true!!!");
updateMessageToUser("<html><font color='green'>True!</font> Move on to the next letter.</html>");
SCORE += 10;
correctLettersSoFar++;
charPosition++;
noOfTrials = 0;
} else {
// System.out.println("false! we are expecting:"
// + currentWord.charAt(charPosition));
if ((2 - noOfTrials) == 0) {
moveOn = "Move on to the next letter";
} else {
moveOn = "";
}
updateMessageToUser("<html><font color='red'>False!</font> we are expecting: "
+ currentWord.charAt(charPosition)
+ ". You have "
+ (2 - noOfTrials)
+ " trials!<br>"
+ moveOn
+ "</html>");
SCORE -= 2;
noOfTrials++;
if (noOfTrials > 2) {
noOfTrials = 0; // reset the number of trials and we are
// ready to move on to the next WHATEVER
charPosition++;
moveOn = "";
}
}
if (charPosition == currentWordSize) {
charPosition = 0;
POINTER++;
if (POINTER < 20) {
updateMessageToUser(" ");
go(POINTER);
if (correctLettersSoFar == currentWordSize) {
progressBarCtr++;
if (progressBarCtr <= 5) {
progressBar.setForeground(Color.red);
} else if (progressBarCtr > 5
&& progressBarCtr <= 10) {
progressBar.setForeground(Color.orange);
} else if (progressBarCtr > 10
&& progressBarCtr <= 15) {
progressBar.setForeground(Color.yellow);
} else if (progressBarCtr > 15
&& progressBarCtr <= 20) {
progressBar.setForeground(Color.green);
}
progressBar.setValue(progressBarCtr);
}
} else {
wowLabel.setText("WOW!!");
}
}
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
System.out.println("MouseReleased---");
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
});
nextButton.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
try {
getWordByBuffer();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
});
}
private ArrayList<String> fillData() {
ArrayList<String> list = new ArrayList<String>();
list.add("eins");
list.add("zwei");
list.add("drei");
list.add("vier");
list.add("funf");
list.add("sechs");
list.add("sieben");
list.add("acht");
list.add("neun");
list.add("zehn");
list.add("elf");
list.add("zwolf");
list.add("dreizehn");
list.add("vierzehn");
list.add("funfzehn");
list.add("sechzehn");
list.add("siebzehn");
list.add("achtzehn");
list.add("neunzehn");
list.add("zwanzig");
Collections.shuffle(list);
return list;
}
private JPanel createMainPanel() {
JPanel mainPanel = new JPanel();
mainPanel.setBackground(backgroundColor);
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
mainPanel.add(Box.createRigidArea(new Dimension(10, 10)));
wordToBeShownLabel = new JLabel();
mainPanel.add(wordToBeShownLabel);
mainPanel.add(Box.createRigidArea(new Dimension(10, 10)));
speakButton = new JButton("Press To Speak");
speakButton.setEnabled(true);
mainPanel.add(speakButton);
mainPanel.add(Box.createRigidArea(new Dimension(10, 10)));
messageToUser = new JLabel();
messageToUser.setText("");
mainPanel.add(messageToUser);
mainPanel.add(Box.createRigidArea(new Dimension(10, 20)));
scoreLabel = new JLabel();
mainPanel.add(scoreLabel);
mainPanel.add(Box.createRigidArea(new Dimension(10, 10)));
progressBar = new JProgressBar(0, 20);
progressBar.setValue(0);
progressBar.setStringPainted(true);
progressBar.setString("Progress");
mainPanel.add(progressBar);
mainPanel.add(Box.createRigidArea(new Dimension(10, 20)));
mainPanel.add(new JSeparator(SwingConstants.HORIZONTAL));
mainPanel.add(Box.createRigidArea(new Dimension(10, 10)));
nextButton = new JButton("Test the backend");
mainPanel.add(nextButton);
mainPanel.add(Box.createRigidArea(new Dimension(10, 10)));
wowLabel = new JLabel();
mainPanel.add(wowLabel);
return mainPanel;
}
/**
* Returns a JPanel with the given layout and custom background color.
*
* @param layout
* the LayoutManager to use for the returned JPanel
*
* @return a JPanel
*/
private JPanel getJPanel(LayoutManager layout) {
JPanel panel = getJPanel();
panel.setLayout(layout);
return panel;
}
/**
* Returns a JPanel with the custom background color.
*
* @return a JPanel
*/
private JPanel getJPanel() {
JPanel panel = new JPanel();
panel.setBackground(backgroundColor);
return panel;
}
public void go(int i) {
if (i <= 20) {
String germanWord = germanDictionary.get(i);
updateUI(germanWord, germanWord.length(), i);
} else {
nextButton.setEnabled(false);
}
}
private void updateUI(String germanWord, int length, int i) {
// TODO Auto-generated method stub
changeDisplayGermanWord(germanWord, i + 1);
updateScore(SCORE);
}
private void updateScore(int score) {
// TODO Auto-generated method stub
scoreLabel.setText("Score: " + score + "");
}
private void updateMessageToUser(String message) {
messageToUser.setText(message);
}
private void changeDisplayGermanWord(String germanWord, int wordNumber) {
// TODO Auto-generated method stub
if (wordColor == 0) {
this.wordToBeShownLabel.setText(String.format(
"<html>Word number %d: <font color='red'>%s</font></html>",
wordNumber, germanWord));
wordColor = 1;
} else {
this.wordToBeShownLabel
.setText(String
.format("<html>Word number %d: <font color='green'>%s</font></html>",
wordNumber, germanWord));
wordColor = 0;
}
}
private void getWordByBuffer() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String currentWord = germanDictionary.get(POINTER);
int currentWordSize = germanDictionary.get(POINTER).length();
int correctLettersSoFar = 0;
for (int i = 0; i < currentWordSize; i++) {
for (int j = 0; j < 3; j++) {
char c = (char) br.readLine().toCharArray()[0];
if (c == currentWord.charAt(i)) {
System.out.println("true!!!");
SCORE += 10;
correctLettersSoFar++;
break;
} else {
System.out.println("false! we are expecting:"
+ currentWord.charAt(i));
SCORE -= 2;
}
}
}
if (correctLettersSoFar == currentWordSize) {
progressBarCtr++;
if (progressBarCtr <= 5) {
progressBar.setForeground(Color.red);
} else if (progressBarCtr > 5 && progressBarCtr <= 10) {
progressBar.setForeground(Color.orange);
} else if (progressBarCtr > 10 && progressBarCtr <= 15) {
progressBar.setForeground(Color.yellow);
} else if (progressBarCtr > 15 && progressBarCtr <= 20) {
progressBar.setForeground(Color.green);
}
progressBar.setValue(progressBarCtr);
}
POINTER++;
if (POINTER > 20) {
wowLabel.setText("WOW!");
} else {
go(POINTER);
}
}
public static void main(String[] args) throws IOException {
BuildRecSys x = new BuildRecSys();
x.setVisible(true);
x.go(POINTER);
}
}