-
Notifications
You must be signed in to change notification settings - Fork 0
/
StanfordLemmatizer.java
267 lines (178 loc) · 9.49 KB
/
StanfordLemmatizer.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
import java.io.File;
import java.io.IOException;
import java.util.*;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.util.CoreMap;
import edu.stanford.nlp.ling.*;
import edu.stanford.nlp.ling.CoreAnnotations.*;
import edu.stanford.nlp.tagger.maxent.MaxentTagger;
public class StanfordLemmatizer
{
public static ArrayList<ArrayList<String>> getTrainingDataPos() throws IOException
{
String posTrainPath = "C:\\Users\\Anand\\git\\Project\\Sentiment Test\\aclImdb\\train\\pos";
File folder = new File(posTrainPath);
ArrayList<Review> posData = new ArrayList<Review>();
ReadTweets.listFilesForFolder(folder, posData);
Properties props = new Properties();
props.put("annotators", "tokenize, ssplit, pos, lemma");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props, false);
ArrayList<ArrayList<String>> collectionLemma = new ArrayList<ArrayList<String>>();
int posSize = posData.size();
for(int i=0;i<posSize;i++)
{
Annotation document = pipeline.process(posData.get(i).review);
ArrayList<String> currentReview = new ArrayList<String>();
for(CoreMap sentence: document.get(SentencesAnnotation.class))
{
for(CoreLabel token: sentence.get(TokensAnnotation.class))
{
String lemma = token.get(LemmaAnnotation.class);
lemma = lemma.replaceAll("\\s+","");
String[] tempLemma = lemma.split("\\.|[-,!?/]");
for(String word : tempLemma)
{
currentReview.add(word);
}
}
}
collectionLemma.add(currentReview);
System.out.println("Pos Lemmatization completed for "+(i+1)+" reviews");
}
/*
System.out.println("Check the stop words:");
for(int i = 0 ; i < collectionFinalPos.size() ; i++) {
ArrayList<String> printList = collectionFinalPos.get(i);
//now iterate on the current list
for (int j = 0; j < printList.size(); j++) {
String s = printList.get(j);
System.out.println(s);
}
}
*/
return collectionLemma;
}
public static ArrayList<ArrayList<String>> getTrainingDataNeg() throws IOException
{
String negTrainPath = "C:\\Users\\Anand\\git\\Project\\Sentiment Test\\aclImdb\\train\\neg";
File negFolder = new File(negTrainPath);
ArrayList<Review> negData = new ArrayList<Review>();
ReadTweets.listFilesForFolder(negFolder, negData);
Properties props = new Properties();
props.put("annotators", "tokenize, ssplit, pos, lemma");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props, false);
ArrayList<ArrayList<String>> collectionLemmaNeg = new ArrayList<ArrayList<String>>();
int negSize = negData.size();
for(int i=0;i<negSize;i++)
{
Annotation document = pipeline.process(negData.get(i).review);
ArrayList<String> currentList = new ArrayList<String>();
for(CoreMap sentence: document.get(SentencesAnnotation.class))
{
for(CoreLabel token: sentence.get(TokensAnnotation.class))
{
String lemma = token.get(LemmaAnnotation.class);
lemma = lemma.replaceAll("\\s+","");
String[] tempLemma = lemma.split("\\.|[-,!?/]");
for(String word : tempLemma)
{
currentList.add(word);
}
}
}
collectionLemmaNeg.add(currentList);
System.out.println("Neg Lemmatization completed for "+(i+1)+" reviews");
}
return collectionLemmaNeg;
}
public static ArrayList<ArrayList<String>> getPosTagging(ArrayList<ArrayList<String>> collectionLemma)
{
MaxentTagger tagger = new MaxentTagger(
"taggers/english-left3words-distsim.tagger");
ArrayList<ArrayList<String>> collectionPos = new ArrayList<ArrayList<String>>();
//System.out.println("Check the stop words other than adverbs and adjectives:");
for(int i = 0 ; i < collectionLemma.size() ; i++) {
ArrayList<String> LemmatizedReview = collectionLemma.get(i);
ArrayList<String> currentReviewPOS = new ArrayList<String>();
//now iterate on the current list
for (int j = 0; j < LemmatizedReview.size(); j++)
{
String s = LemmatizedReview.get(j);
String taggedWord = tagger.tagString(s);
currentReviewPOS.add(taggedWord);
}
collectionPos.add(currentReviewPOS);
System.out.println("POS completed for "+(i+1)+" reviews");
}
return collectionPos;
}
public static ArrayList<ArrayList<String>> getFilteredList(ArrayList<ArrayList<String>> collectionPos,ArrayList<ArrayList<String>> collectionLemma)
throws java.lang.StringIndexOutOfBoundsException
{
ArrayList<ArrayList<String>> collectionFinalPos = new ArrayList<ArrayList<String>>();
for(int i = 0 ; i < collectionPos.size() ; i++) {
System.out.println("Filtering started for review " +i);
ArrayList<String> printList = collectionPos.get(i);
ArrayList<String> currentListLemma = new ArrayList<String>();
ArrayList<String> currentListFinal = new ArrayList<String>();
currentListLemma=collectionLemma.get(i);
for (int j = 0; j < printList.size(); j++) {
String s = printList.get(j);
String curstr;
String no="no";
//int pos = s.indexOf("_");
if((curstr=currentListLemma.get(j)).equals(no))
{
curstr=currentListLemma.get(j);
currentListFinal.add(curstr);
}
else if( (s.substring(s.indexOf('_')+1,s.length())!="NNP") ||(s.substring(s.indexOf('_')+1,s.length())=="NNS"))
//else if((s.charAt(pos+1) == 'N' && (s.charAt(pos+2) == 'N') && (s.charAt(pos+3) != 'P'))|| ((s.charAt(pos+1) == 'N') &&(s.charAt(pos+3)=='S')) )
{
curstr=currentListLemma.get(j);
currentListFinal.add(curstr);
}
else if( s.substring(s.indexOf('_')+1,s.length())=="JJ")
//else if((s.charAt(pos+1) == 'J' && (s.charAt(pos+2) == 'J')))
{
curstr=currentListLemma.get(j);
currentListFinal.add(curstr);
}
else if( (s.substring(s.indexOf('_')+1,s.indexOf('_')+2)=="V"))
//else if((s.charAt(pos+1) == 'V' ))
{
curstr=currentListLemma.get(j);
currentListFinal.add(curstr);
}
else if( (s.substring(s.indexOf('_')+1,s.indexOf('_')+3)=="RB"))
// else if((s.charAt(pos+1) == 'R' && (s.charAt(pos+2) == 'B')))
{
curstr=currentListLemma.get(j);
currentListFinal.add(curstr);
}
}
collectionFinalPos.add(currentListFinal);
}
return collectionFinalPos;
}
public static ArrayList<ArrayList<String>> getTrainingDataP() throws IOException
{
ArrayList<ArrayList<String>> collectionLemma = new ArrayList<ArrayList<String>>();
ArrayList<ArrayList<String>> collectionPos = new ArrayList<ArrayList<String>>();
ArrayList<ArrayList<String>> collectionFinalPos = new ArrayList<ArrayList<String>>();
collectionLemma=StanfordLemmatizer.getTrainingDataPos();
collectionPos = StanfordLemmatizer.getPosTagging(collectionLemma);
collectionFinalPos=StanfordLemmatizer.getFilteredList(collectionPos, collectionLemma);
return collectionFinalPos;
}
public static ArrayList<ArrayList<String>> getTrainingDataN() throws IOException
{
ArrayList<ArrayList<String>> collectionLemma = new ArrayList<ArrayList<String>>();
ArrayList<ArrayList<String>> collectionPos = new ArrayList<ArrayList<String>>();
ArrayList<ArrayList<String>> collectionFinalPos = new ArrayList<ArrayList<String>>();
collectionLemma=StanfordLemmatizer.getTrainingDataNeg();
collectionPos = StanfordLemmatizer.getPosTagging(collectionLemma);
collectionFinalPos=StanfordLemmatizer.getFilteredList(collectionPos, collectionLemma);
return collectionFinalPos;
}
}