-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNegationHandling.java
142 lines (106 loc) · 4.25 KB
/
NegationHandling.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
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class NegationHandling {
public static void main(String[] args) throws FileNotFoundException, IOException {
ArrayList<ArrayList<String>> posTrainSet = new ArrayList<ArrayList<String>>();
ArrayList<ArrayList<String>> negTrainSet = new ArrayList<ArrayList<String>>();
posTrainSet = StanfordLemmatizer.getTrainingDataP();
negTrainSet = StanfordLemmatizer.getTrainingDataN();
System.out.println("Pos Train Set");
printTrainingSet(posTrainSet);
System.out.println("Neg Train Set");
printTrainingSet(negTrainSet);
HashMap<String, Integer> posCounts = new HashMap<String, Integer>();
HashMap<String, Integer> negCounts = new HashMap<String, Integer>();
buildTrainingSet(posTrainSet,posCounts,negCounts);
buildTrainingSet(negTrainSet,negCounts,posCounts);
System.out.println("Pos Hashmap");
printMap(posCounts);
System.out.println("Neg Hashmap");
printMap(negCounts);
//Saving in Pos File
String fileName = "posMap.properties";
saveMapToFile(posCounts,fileName);
//Saving in Neg File
fileName = "negMap.properties";
saveMapToFile(negCounts,fileName);
}
private static void printTrainingSet(ArrayList<ArrayList<String>> TrainSet) {
// TODO Auto-generated method stub
System.out.println("Map Size: "+ TrainSet.size());
for(int i = 0 ; i < TrainSet.size() ; i++) {
ArrayList<String> currentReview = TrainSet.get(i);
//now iterate on the current list
for (int j = 0; j < currentReview.size(); j++) {
String s = currentReview.get(j);
System.out.print(s+" ");
}
System.out.println();
}
}
private static void buildTrainingSet(ArrayList<ArrayList<String>> posTrainSet, HashMap<String, Integer> hashMap1,
HashMap<String, Integer> hashMap2)
{
// TODO Auto-generated method stub
for (ArrayList<String> currentReview : posTrainSet)
{
int negation = 0;
for(String word : currentReview)
{
//ADD OTHER NEGATIVE WORDS.ALSO CONSIDER NOT ONLY AND NEITHER NOR
if(word.equals("not") || word.equals("but") || word.equals("no") || word.equals("never"))
{
negation = 1- negation;
}
else if(word.equals("."))
{
negation =0;
}
else if(negation == 0)
{
Integer count = hashMap1.get(word);
if (count == null) {
count = 0;
}
hashMap1.put(word, count + 1);
//ADD TO NEGATIVE NOT_WORD
String negForm = "NOT_"+ word;
count = hashMap2.get(negForm);
if (count == null) {
count = 0;
}
hashMap2.put(negForm, count + 1);
}
else
{
String negForm = "NOT_"+ word;
Integer count = hashMap1.get(negForm);
if (count == null) {
count = 0;
}
hashMap1.put(negForm, count + 1);
}
}
}
}
private static void saveMapToFile(HashMap<String, Integer> posCounts, String fileName) throws FileNotFoundException, IOException {
// TODO Auto-generated method stub
Properties properties = new Properties();
for (Map.Entry<String,Integer> entry : posCounts.entrySet()) {
properties.put(entry.getKey(), entry.getValue().toString());
}
properties.store(new FileOutputStream(fileName), null);
}
private static void printMap(HashMap<String, Integer> hashMap) {
// TODO Auto-generated method stub
for (String key : hashMap.keySet()) {
System.out.println("Key: " + key + " Value: " + hashMap.get(key));
}
System.out.println("Total features in Hashmap: "+ hashMap.size());
}
}