Seeded synthetic support tickets in five categories; Tribuo and Smile logistic regression and Smile naive Bayes against a majority baseline; skewed classes, train/test duplicates, vocabulary drift and the three different answers to a ticket of unseen words; a zero-shot client for OpenAI-compatible endpoints tested against a fake endpoint (no model was called) and a cost formula with assumed prices. Every figure is written to output/ by the tests that assert it. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b69H5rTewUKPyhGS8r
61 lines
2.3 KiB
Java
61 lines
2.3 KiB
Java
package com.ankurm.classicml;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.EnumMap;
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.Locale;
|
|
import java.util.Map;
|
|
import java.util.Random;
|
|
import java.util.Set;
|
|
|
|
/** Stratified train/test split, plus the duplicate check that keeps the split honest. */
|
|
public final class Splits {
|
|
|
|
public record Split(List<Ticket> train, List<Ticket> test) { }
|
|
|
|
private Splits() { }
|
|
|
|
/** Splits per category so every class keeps the same share in both halves. */
|
|
public static Split stratified(List<Ticket> all, double trainShare, long seed) {
|
|
Map<Category, List<Ticket>> byClass = new EnumMap<>(Category.class);
|
|
for (Ticket t : all) byClass.computeIfAbsent(t.label(), k -> new ArrayList<>()).add(t);
|
|
List<Ticket> train = new ArrayList<>();
|
|
List<Ticket> test = new ArrayList<>();
|
|
Random rnd = new Random(seed);
|
|
for (List<Ticket> group : byClass.values()) {
|
|
Collections.shuffle(group, rnd);
|
|
int cut = (int) Math.round(group.size() * trainShare);
|
|
train.addAll(group.subList(0, cut));
|
|
test.addAll(group.subList(cut, group.size()));
|
|
}
|
|
Collections.shuffle(train, rnd);
|
|
Collections.shuffle(test, rnd);
|
|
return new Split(train, test);
|
|
}
|
|
|
|
/** Lower-cases and collapses whitespace so "Hi team" and "hi team" count as the same ticket. */
|
|
public static String key(String text) {
|
|
return text.toLowerCase(Locale.ROOT).replaceAll("\\s+", " ").trim();
|
|
}
|
|
|
|
/** Test tickets whose normalised text also occurs in training: the model has already seen them. */
|
|
public static List<Ticket> testSeenInTraining(Split s) {
|
|
Set<String> seen = new HashSet<>();
|
|
for (Ticket t : s.train()) seen.add(key(t.text()));
|
|
List<Ticket> dup = new ArrayList<>();
|
|
for (Ticket t : s.test()) if (seen.contains(key(t.text()))) dup.add(t);
|
|
return dup;
|
|
}
|
|
|
|
/** Test tickets that never occur in training. */
|
|
public static List<Ticket> testUnseen(Split s) {
|
|
Set<String> seen = new HashSet<>();
|
|
for (Ticket t : s.train()) seen.add(key(t.text()));
|
|
List<Ticket> fresh = new ArrayList<>();
|
|
for (Ticket t : s.test()) if (!seen.contains(key(t.text()))) fresh.add(t);
|
|
return fresh;
|
|
}
|
|
}
|