forked from omerbsezer/CNN-TA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Phase2.java
161 lines (124 loc) · 4.21 KB
/
Phase2.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
package ml.cnn.paper;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import org.apache.commons.math3.util.Precision;
public class Phase2 {
public static int CREATEBOOSTEDTRAINDATA=0;
public static int numberOfZero,numberOfOne,numberOfTwo;
public static int lineCount, formula;
public static String[][] secondData;
public static String[][] firstData;
public static void PhaseProcess(String inputFilePath, String outputFile) throws Exception {
//String fName = "resources3/outputOfDf.csv";
String fName=inputFilePath;
String thisLine;
int count=0;
FileInputStream fis = new FileInputStream(fName);
DataInputStream myInput = new DataInputStream(fis);
int i=0;
String[][] data = new String[0][];//csv data line count=0 initially
while ((thisLine = myInput.readLine()) != null) {
++i;//increment the line count when new line found
String[][] newdata = new String[i][2];//create new array for data
String strar[] = thisLine.split(";");//get contents of line as an array
newdata[i - 1] = strar;//add new line to the array
System.arraycopy(data, 0, newdata, 0, i - 1);//copy previously read values to new array
data = newdata;//set new array as csv data
}
for(int j=0;j<6;j++){ //call shiftUp windowSize/2
shiftUp(0,data);
}
//relabel(data);
converToCsv(data,outputFile);
}
public static void main(String[] args) throws Exception {
String fName = "resources3/outputOfDf.csv";
String thisLine;
int count=0;
FileInputStream fis = new FileInputStream(fName);
DataInputStream myInput = new DataInputStream(fis);
int i=0;
String outputFile="Test";
String[][] data = new String[0][];//csv data line count=0 initially
while ((thisLine = myInput.readLine()) != null) {
++i;//increment the line count when new line found
String[][] newdata = new String[i][2];//create new array for data
String strar[] = thisLine.split(";");//get contents of line as an array
newdata[i - 1] = strar;//add new line to the array
System.arraycopy(data, 0, newdata, 0, i - 1);//copy previously read values to new array
data = newdata;//set new array as csv data
}
for(int j=0;j<6;j++){ //call shiftUp windowSize/2
shiftUp(0,data);
}
//relabel(data);
converToCsv(data,outputFile);
}
//relabel data
public static void relabel(String[][] array) {
int m = array.length;
int k=0;
while(k<m-1){
//for (int k=0; k<m-1; k++){
if(array[k][0].equals("2")){
//System.out.println("into 0");
for (int i=1; i<5; i++){
if(array[k-i][0].equals("0") && array[k-i-1][0].equals("0")){
array[k-i][0]="2";
}
if(array[k+i][0].equals("0") && array[k+i+1][0].equals("0")){
array[k+i][0]="2";
}
}
k=k+4;
}
if(array[k][0].equals("1")){
for (int i=1; i<5; i++){
if(array[k-i][0].equals("0") && array[k-i-1][0].equals("0")){
array[k-i][0]="1";
}
if(array[k+i][0].equals("0") && array[k+i+1][0].equals("0")){
array[k+i][0]="1";
}
}
k=k+4;
}
k++;
}
//array[m-1][0] = "--";
}
//shift up labelled data
public static void shiftUp(int i, String[][] array) {
int m = array.length;
for (int k=0; k<m-1; k++){
array[k][0] = array[k+1][0];
}
//array[m-1][0] = "--";
}
public static void converToCsv(String[][] board, String outputFile) {
StringBuilder builder = new StringBuilder();
for(int n = 0; n < board.length; n++)//for each row
{
for(int j = 0; j < board[0].length; j++)//for each column
{
builder.append(board[n][j]+"");//append to the output string
if(j < board.length - 1)//if this is not the last row element
builder.append(";");//then add comma (if you don't like commas you can use spaces)
}
builder.append("\n");//append new line at the end of the row
}
BufferedWriter writer;
try {
writer = new BufferedWriter(new FileWriter("resources3/outputOfPhase2"+outputFile+".csv"));
writer.write(builder.toString());//save the string representation of the board
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}