feat: implement Main class with methods to read training and evaluation files

This commit is contained in:
2025-12-05 22:11:29 +01:00
parent bf9c4be620
commit 57621315f5

View File

@@ -0,0 +1,82 @@
package de.th_luebeck.ws25;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
public class Main {
static void main(String[] args) {
/* ... */
}
private List<LabeledSequence> readTrainFile(Path path) throws IOException {
List<LabeledSequence> outputSequences = new ArrayList<>();
List<String> lines = Files.readAllLines(path);
for (String line : lines) {
line = line.trim();
if (line.isEmpty()) {
continue;
}
String[] parts = line.split("\\s+", 2);
if (parts.length < 2) {
continue;
}
Label label = Label.NORMAL;
if (parts[0].equals("A")) {
label = Label.ARRHYTHMIA;
}
List<Integer> values = Arrays.stream(parseInts(parts[1])).boxed().toList();
outputSequences.add(new LabeledSequence(label, values));
}
return outputSequences;
}
public static EvalFile readEvalFile(Path path) throws IOException {
List<String> lines = Files.readAllLines(path);
if (lines.size() < 2) {
throw new IOException("Eval file must contain at least two lines");
}
List<Integer> sequence = Arrays.stream(parseInts(lines.get(0).trim())).boxed().toList();
List<Interval> intervals = parseIntervals(lines.get(1));
return new EvalFile(sequence, intervals);
}
public static int[] parseInts(String inputString) {
List<Integer> numbers = new ArrayList<>();
Scanner scanner = new Scanner(inputString);
scanner.useDelimiter("[\\s,\\[\\]]+");
while (scanner.hasNext()) {
if (scanner.hasNextInt()) {
numbers.add(scanner.nextInt());
} else {
scanner.next();
}
}
scanner.close();
return numbers.stream().mapToInt(i -> i).toArray();
}
public static List<Interval> parseIntervals(String inputString) {
int[] numbers = parseInts(inputString);
List<Interval> outputIntervals = new ArrayList<>();
for (int i = 0; i + 1 < numbers.length; i += 2) {
int start = numbers[i], end = numbers[i + 1];
if (start <= end) {
outputIntervals.add(new Interval(start, end));
} else {
outputIntervals.add(new Interval(end, start));
}
}
return outputIntervals;
}
}