import {
trainLogisticTextClassifier,
trainLinearSvmTextClassifier,
} from "bun_nltk";
// Training data for sentiment analysis
const trainingData = [
{ label: "positive", text: "Excellent product, very satisfied" },
{ label: "negative", text: "Terrible quality, waste of money" },
{ label: "neutral", text: "Average product, nothing special" },
{ label: "positive", text: "Love it! Highly recommend" },
{ label: "negative", text: "Broke after one week" },
{ label: "neutral", text: "Works as expected" },
];
// Train logistic regression
const logistic = trainLogisticTextClassifier(trainingData, {
epochs: 30,
learningRate: 0.1,
});
// Train linear SVM
const svm = trainLinearSvmTextClassifier(trainingData, {
epochs: 25,
learningRate: 0.05,
margin: 1.0,
});
// Compare predictions
const text = "Amazing value for the price";
console.log("Logistic:", logistic.predict(text));
console.log("SVM:", svm.predict(text));
// Batch classification
const reviews = [
"Great purchase",
"Not worth it",
"It's okay",
];
console.log(logistic.classifyBatch(reviews));
// ["positive", "negative", "neutral"]
// Evaluate both models
const testData = [
{ label: "positive", text: "Best decision ever" },
{ label: "negative", text: "Complete disappointment" },
];
const logisticMetrics = logistic.evaluate(testData);
const svmMetrics = svm.evaluate(testData);
console.log(`Logistic accuracy: ${(logisticMetrics.accuracy * 100).toFixed(1)}%`);
console.log(`SVM accuracy: ${(svmMetrics.accuracy * 100).toFixed(1)}%`);
// Save models
await Bun.write("logistic.json", JSON.stringify(logistic.toJSON()));
await Bun.write("svm.json", JSON.stringify(svm.toJSON()));