import {
trainMaxEntTextClassifier,
loadMaxEntTextClassifier,
} from "bun_nltk";
// Training data for intent classification
const trainingData = [
{ label: "book_flight", text: "I want to book a flight to Paris" },
{ label: "cancel_booking", text: "Cancel my reservation please" },
{ label: "check_status", text: "What's the status of my order?" },
{ label: "book_flight", text: "Reserve a ticket to London tomorrow" },
{ label: "cancel_booking", text: "I need to cancel my appointment" },
{ label: "check_status", text: "Where is my package?" },
{ label: "book_flight", text: "Schedule a flight for next week" },
{ label: "cancel_booking", text: "Remove my booking" },
];
// Train with custom hyperparameters
const classifier = trainMaxEntTextClassifier(trainingData, {
epochs: 40,
learningRate: 0.2,
l2: 1e-4,
maxFeatures: 5000,
});
// Classify user input
const userText = "I'd like to fly to Tokyo";
const intent = classifier.classify(userText);
console.log(`Detected intent: ${intent}`); // "book_flight"
// Get confidence scores
const predictions = classifier.predict(userText);
for (const pred of predictions) {
console.log(
`${pred.label}: ${(pred.probability * 100).toFixed(1)}% (logit: ${pred.logit.toFixed(2)})`
);
}
// Output:
// book_flight: 87.3% (logit: 1.94)
// check_status: 8.2% (logit: -0.68)
// cancel_booking: 4.5% (logit: -1.23)
// Evaluate on test set
const testData = [
{ label: "book_flight", text: "Get me a plane ticket" },
{ label: "cancel_booking", text: "Delete my reservation" },
{ label: "check_status", text: "Track my order" },
];
const metrics = classifier.evaluate(testData);
console.log(`Test accuracy: ${(metrics.accuracy * 100).toFixed(1)}%`);
console.log(`Correct: ${metrics.correct}/${metrics.total}`);
// Check available labels
const labels = classifier.labelsList();
console.log(`Trained on ${labels.length} intents:`, labels);
// Save model
const modelData = classifier.toJSON();
await Bun.write("intent-classifier.json", JSON.stringify(modelData));
// Load model later
const loadedData = await Bun.file("intent-classifier.json").json();
const loadedClassifier = loadMaxEntTextClassifier(loadedData);
console.log(loadedClassifier.classify("Book a flight for me"));
// "book_flight"