-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateTrainTestFolders.java
More file actions
167 lines (135 loc) · 3.75 KB
/
Copy pathCreateTrainTestFolders.java
File metadata and controls
167 lines (135 loc) · 3.75 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
package com.deft.sarcasm.preprocess;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
public class CreateTrainTestFolders
{
/**
* @param args
*/
private static Integer FOLD_NUM = 5 ;
public CreateTrainTestFolders()
{
}
public void readOriginalFileAndCreateFolds ( String path ) throws IOException
{
// String foldPath = path + "/output/svm/5fold/";
String foldPath = path + "/5fold/";
// String file = "rawcorpus_s_p.txt.binary.svm.TRAINING.txt" ;
// String file = "tweet.SARCNOSARC.ONLY.CONTEXT.TRAIN.prev_current.binary.svm.TRAINING.txt" ;
// String file = "tweet.SARCNOSARC.ONLY.CONTEXT.TRAIN.filtered.english.05122016.current.binary.svm.TRAINING.txt" ;
String file = "tweet.SARCNOSARC.CONTEXT.txt.current.svm.TRAINING.num.txt" ;
// String file = "tweet.SARCNOSARC.CONTEXT.txt" ;
//get the relation name
String relation = "SARCASM";
//load the relation file
List<String> docs = read(path + file);
//randomize the list
java.util.Collections.shuffle(docs);
int size = docs.size();
int foldSize = size/FOLD_NUM ;
foldSize +=1 ; //why? to take the very last few lines
int index = 0;
Writer writer = null ;
for (int i = 1; i<=FOLD_NUM; i++)
{
String folder = getName(i);
writer = new BufferedWriter ( new OutputStreamWriter (new FileOutputStream
(foldPath + folder +"/" + file + "." + folder),"UTF8"));
for ( int j = index ; j < foldSize * i ;j++)
{
String doc = null ;
try
{
doc = docs.get(j);
}
catch(IndexOutOfBoundsException e)
{
//so we have crossed the limit of docs
//do nothing?
writer.close();
break;
}
{
writer.write(doc);
writer.write("\n");
}
}
index = foldSize*i;
writer.close();
System.out.println("finish writing "+ folder +"/" + file + "." + folder) ;
}
}
private String getRelName(String file)
{
// TODO Auto-generated method stub
if(file.contains("Kill"))
return "Kill" ;
if(file.contains("Live_In"))
return "Live_In" ;
if(file.contains("OrgBased_In"))
return "OrgBased_In" ;
if(file.contains("Located_In"))
return "Located_In" ;
if(file.contains("Work_For"))
return "Work_For" ;
return null;
}
private String getName(int i)
{
// TODO Auto-generated method stub
switch (i)
{
case 1:
return "one";
case 2:
return "two" ;
case 3:
return "three" ;
case 4:
return "four" ;
case 5:
return "five" ;
default:
break;
}
return "none";
}
private List<String> read(String file) throws IOException
{
// TODO Auto-generated method stub
List<String> docs = new ArrayList<String>();
BufferedReader reader = new BufferedReader( new InputStreamReader
(
new FileInputStream (file), "UTF8") );
while (true)
{
String line = reader.readLine();
if ( line == null )
{
break;
}
docs.add(line.trim());
}
reader.close();
return docs;
}
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
// String input = "./data/oldpython/rawcorpus_s_p/svm/";
// String input = "./data/twitter_corpus/output/svm/";
String input = "/Users/dg513/work/eclipse-workspace/sarcasm-workspace/sarcasm_dialogue/Corpus/output/svm/" ;
// String input = "/Users/dg513/work/eclipse-workspace/sarcasm-workspace/sarcasm_dialogue/Corpus/" ;
CreateTrainTestFolders createTrainTestObj = new CreateTrainTestFolders();
createTrainTestObj.readOriginalFileAndCreateFolds(input);
}
}