-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSVStripper.java
More file actions
238 lines (233 loc) · 7.67 KB
/
CSVStripper.java
File metadata and controls
238 lines (233 loc) · 7.67 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class CSVStripper {
public static JFrame f= new JFrame("Java Swing TextField");
public static JButton submit = new JButton("Submit");
public static String line = "";
public static long cc1, cc2, cc3, cc4 = 0;
public static int rows = 0;
public static String delim = "";
public static void main(String[]args) throws Exception {
JFileChooser chooser = new JFileChooser("/run/media/mm/Easystore/Research/Original/");
chooser.showOpenDialog(null);
FileReader fr = new FileReader(new File(chooser.getSelectedFile().getPath()));
//FileReader fr = new FileReader(new File("/run/media/mm/Easystore/Research/ADHD.csv"));
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter(new File("/run/media/mm/Easystore/Research/ADHD_Drug.csv"));
BufferedWriter bw = new BufferedWriter(fw);
String headers = br.readLine();
bw.write(headers + "\n");
if(headers.contains("\",\"")) {
headers = headers.substring(1, headers.length()-1);
delim = "\",\"";
}
line = br.readLine();
if(line.contains("\",\"")) {
line = line.substring(1, line.length()-1);
delim = "\",\"";
}
else if(line.contains(";")) {
delim = ";";
}
else if(line.contains(",")) {
delim = ",";
}
String [] s = headers.split(delim);
String [] fl = line.split(delim);
JLabel [] l = new JLabel[s.length];
JTextField [] tf = new JTextField[s.length];
ArrayList<ArrayList<String>> arr = new ArrayList<ArrayList<String>>();
f.setSize(1000,800);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Font font = new Font("Serif", Font.PLAIN, 18);
for(int i = 0; i < s.length; i++) {
l[i]=new JLabel(s[i] + " (ex. " + fl[i] + ")");
l[i].setBounds(20,20+((int)f.getSize().getHeight()/(s.length+2))*i, 500,40);
l[i].setFont(font);
l[i].setHorizontalAlignment(SwingConstants.RIGHT);
tf[i]=new JTextField("");
tf[i].setBounds(550,20+((int)f.getSize().getHeight()/(s.length+2))*i, 400,40);
tf[i].setFont(font);
f.add(l[i]);
f.add(tf[i]);
}
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ArrayList<Integer> pos = new ArrayList<Integer>();
for(int i = 0; i < tf.length; i++) {
tf[i].setText(tf[i].getText().replace(" ", ""));
arr.add(new ArrayList<String>(Arrays.asList(tf[i].getText().split(","))));
}
for(int i = 0; i < arr.size(); i++) {
if(!arr.get(i).isEmpty()) {
pos.add(i);
}
}
for(int i = 0; i < arr.size(); i++) {
System.out.print(arr.get(i) + "\t");
}
System.out.println();
System.out.print(arr.get(3) + "\t");
System.out.println(arr.get(3).get(0));
f.dispose();
long time = System.currentTimeMillis();
int count2 = 0;
while(line != null) {
int ccc = arr.size();
long time2 = System.currentTimeMillis();
String [] t = new String[ccc];
String z = line;
for(int i = 0; i < t.length-1; i++) {
t[i] = z.substring(0, z.indexOf(delim));
z = z.substring(z.indexOf(delim)+delim.length());
}
t[t.length-1] = z;
boolean print = false;
cc1 += System.currentTimeMillis() - time2;
time2 = System.currentTimeMillis();
for(int i = 0; i < pos.size(); i++) {
for(int j = 0; j < arr.get(pos.get(i)).size(); j++) {
String testing = arr.get(pos.get(i)).get(j);
String argg = t[pos.get(i)];
if(!testing.isEmpty() && testing.charAt(testing.length()-1) == '*') {
int mark = testing.indexOf('*');
if(mark <= argg.length() && testing.substring(0, mark).equals(argg.substring(0, mark))) {
print = true;
break;
}
}
else if(!testing.isEmpty() && testing.charAt(0) == '*') {
if(testing.substring(1).equals(argg.substring(argg.length() - testing.length() + 1))) {
print = true;
break;
}
}
else if(!testing.isEmpty() && testing.contains("*") && testing.length() == argg.length()) {
int mark = testing.indexOf("*");
if(testing.substring(0, mark).equals(argg.substring(0, mark)) && testing.substring(mark+1).equals(argg.substring(mark+1))) {
print = true;
break;
}
}
else if(!testing.isEmpty() && testing.contains("-")) {
String upper = testing.substring(0, testing.indexOf("-"));
String lower = testing.substring(testing.indexOf("-")+1);
int ssize = 0;
int arggL = argg.length();
int upperL = upper.length();
int lowerL = lower.length();
if(arggL <= upperL && arggL <= lowerL) {
ssize = arggL;
}
else if(upperL <= arggL && upperL <= lowerL) {
ssize = upperL;
}
else if(lowerL <= arggL && lowerL <= upperL) {
ssize = lowerL;
}
char [][] ch = new char[3][ssize];
for(int k = 0; k < ssize; k++) {
ch[0][k] = argg.charAt(k);
ch[1][k] = upper.charAt(k);
ch[2][k] = lower.charAt(k);
boolean current01 = ch[0][k] == ch[1][k];
boolean current02 = ch[0][k] == ch[2][k];
boolean previous01 = false;
boolean previous02 = false;
if(k > 0) {
previous01 = ch[0][k-1] == ch[1][k-1];
previous02 = ch[0][k-1] == ch[2][k-1];
}
if(ch[0][k] > ch[1][k] && ch[0][k] < ch[2][k]) {
print = true;
break;
}
else if(current01 || current02) {
if(k == ssize - 1) {
print = true;
break;
}
}
else if(k > 0 && k < ssize && ((previous01 && !previous02 && ch[0][k] >= ch[1][k]) || (previous02 && !previous01 && ch[0][k] <= ch[2][k])) ) {
print = true;
break;
}
else {
print = false;
break;
}
}
if(print) {
break;
}
}
else {
if(!testing.isEmpty() && testing.equals(argg)) {
print = true;
break;
}
}
}
}
cc2 += System.currentTimeMillis() - time2;
time2 = System.currentTimeMillis();
if(print) {
if(delim.equals("\",\"")) {
line = "\"" + line + "\"";
}
try {
bw.write(line+"\n");
//System.out.println(line);
//Thread.sleep(2000);
bw.flush();
}
catch (Exception e1) {
System.out.println("HERE -----------------------------------------------------" + line);
}
}
count2++;
if(count2 == 10000000) {
count2 = 0;
rows++;
System.out.println(System.currentTimeMillis() - time + "\t" + cc1 + "\t" + cc2 + "\t" + cc3 + "\t" + cc4 + "\t" + rows);
time = System.currentTimeMillis();
cc1 = 0;cc2 = 0;cc3 = 0; cc4 = 0;
}
cc3 += System.currentTimeMillis() - time2;
time2 = System.currentTimeMillis();
try {
line = br.readLine();
//System.out.println(line);
//Thread.sleep(50);
}
catch (Exception e1) {
System.out.println("HELP");
}
if(line != null && delim.equals("\",\"")) {
line = line.substring(1, line.length()-1);
}
cc4 += System.currentTimeMillis() - time2;
}
try {
bw.flush();
fw.flush();
bw.close();
fw.close();
br.close();
fr.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
f.dispatchEvent(new WindowEvent(f, WindowEvent.WINDOW_CLOSING));
}
} );
submit.setBounds((int)f.getSize().getWidth()-150, (int)f.getSize().getHeight()-100, 100, 40);
f.add(submit);
f.setLayout(null);
f.setVisible(true);
}
}