-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWekaWriter.java
More file actions
189 lines (151 loc) · 4.55 KB
/
Copy pathWekaWriter.java
File metadata and controls
189 lines (151 loc) · 4.55 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
package com.deft.sarcasm.features;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
//import com.rutgers.justification.pdtb.classifier.DiscFeaturesForJustiData.FEAT_TYPE;
public class WekaWriter
{
private int featureSize;
private Map<String, List<Map<Integer, Double>>> allFeatureMap;
private Set<String> allLabels;
private int startingPoint;
private List<String> features;
public WekaWriter()
{
}
public void setStartingPointForFeaturePostion ( int startingPoint)
{
this.startingPoint = startingPoint ;
}
public void setFeatureSize ( int featureSize)
{
this.featureSize = featureSize ;
}
public void setFeatureMap (Map<String, List<Map<Integer, Double>>> allFeatureMap )
{
this.allFeatureMap = allFeatureMap ;
}
public void writeWekaArffFile ( String path, String trainingFile ) throws IOException
{
BufferedWriter wekaLexPragBoWPunchWriter = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(path + "/" + trainingFile + ".arff"),
"UTF8"));
//we need to find the max id of unigrams
int allFeatureSize = 0 ;
/*
if ( type != null)
{
if ( type.equals(FEAT_TYPE.DISC))
{
allFeatureSize = 150 ;
}
}
else
{
*/
allFeatureSize = featureSize ;
// }
// allFeatureSize += bigramList.size() ;
// allFeatureSize = allFeatureSize ;// + 10 ; //lex and puncs features
Set<String> labels = allLabels ;
//prologue of wiki files
writePrologueOfWeka(wekaLexPragBoWPunchWriter,labels,allFeatureSize);
wekaLexPragBoWPunchWriter.newLine();
wekaLexPragBoWPunchWriter.write("@data");
wekaLexPragBoWPunchWriter.newLine();
for ( String key : allFeatureMap.keySet() )
{
List<Map<Integer,Double>> fvs = allFeatureMap.get(key);
int index = 0 ;
for ( Map<Integer,Double> fv : fvs )
{
StringBuffer ret = new StringBuffer() ;
//1,1,1,1,0,0,1,0,1,1,1,1,0,1,0,1,0,0,...0,0,0,'sarcasm'
// for ( int i = 1 ; i <= allFeatureSize ; i ++ )
for ( int i = startingPoint ; i <= startingPoint+allFeatureSize ; i ++ )
{
if(fv.containsKey(i))
{
Double value = fv.get(i);
{
// value = 1.0 ; //1 = only present. if we do not set one - means using direct value
}
ret.append("1");
ret.append(",");
}
else
{
ret.append("0");
ret.append(",");
}
}
ret.append("'");
ret.append(key);
ret.append("'");
wekaLexPragBoWPunchWriter.write(ret.toString());
wekaLexPragBoWPunchWriter.newLine();
index++ ;
}
}
wekaLexPragBoWPunchWriter.close();
}
private void writePrologueOfWeka(BufferedWriter wekaLexPragBoWPunchWriter,Set<String>labels,
int size) throws IOException
{
// TODO Auto-generated method stub
Calendar now = GregorianCalendar.getInstance();
wekaLexPragBoWPunchWriter.write("% Weka ARFF file");
wekaLexPragBoWPunchWriter.newLine();
wekaLexPragBoWPunchWriter.write("% Generated by Java Program: Debanjan");
wekaLexPragBoWPunchWriter.newLine();
wekaLexPragBoWPunchWriter.write("% " + now.toString());
wekaLexPragBoWPunchWriter.newLine();
wekaLexPragBoWPunchWriter.write("@RELATION rel") ;
wekaLexPragBoWPunchWriter.newLine();
size = features.size();
for (int i = 1 ; i <= size ; i++)
{
//@ATTRIBUTE 1 NUMERIC
String att = features.get(i-1) ;
att = att.replaceAll("'", "QUOTE");
att = att.replaceAll("\"", "DOUBQUOTE");
att =att.replaceAll("'", "-");
wekaLexPragBoWPunchWriter.write("@ATTRIBUTE" + " " + att+ " " + "NUMERIC");
wekaLexPragBoWPunchWriter.newLine();
}
//@ATTRIBUTE '-label-' {sarcasm,negative,positive}
String label = createStringOnLabels(labels);
wekaLexPragBoWPunchWriter.write("@ATTRIBUTE '-label-' " + label) ;
}
private String createStringOnLabels(Set<String> labels)
{
// TODO Auto-generated method stub
String ret = "{" ;
for ( String l : labels )
{
ret = ret + l +"," ;
}
ret = ret.substring(0,ret.length()-1) ;
ret = ret + "}" ;
return ret;
}
public void setLabels(Set<String> allLabels)
{
// TODO Auto-generated method stub
this.allLabels = allLabels ;
}
public void setFeatures(List<String> nonWPFeatures)
{
// TODO Auto-generated method stub
this.features = nonWPFeatures ;
featureSize = this.features.size();
}
}