-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerspectiveClassification.java
More file actions
214 lines (170 loc) · 5.11 KB
/
Copy pathPerspectiveClassification.java
File metadata and controls
214 lines (170 loc) · 5.11 KB
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
package com.research.course.debate.LDA;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
public class PerspectiveClassification
{
/**
* @param args
*/
private List<String> unigrams ;
private List<String> bigrams ;
private List<String> trigrams ;
public PerspectiveClassification()
{
unigrams = new ArrayList<String>();
bigrams = new ArrayList<String>();
trigrams = new ArrayList<String>();
}
public void loadPerspectiveData ( String path, String file ) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader (
new FileInputStream( path + file) , "UTF8") );
BufferedWriter writer = new BufferedWriter(new FileWriter ( path + file + ".fv") );
//header
String line = reader.readLine();
while (true)
{
line = reader.readLine();
if (line == null)
{
break;
}
String features[] = line.split("\t");
String debate = ExtractDebateName(features[0].trim());
String firstSpeech = features[2].trim().toLowerCase();
firstSpeech = firstSpeech.substring(1,firstSpeech.length()-1);
List<String> firstTopics = ExtractTopics (features[3]);
String firstParty = firstSpeech.split("\\|")[0];
String firstVote = firstSpeech.split("\\|")[1];
firstSpeech = firstSpeech.split("\\|")[2];
List<Integer> trigramFirstSpeechFeatures = generateFeatures(firstSpeech);
String fv = generateFeatureString(trigramFirstSpeechFeatures);
fv = convert(firstParty) + " " + fv ;
System.out.println(fv);
writer.write(fv);
writer.newLine();
String secondSpeech = features[4].trim().toLowerCase() ;
secondSpeech = secondSpeech.substring(1,secondSpeech.length()-1);
List<String> secondTopics = ExtractTopics (features[5]);
String secondParty = secondSpeech.split("\\|")[0];
String secondVote = secondSpeech.split("\\|")[1];
secondSpeech = secondSpeech.split("\\|")[2];
List<Integer> trigramSecondSpeechFeatures = generateFeatures(secondSpeech);
fv = generateFeatureString(trigramSecondSpeechFeatures);
fv = convert(secondParty) + " " + fv ;
System.out.println(fv);
writer.write(fv);
writer.newLine();
}
reader.close();
writer.close();
}
private String convert(String party)
{
// TODO Auto-generated method stub
if(party.equalsIgnoreCase("d"))
{
return "1" ;
}
if(party.equalsIgnoreCase("r"))
{
return "2" ;
}
else
{
return null ;
}
}
private String generateFeatureString(
List<Integer> trigramSpeechFeatures)
{
// TODO Auto-generated method stub
StringBuffer ret = new StringBuffer();
for ( Integer trigram : trigramSpeechFeatures)
{
ret.append(trigram);
ret.append(":") ;
ret.append("1") ;
ret.append(" ");
}
return ret.toString().trim();
}
private List<Integer> generateFeatures(String firstSpeech)
{
// TODO Auto-generated method stub
List<Integer> trigramFeatures = new ArrayList<Integer>() ;
String words[] = firstSpeech.split("\\s+") ;
for ( int i = 0 ; i < words.length-2 ; i++ )
{
String one = words[i].trim();
String two = words[i+1].trim();
String three = words[i+2].trim() ;
checkUnigrams(one);
checkUnigrams(two);
checkUnigrams(three);
checkBigrams(one,two);
checkBigrams(two,three);
checkTrigrams(one,two,three);
int index = trigrams.indexOf(three+"|||"+two+"|||"+one) ;
if(!trigramFeatures.contains(index))
{
trigramFeatures.add(index) ;
}
}
java.util.Collections.sort(trigramFeatures) ;
return trigramFeatures;
}
private void checkTrigrams(String one, String two, String three) {
// TODO Auto-generated method stub
if(!trigrams.contains(three+"|||"+two+"|||"+one))
{
trigrams.add(three+"|||"+two+"|||"+one);
}
}
private void checkBigrams(String one, String two) {
// TODO Auto-generated method stub
if(!bigrams.contains(two+"|||"+one))
{
bigrams.add(two+"|||"+one);
}
}
private void checkUnigrams(String one)
{
// TODO Auto-generated method stub
if(!unigrams.contains(one))
{
unigrams.add(one);
}
}
private List<String> ExtractTopics(String topicString)
{
// TODO Auto-generated method stub
topicString = topicString.substring(2,topicString.length()-2);
List<String> topics = new ArrayList<String>(Arrays.asList(topicString.split(","))) ;
return topics;
}
private String ExtractDebateName(String debateName)
{
// TODO Auto-generated method stub
//./data/convote_v1.1/data_stage_one/mixed_set/426.txt
int index = debateName.lastIndexOf("/");
String debate = debateName.substring(index+1,debateName.length());
return debate;
}
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
String path = "./data/output/LDA/" ;
String file = "perspectives_121326_904.txt" ;
PerspectiveClassification povClassifierObj = new PerspectiveClassification();
povClassifierObj.loadPerspectiveData(path, file) ;
}
}