-
Notifications
You must be signed in to change notification settings - Fork 0
/
RhymeFinder.java
294 lines (261 loc) · 8.63 KB
/
RhymeFinder.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
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JTextField;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
//<applet code="RhymeFinder$Applet.class" archive="rhyme.jar" width="100" height="50"></applet>
public final class RhymeFinder extends JFrame {
private final JTextField queryMora = new JTextField();
private final JTextField blacklist = new JTextField();
private final JEditorPane strict = new JEditorPane("text/html", "");
private final JEditorPane head = new JEditorPane("text/html", "");
private final JEditorPane tail = new JEditorPane("text/html", "");
private final JEditorPane internal = new JEditorPane("text/html", "");
private final JCheckBox[] features = new JCheckBox[Dictionary.NAMES.length];
private final Dictionary dictionary = new Dictionary.Builder().build();
private Word[] strictResult;
private Word[] headResult;
private Word[] tailResult;
private Word[] internalResult;
private ExecutorService exec = Executors.newFixedThreadPool(3);
private RhymeFinder(final boolean isApplet) {
super("RhymeFinder");
setDefaultCloseOperation(isApplet ? JFrame.HIDE_ON_CLOSE : JFrame.EXIT_ON_CLOSE);
final ActionListener findAction = new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
if (strict.isEnabled() && head.isEnabled() && tail.isEnabled() && internal.isEnabled()) {
strict .setEnabled(false);
head .setEnabled(false);
tail .setEnabled(false);
internal.setEnabled(false);
repaint();
findRhyme();
showResult();
}
}
};
final ActionListener showAction = new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
showResult();
}
};
final JPanel north = new JPanel(new GridLayout(1, 2));
{
queryMora.addActionListener(findAction);
blacklist.addActionListener(showAction);
north.add(queryMora);
north.add(blacklist);
}
final JPanel center = new JPanel(new GridLayout(1, 4));
{
strict .setEditable(false);
head .setEditable(false);
tail .setEditable(false);
internal.setEditable(false);
center.add(new JScrollPane(strict,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
center.add(new JScrollPane(head,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
center.add(new JScrollPane(tail,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
center.add(new JScrollPane(internal,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
}
final JPanel south = new JPanel(new GridLayout(8, 4));
{
for (int i = 0, size = Dictionary.NAMES.length; i < size; ++i) {
features[i] = new JCheckBox(Dictionary.NAMES[i]);
features[i].addActionListener(showAction);
south.add(features[i]);
}
}
final JPanel main = new JPanel(new BorderLayout());
main.add(north , BorderLayout.NORTH );
main.add(center, BorderLayout.CENTER);
main.add(south , BorderLayout.SOUTH );
add(main);
pack();
}
private void findRhyme() {
final String queryText = this.queryMora.getText().trim();
if (queryText.isEmpty()) {
return;
}
final Mora[] queryMora = Word.toMora(queryText);
if (queryMora.length <= 0) {
return;
}
final Word[] strictResult = dictionary.match (queryMora);
final Word[] headResult = dictionary.matchPrefix (queryMora);
final Word[] tailResult = dictionary.matchPostfix (queryMora);
final Word[] internalResult = dictionary.matchInternal(queryMora);
this.strictResult = strictResult;
this.tailResult = tailResult;
this.headResult = headResult;
this.internalResult = internalResult;
}
private int getFeature() {
int feature = 0;
for (int i = 0, size = features.length; i < size; ++i) {
if (features[i].isSelected()) {
feature |= (1 << i);
}
}
return feature;
}
private void showResult() {
final Mora[] blacklist = Word.toMora(this.blacklist.getText().trim());
final int feature = this.getFeature();
final String beginHtml = "<html><head></head><body><dl>";
final String endHtml = "</dl></body></html>";
// 押韻
if (strictResult != null) {
exec.execute(new Runnable() {
@Override
public void run() {
final StringBuilder builder = new StringBuilder(beginHtml);
for (final Word word : strictResult) {
if (!word.contains(blacklist)) {
builder.append(word.toHTML(feature));
}
}
builder.append(endHtml);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
strict.setEnabled(true);
strict.setText(builder.toString());
strict.setCaretPosition(0);
strict.repaint();
}
});
}
});
} else {
strict.setEnabled(true);
}
// 頭韻
if (headResult != null) {
exec.execute(new Runnable() {
@Override
public void run() {
final StringBuilder builder = new StringBuilder(beginHtml);
for (final Word word : headResult) {
if (!word.contains(blacklist)) {
builder.append(word.toHTML(feature));
}
}
builder.append(endHtml);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
head.setEnabled(true);
head.setText(builder.toString());
head.setCaretPosition(0);
head.repaint();
}
});
}
});
} else {
head.setEnabled(true);
}
// 脚韻
if (tailResult != null) {
exec.execute(new Runnable() {
@Override
public void run() {
final StringBuilder builder = new StringBuilder(beginHtml);
for (final Word word : tailResult) {
if (!word.contains(blacklist)) {
builder.append(word.toHTML(feature));
}
}
builder.append(endHtml);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
tail.setEnabled(true);
tail.setText(builder.toString());
tail.setCaretPosition(0);
tail.repaint();
}
});
}
});
} else {
tail.setEnabled(true);
}
// 中間韻
if (internalResult != null) {
exec.execute(new Runnable() {
@Override
public void run() {
final StringBuilder builder = new StringBuilder(beginHtml);
for (final Word word : internalResult) {
if (!word.contains(blacklist)) {
builder.append(word.toHTML(feature));
}
}
builder.append(endHtml);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
internal.setEnabled(true);
internal.setText(builder.toString());
internal.setCaretPosition(0);
internal.repaint();
}
});
}
});
} else {
internal.setEnabled(true);
}
}
public static void main(String[] args) throws Exception {
showFrame(new RhymeFinder(false));
}
public static final class Applet extends java.applet.Applet {
private final JFrame frame = new RhymeFinder(true);
public Applet() {
setLayout(new BorderLayout());
final JButton button = new JButton("RhymeFinder");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
showFrame(frame);
}
});
add(button);
}
}
private static void showFrame(final JFrame frame) {
if (SwingUtilities.isEventDispatchThread()) {
frame.setVisible(true);
} else {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame.setVisible(true);
}
});
}
}
}