-
Notifications
You must be signed in to change notification settings - Fork 0
/
CyberTruckTwitterAnalysis.java
209 lines (193 loc) · 7.12 KB
/
CyberTruckTwitterAnalysis.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
package mj.cybertruck;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import opennlp.tools.doccat.DoccatModel;
import opennlp.tools.doccat.DocumentCategorizerME;
import opennlp.tools.doccat.DocumentSample;
import opennlp.tools.doccat.DocumentSampleStream;
import opennlp.tools.util.ObjectStream;
import opennlp.tools.util.PlainTextByLineStream;
public class CyberTruckTwitterAnalysis {
/**
* @param args
*/
public static int classifyNewText(DoccatModel sentimentModel, String input)
throws IOException {
DocumentCategorizerME myCategorizer = new DocumentCategorizerME(
sentimentModel);
double[] outcomes = myCategorizer.categorize(input);
return Integer.parseInt(myCategorizer.getBestCategory(outcomes));
}
public static List<String> getPositiveWords() throws IOException {
List<String> outputList = new ArrayList<String>();
BufferedReader br = new BufferedReader(new FileReader(
"C:\\positive-words.txt"));
String line = null;
while ((line = br.readLine()) != null) {
outputList.add(line);
}
br.close();
return outputList;
}
public static List<String> getNegativeWords() throws IOException {
List<String> outputList = new ArrayList<String>();
BufferedReader br = new BufferedReader(new FileReader(
"C:\\negative-words.txt"));
String line = null;
while ((line = br.readLine()) != null) {
outputList.add(line);
}
br.close();
return outputList;
}
public static List<String> getStopWords() throws IOException {
List<String> outputList = new ArrayList<String>();
BufferedReader br = new BufferedReader(new FileReader(
"C:\\nlp_en_stop_words.txt"));
String line = null;
while ((line = br.readLine()) != null) {
outputList.add(line);
}
br.close();
return outputList;
}
public static LinkedHashMap<String, Integer> sortHashMapByValues(
Map<String, Integer> wordCountMap) {
List<String> mapKeys = new ArrayList<String>(wordCountMap.keySet());
List<Integer> mapValues = new ArrayList<Integer>(wordCountMap.values());
Collections.sort(mapValues, Collections.reverseOrder());
Collections.sort(mapKeys);
LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
Iterator<Integer> valueIt = mapValues.iterator();
while (valueIt.hasNext()) {
Integer val = valueIt.next();
Iterator<String> keyIt = mapKeys.iterator();
while (keyIt.hasNext()) {
String key = keyIt.next();
Integer comp1 = wordCountMap.get(key);
Integer comp2 = val;
if (comp1.equals(comp2)) {
keyIt.remove();
sortedMap.put(key, val);
break;
}
}
}
return sortedMap;
}
public static void main(String[] args) throws IOException {
Map<String, Integer> wordCountMap = new HashMap<String, Integer>();
List<String> stopWordsList = getStopWords();
List<String> globalPositiveWordsList = getPositiveWords();
Map<String, Integer> positiveWordsMap = new HashMap<String, Integer>();
List<String> globalNegativeWordsList = getNegativeWords();
Map<String, Integer> negativeWordsMap = new HashMap<String, Integer>();
InputStream dataIn = new FileInputStream(
"C:\\tweets.txt");
ObjectStream<String> lineStream = new PlainTextByLineStream(dataIn,
"UTF-8");
ObjectStream<DocumentSample> sampleStream = new DocumentSampleStream(
lineStream);
DoccatModel sentimentModel = DocumentCategorizerME.train("en",
sampleStream);
int positiveCounts = 0, negativeCounts = 0;
BufferedReader br = new BufferedReader(new FileReader(
"C:\\CyberTruck_Tweets.csv"));
String line = null, cleanLine = null, mapKey = null;
String[] lineSplitter = null;
int i = 0;
while ((line = br.readLine()) != null) {
cleanLine = line
.replace("https:\\/\\/tco\\/yPc30\\xe2\\x80\\xa6", "")
.replace("\\xf0\\x9f\\x91\\x87", "").replaceAll("\n", " ")
.replace("https:\\/\\/tco\\/Ptc\\xe2\\x80\\xa6", "")
.replace("\\xf0\\x9f\\x91\\x8a\\xf0\\x9f\\x8f\\xbe", "")
.replace("https:\\/\\/tco\\/pCxyhkfBbD\"", "")
.replace("https:\\/\\/tco\\/6F4ZCUrl\\xe2\\x80\\xa6", "")
.replace("\\xe2\\x80\\x98", "")
.replace("\\xe2\\x80\\x99", "")
.replace("\\xe2\\x80\\xa6", "").replace("\"", "")
.replace("\\xe2\\x80\\x93", "")
.replace("https://tco/yPc30", "");
if (classifyNewText(sentimentModel, cleanLine) == 1)
positiveCounts++;
if (classifyNewText(sentimentModel, cleanLine) == 0)
negativeCounts++;
lineSplitter = cleanLine.split(" ");
for (i = 0; i < lineSplitter.length; i++) {
mapKey = lineSplitter[i].replaceAll("[\\.\\',\\?]", "");
if (!(stopWordsList.contains(mapKey)) && mapKey.length() > 3) {
if (wordCountMap.containsKey(mapKey))
wordCountMap.put(mapKey, wordCountMap.get(mapKey) + 1);
else
wordCountMap.put(mapKey, 1);
}
if (globalPositiveWordsList.contains(mapKey)) {
if (positiveWordsMap.containsKey(mapKey))
positiveWordsMap.put(mapKey,
positiveWordsMap.get(mapKey) + 1);
else
positiveWordsMap.put(mapKey, 1);
}
if (globalNegativeWordsList.contains(mapKey)) {
if (negativeWordsMap.containsKey(mapKey))
negativeWordsMap.put(mapKey,
negativeWordsMap.get(mapKey) + 1);
else
negativeWordsMap.put(mapKey, 1);
}
}
}
br.close();
LinkedHashMap<String, Integer> sortedWordCountMap = sortHashMapByValues(wordCountMap);
System.out.println("***TOP 10 MOST USED WORDS***");
int count = 0;
for (Entry<String, Integer> entry : sortedWordCountMap.entrySet()) {
// if (count < 10)
if (entry.getValue() > 10)
System.out.println(entry.getKey() + " - " + entry.getValue());
// count++;
}
System.out
.println("\n----------------------------------------------------------------");
System.out
.println("\n----------------------------------------------------------------");
System.out.println("***POSITIVE WORDS***");
count = 0;
LinkedHashMap<String, Integer> sortedPositiveWordsMap = sortHashMapByValues(positiveWordsMap);
for (Entry<String, Integer> entry : sortedPositiveWordsMap.entrySet()) {
if (count < 10)
System.out.println(entry.getKey() + " - " + entry.getValue());
count++;
}
System.out
.println("\n----------------------------------------------------------------");
System.out.println("***NEGATIVE WORDS***");
count = 0;
LinkedHashMap<String, Integer> sortedNegativeWordsMap = sortHashMapByValues(negativeWordsMap);
for (Entry<String, Integer> entry : sortedNegativeWordsMap.entrySet()) {
if (count < 10)
System.out.println(entry.getKey() + " - " + entry.getValue());
count++;
}
System.out
.println("\n----------------------------------------------------------------");
System.out.println("***POSITIVE SENTIMENT COUNT***");
System.out.print(positiveCounts);
System.out
.println("\n----------------------------------------------------------------");
System.out.println("***NEGATIVE SENTIMENT COUNT***");
System.out.print(negativeCounts);
}
}