1
- /** *****************************************************************************
1
+ /* *****************************************************************************
2
2
* Copyright (c) 2015-2019 Skymind, Inc.
3
3
*
4
4
* This program and the accompanying materials are made available under the
14
14
* SPDX-License-Identifier: Apache-2.0
15
15
******************************************************************************/
16
16
17
- /**-
18
- * This is a test program that uses word vector and trained network generated by PrepareWordVector.java and TrainNews.java
19
- * - Type or copy/paste news headline from news (indian news channel is preferred) and click on Check button
20
- * and see the predicted category right to the Check button
21
- * <p>
22
- * <b></b>KIT Solutions Pvt. Ltd. (www.kitsol.com)</b>
23
- */
24
-
25
17
package org .deeplearning4j .examples .recurrent .processnews ;
26
18
27
19
import org .deeplearning4j .examples .download .DownloaderUtility ;
37
29
import org .nd4j .linalg .indexing .INDArrayIndex ;
38
30
import org .nd4j .linalg .indexing .NDArrayIndex ;
39
31
32
+ import javax .swing .*;
40
33
import java .io .BufferedReader ;
41
34
import java .io .File ;
42
35
import java .io .FileReader ;
45
38
import java .util .logging .Level ;
46
39
import java .util .logging .Logger ;
47
40
41
+ /**-
42
+ * This is a test program that uses word vector and trained network generated by PrepareWordVector.java and TrainNews.java
43
+ * - Type or copy/paste news headline from news (indian news channel is preferred) and click on Check button
44
+ * and see the predicted category right to the Check button
45
+ * <p>
46
+ * <b></b>KIT Solutions Pvt. Ltd. (www.kitsol.com)</b>
47
+ */
48
48
public class TestNews extends javax .swing .JFrame {
49
49
private static WordVectors wordVectors ;
50
50
private static TokenizerFactory tokenizerFactory ;
51
- private static int maxLength = 8 ;
52
51
53
- // Variables declaration - do not modify
54
- private javax .swing .JButton jButton1 ;
55
- private javax .swing .JLabel jLabel1 ;
56
- private javax .swing .JLabel jLabel2 ;
57
52
private javax .swing .JLabel jLabel3 ;
58
- private javax .swing .JScrollPane jScrollPane1 ;
59
53
private javax .swing .JTextArea jTextArea1 ;
60
54
private static MultiLayerNetwork net ;
61
55
private static String dataLocalPath ;
62
56
63
- public TestNews () throws Exception {
57
+ private TestNews () throws Exception {
64
58
initComponents ();
65
59
dataLocalPath = DownloaderUtility .NEWSDATA .Download ();
66
60
}
@@ -70,16 +64,16 @@ public TestNews() throws Exception {
70
64
* WARNING: Do NOT modify this code. The content of this method is always
71
65
* regenerated by the Form Editor.
72
66
*/
73
- @ SuppressWarnings ("unchecked" )
74
67
// <editor-fold defaultstate="collapsed" desc="Generated Code">
75
68
private void initComponents () {
76
69
77
70
this .setTitle ("Predict News Category - KITS" );
78
- jLabel1 = new javax .swing .JLabel ();
79
- jScrollPane1 = new javax .swing .JScrollPane ();
71
+ javax . swing . JLabel jLabel1 = new javax .swing .JLabel ();
72
+ javax . swing . JScrollPane jScrollPane1 = new javax .swing .JScrollPane ();
80
73
jTextArea1 = new javax .swing .JTextArea ();
81
- jButton1 = new javax .swing .JButton ();
82
- jLabel2 = new javax .swing .JLabel ();
74
+ // Variables declaration - do not modify
75
+ javax .swing .JButton jButton1 = new javax .swing .JButton ();
76
+ javax .swing .JLabel jLabel2 = new javax .swing .JLabel ();
83
77
jLabel3 = new javax .swing .JLabel ();
84
78
85
79
setDefaultCloseOperation (javax .swing .WindowConstants .EXIT_ON_CLOSE );
@@ -91,11 +85,7 @@ private void initComponents() {
91
85
jScrollPane1 .setViewportView (jTextArea1 );
92
86
93
87
jButton1 .setText ("Check" );
94
- jButton1 .addActionListener (new java .awt .event .ActionListener () {
95
- public void actionPerformed (java .awt .event .ActionEvent evt ) {
96
- jButton1ActionPerformed (evt );
97
- }
98
- });
88
+ jButton1 .addActionListener (this ::jButton1ActionPerformed );
99
89
100
90
jLabel2 .setText ("Category" );
101
91
@@ -142,10 +132,6 @@ private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
142
132
INDArray fet = testNews .getFeatures ();
143
133
INDArray predicted = net .output (fet , false );
144
134
long [] arrsiz = predicted .shape ();
145
- double crimeTotal = 0 ;
146
- double politicsTotal = 0 ;
147
- double bollywoodTotal = 0 ;
148
- double developmentTotal = 0 ;
149
135
150
136
File categories = new File (dataLocalPath , "LabelledNews/categories.txt" );
151
137
@@ -159,7 +145,7 @@ private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
159
145
}
160
146
161
147
try (BufferedReader brCategories = new BufferedReader (new FileReader (categories ))) {
162
- String temp = "" ;
148
+ String temp ;
163
149
List <String > labels = new ArrayList <>();
164
150
while ((temp = brCategories .readLine ()) != null ) {
165
151
labels .add (temp );
@@ -171,7 +157,7 @@ private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
171
157
}
172
158
}
173
159
174
- public static void main (String args [] ) throws Exception {
160
+ public static void main (String [] args ) throws Exception {
175
161
176
162
try {
177
163
for (javax .swing .UIManager .LookAndFeelInfo info : javax .swing .UIManager .getInstalledLookAndFeels ()) {
@@ -180,32 +166,23 @@ public static void main(String args[]) throws Exception{
180
166
break ;
181
167
}
182
168
}
183
- } catch (ClassNotFoundException ex ) {
184
- Logger .getLogger (TestNews .class .getName ()).log (Level .SEVERE , null , ex );
185
- } catch (InstantiationException ex ) {
186
- Logger .getLogger (TestNews .class .getName ()).log (Level .SEVERE , null , ex );
187
- } catch (IllegalAccessException ex ) {
188
- Logger .getLogger (TestNews .class .getName ()).log (Level .SEVERE , null , ex );
189
- } catch (javax .swing .UnsupportedLookAndFeelException ex ) {
169
+ } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex ) {
190
170
Logger .getLogger (TestNews .class .getName ()).log (Level .SEVERE , null , ex );
191
171
}
192
172
TestNews test = new TestNews ();
193
173
test .setVisible (true );
194
174
195
- try {
196
- tokenizerFactory = new DefaultTokenizerFactory ();
197
- tokenizerFactory .setTokenPreProcessor (new CommonPreprocessor ());
198
- net = MultiLayerNetwork .load (new File (dataLocalPath ,"NewsModel.net" ), true );
199
- wordVectors = WordVectorSerializer .readWord2VecModel (new File (dataLocalPath ,"NewsWordVector.txt" ));
200
- } catch (Exception e ) {
201
-
202
- }
175
+ tokenizerFactory = new DefaultTokenizerFactory ();
176
+ tokenizerFactory .setTokenPreProcessor (new CommonPreprocessor ());
177
+ net = MultiLayerNetwork .load (new File (dataLocalPath ,"NewsModel.net" ), true );
178
+ wordVectors = WordVectorSerializer .readWord2VecModel (new File (dataLocalPath ,"NewsWordVector.txt" ));
203
179
}
204
180
181
+ // One news story gets transformed into a dataset with one element.
182
+ @ SuppressWarnings ("DuplicatedCode" )
205
183
private static DataSet prepareTestData (String i_news ) {
206
184
List <String > news = new ArrayList <>(1 );
207
185
int [] category = new int [1 ];
208
- int currCategory = 0 ;
209
186
news .add (i_news );
210
187
211
188
List <List <String >> allTokens = new ArrayList <>(news .size ());
@@ -246,7 +223,6 @@ private static DataSet prepareTestData(String i_news) {
246
223
labelsMask .putScalar (new int []{i , lastIdx - 1 }, 1.0 );
247
224
}
248
225
249
- DataSet ds = new DataSet (features , labels , featuresMask , labelsMask );
250
- return ds ;
226
+ return new DataSet (features , labels , featuresMask , labelsMask );
251
227
}
252
228
}
0 commit comments