-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prediction.java
executable file
·274 lines (247 loc) · 10.1 KB
/
Prediction.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
import com.aliasi.util.Files;
import com.aliasi.classify.Classification;
import com.aliasi.classify.Classified;
import com.aliasi.classify.DynamicLMClassifier;
import com.aliasi.lm.NGramProcessLM;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import edu.illinois.cs.cogcomp.lbj.pos.POSTagger;
import LBJ2.nlp.seg.Token;
import LBJ2.nlp.SentenceSplitter;
import LBJ2.nlp.WordSplitter;
import LBJ2.nlp.seg.PlainToTokenParser;
import LBJ2.parse.ChildrenFromVectors;
public class Prediction {
File mPolarityDir;
String[] mCategories;
DynamicLMClassifier<NGramProcessLM> mClassifier;
File mPolarityDir1;
String[] mCategories1;
DynamicLMClassifier<NGramProcessLM> mClassifier1;
Prediction(String[] args) {
System.out.println("\nBASIC POLARITY DEMO");
mPolarityDir = new File("C:\\Users\\Arunima\\Desktop\\test_predictor2");
System.out.println("\nData Directory=" + mPolarityDir);
mCategories = mPolarityDir.list();
int nGram = 8;
mClassifier
= DynamicLMClassifier
.createNGramProcess(mCategories,nGram);
mPolarityDir1 = new File("C:\\Users\\Arunima\\Desktop\\test_predictor2");
System.out.println("\nData Directory=" + mPolarityDir1);
mCategories1 = mPolarityDir1.list();
int nGram1 = 8;
mClassifier1
= DynamicLMClassifier
.createNGramProcess(mCategories1,nGram1);
}
void run() throws Exception {
train();
evaluate();
trainadj();
evaluateadj();
}
private static String onlyadjectives(String filename)
{
String onlyadj="";
POSTagger tagger = new POSTagger();
PlainToTokenParser parser =
new PlainToTokenParser(
new WordSplitter(
new SentenceSplitter(filename)));
Token w = (Token) parser.next();
while(w!=null){
String tag = tagger.discreteValue(w);
if(tag.equals("JJ")||tag.equals("JJS")||tag.equals("JJR"))
{
onlyadj+=w.form+" ";
//System.out.println(w.form+" "+tag);
}
w = (Token) parser.next();
}
// System.out.println(onlyadj);
return onlyadj;
}
private static String sendPost(String revtext) throws Exception {
final String USER_AGENT = "Mozilla/5.0";
String url = "http://text-processing.com/api/sentiment/";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
// con.setRequestProperty("User-Agent", USER_AGENT);
//con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "text="+revtext;
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
if(responseCode!=200)
{
System.out.println("RESPONSE ERROR"+responseCode);
System.exit(0);
}
//System.out.println("\nSending 'POST' request to URL : " + url);
//System.out.println("Post parameters : " + urlParameters);
//System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
//System.out.println(response.toString());
String s = "["+response+"]";
Object ob=JSONValue.parse(s);
JSONArray array=(JSONArray)ob;
//System.out.println("=========================");
//System.out.println(array.get(0));
JSONObject ob2=(JSONObject)array.get(0);
return ob2.get("label").toString();
}
boolean isTrainingFile(File file) {
int beg = file.getName().indexOf("(");
int end = file.getName().indexOf(")");
int num = Integer.parseInt(file.getName().substring(beg+1, end));
return (num%2!= 0); // test on fold 9
}
void train() throws IOException {
int numTrainingCases = 0;
int numTrainingChars = 0;
System.out.println("\nTraining.");
for (int i = 0; i < mCategories.length; ++i) {
String category = mCategories[i];
Classification classification
= new Classification(category);
File file = new File(mPolarityDir,mCategories[i]);
File[] trainFiles = file.listFiles();
for (int j = 0; j < trainFiles.length; ++j) {
File trainFile = trainFiles[j];
if (isTrainingFile(trainFile)) {
++numTrainingCases;
String review = Files.readFromFile(trainFile,"ISO-8859-1");
numTrainingChars += review.length();
Classified<CharSequence> classified
= new Classified<CharSequence>(review,classification);
mClassifier.handle(classified);
}
}
}
System.out.println(" # Training Cases=" + numTrainingCases);
System.out.println(" # Training Chars=" + numTrainingChars);
}
void evaluate() throws Exception {
System.out.println("\nEvaluating.");
int numTests = 0;
int numCorrect = 0;
int numcorrectother =0;
for (int i = 0; i < mCategories.length; ++i) {
String category = mCategories[i];
File file = new File(mPolarityDir,mCategories[i]);
File[] trainFiles = file.listFiles();
for (int j = 0; j < trainFiles.length; ++j) {
File trainFile = trainFiles[j];
if (!isTrainingFile(trainFile)) {
String review = Files.readFromFile(trainFile,"ISO-8859-1");
++numTests;
Classification classification
= mClassifier.classify(review);
if (classification.bestCategory().equals(category))
++numCorrect;
if(sendPost(review).equals(category))
++numcorrectother;
}
}
}
System.out.println(" # Test Cases=" + numTests);
System.out.println(" # Correct=" + numCorrect);
System.out.println(" % Correct="
+ ((double)numCorrect)/(double)numTests);
System.out.println(" % Correctother="
+ ((double)numcorrectother)/(double)numTests);
}
void trainadj() throws IOException {
int numTrainingCases = 0;
int numTrainingChars = 0;
System.out.println("\nTraining.");
for (int i = 0; i < mCategories1.length; ++i) {
String category = mCategories1[i];
Classification classification
= new Classification(category);
File file = new File(mPolarityDir1,mCategories1[i]);
File[] trainFiles = file.listFiles();
for (int j = 0; j < trainFiles.length; ++j) {
File trainFile = trainFiles[j];
if (isTrainingFile(trainFile)) {
++numTrainingCases;
// String review = Files.readFromFile(trainFile,"ISO-8859-1");
String review = onlyadjectives(trainFile.getAbsolutePath());
numTrainingChars += review.length();
Classified<CharSequence> classified
= new Classified<CharSequence>(review,classification);
mClassifier1.handle(classified);
}
}
}
System.out.println(" # Training Cases=" + numTrainingCases);
System.out.println(" # Training Chars=" + numTrainingChars);
}
void evaluateadj() throws Exception {
System.out.println("\nEvaluating.");
int numTests = 0;
int numCorrect = 0;
int numcorrectother =0;
for (int i = 0; i < mCategories1.length; ++i) {
String category = mCategories1[i];
File file = new File(mPolarityDir1,mCategories1[i]);
File[] trainFiles = file.listFiles();
for (int j = 0; j < trainFiles.length; ++j) {
File trainFile = trainFiles[j];
if (!isTrainingFile(trainFile)) {
//String review = Files.readFromFile(trainFile,"ISO-8859-1");
String review = onlyadjectives(trainFile.getAbsolutePath());
if(review.length()==0)
{
review="ABCD";
}
++numTests;
Classification classification
= mClassifier1.classify(review);
if (classification.bestCategory().equals(category))
++numCorrect;
if(sendPost(review).equals(category))
++numcorrectother;
}
}
}
System.out.println(" # Test Cases=" + numTests);
System.out.println(" # Correct=" + numCorrect);
System.out.println(" % Correct="
+ ((double)numCorrect)/(double)numTests);
System.out.println(" % Correctother="
+ ((double)numcorrectother)/(double)numTests);
}
public static void main(String[] args) {
try {
//onlyadjectives("C:\\Users\\Arunima\\Desktop\\test_predictor\\neutral\\__0G7C28bnbwxdtanv-1Bg(2728)");
new Prediction(args).run();
} catch (Throwable t) {
System.out.println("Thrown: " + t);
t.printStackTrace(System.out);
}
}
}