-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSVread.java
More file actions
129 lines (121 loc) · 5.35 KB
/
CSVread.java
File metadata and controls
129 lines (121 loc) · 5.35 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
package classification;
import java.io.File;
import java.util.Scanner;
public class CSVread {
// --- Function for counting the number of rows in a CSV file
static int calcRowCount (String dataset, boolean index) throws Exception {
int rowCount = 0;
String line;
// Scanning the CSV file by rows
try (Scanner scanner = new Scanner(new File(dataset));){
line = scanner.nextLine();
}
try (Scanner rowScanner = new Scanner(line)) {
rowScanner.useDelimiter(",");
while (rowScanner.hasNext()) {
rowCount++;
rowScanner.next();
}
}
// If the file has an index, the count is being decremented
if (index == true) { return rowCount -1; }
else { return rowCount;}
}
// --- Function for counting the number of columns in a CSV file
static int calcColumnCount (String dataset, boolean header) throws Exception {
int columnCount = 0;
// Scanning the CSV file by columns
try (Scanner columnScanner = new Scanner(new File(dataset))) {
while (columnScanner.hasNextLine()) {
columnCount++;
columnScanner.nextLine();
}
}
// If the file has a header, the count is being decremented
if (header == true) { return columnCount -1; }
else { return columnCount;}
}
// --- Function for reading the predictor data from a CSV file
static float[][] transformPredictorData(String dataset, boolean index, boolean header, int columnCount, int rowCount) throws Exception{
float[][] predictorData = new float[columnCount][rowCount];
boolean skip = true;
// Scanning the CSV file column by column
try (Scanner scanner = new Scanner(new File(dataset));) {
for (int i = 0; scanner.hasNextLine(); i++) {
// Skipping the first iteration if there is a header
if (header == true && skip == true) {
skip = false;
scanner.nextLine();
}
// Calling the getRecordFromLine function for reading the line in the CSV file and writing it as an array to the data
predictorData[i] = getRecordFromLine(scanner.nextLine(), index, rowCount -1);
// Row count -1 because in the last row is the result data
}
}
return predictorData;
}
// --- Function for reading the lines of and separating it into a string by a delimiter
static float[] getRecordFromLine (String line, boolean index, int rowCount) {
boolean skip = true;
float[] values = new float[rowCount];
try (Scanner rowScanner = new Scanner(line)) {
// Using , as a delimiter for separating the line input
rowScanner.useDelimiter(",");
for (int i = 0; rowScanner.hasNext(); i++) {
//System.out.println(rowScanner);
// Skip if there is an index
if ((index == true && skip == true) || i == rowCount) {
skip = false;
rowScanner.next();
// Break if it has iterated through the elements of the line
// The last element is going to be skipped (result data)
if (i == rowCount) {
break;
}
}
String tempVar = rowScanner.next();
//System.out.println(tempVar);
values[i] = Float.parseFloat(tempVar);
}
}
return values;
}
// --- Function for reading the predictor data from a CSV file
static String[] transformResultData(String dataset, boolean index, boolean header, int columnCount, int rowCount) throws Exception{
String[] resultData = new String[columnCount];
boolean skip = true;
// Scanning the CSV file column by column
try (Scanner scanner = new Scanner(new File(dataset));) {
for (int i = 0; scanner.hasNextLine(); i++) {
// Skip if there is a header
if (header == true && skip == true) {
skip = false;
scanner.nextLine();
}
// Calling the getRecordFromLineString function for reading the line in the CSV file and writing it as an string element
resultData[i] = getRecordFromLineString(scanner.nextLine(), index, rowCount);
}
}
return resultData;
}
// --- Function for reading the lines of and separating it into a string by a delimiter
static String getRecordFromLineString (String line, boolean index, int rowCount) {
boolean skip = true;
String values = new String();
try (Scanner rowScanner = new Scanner(line)) {
// Using , as a delimiter for separating the line input
rowScanner.useDelimiter(",");
for (int i = 0; rowScanner.hasNext(); i++) {
if (i == rowCount) {
// Only writing the last element to the return string (result data)
if (i == rowCount) {
values = rowScanner.next();
break;
}
}
rowScanner.next();
}
}
return values;
}
}