diff --git a/BotSharp.NLP.UnitTest/NaiveBayesClassifierTest.cs b/BotSharp.NLP.UnitTest/NaiveBayesClassifierTest.cs index 445bb12a..5cfefb69 100644 --- a/BotSharp.NLP.UnitTest/NaiveBayesClassifierTest.cs +++ b/BotSharp.NLP.UnitTest/NaiveBayesClassifierTest.cs @@ -47,6 +47,8 @@ namespace BotSharp.NLP.UnitTest var classifier = new ClassifierFactory(options, SupportedLanguage.English); var dataset = sentences.Split(0.9M); + classifier.TrainInVector(dataset.Item1); + classifier.Train(dataset.Item1); int correct = 0; diff --git a/BotSharp.NLP/Classify/ClassifierFactory.cs b/BotSharp.NLP/Classify/ClassifierFactory.cs index 09d62df4..bd32aac8 100644 --- a/BotSharp.NLP/Classify/ClassifierFactory.cs +++ b/BotSharp.NLP/Classify/ClassifierFactory.cs @@ -50,5 +50,14 @@ namespace BotSharp.NLP.Classify _classifier.Train(sents, _options); } + + public void TrainInVector(List sentences) + { + var vectors = new List>(); + + var sents = sentences.Select(x => new Tuple(x.Label, x.Vector)).ToList(); + + _classifier.Train(sents, _options); + } } } diff --git a/BotSharp.NLP/Classify/IClassifier.cs b/BotSharp.NLP/Classify/IClassifier.cs index 8197f081..5b011508 100644 --- a/BotSharp.NLP/Classify/IClassifier.cs +++ b/BotSharp.NLP/Classify/IClassifier.cs @@ -10,5 +10,20 @@ namespace BotSharp.NLP.Classify void Train(List featureSets, ClassifyOptions options); List> Classify(List features, ClassifyOptions options); + + /// + /// Training by feature vector + /// + /// + /// + void Train(List> featureSets, ClassifyOptions options); + + /// + /// Predict by feature vector + /// + /// + /// + /// + List> Classify(double[] features, ClassifyOptions options); } } diff --git a/BotSharp.NLP/Classify/NaiveBayesClassifier.cs b/BotSharp.NLP/Classify/NaiveBayesClassifier.cs index b6586611..834b1864 100644 --- a/BotSharp.NLP/Classify/NaiveBayesClassifier.cs +++ b/BotSharp.NLP/Classify/NaiveBayesClassifier.cs @@ -138,6 +138,22 @@ namespace BotSharp.NLP.Classify return labelDist.Select(x => new Tuple(x.Value, x.Prob)).ToList(); } + + public void Train(List> featureSets, ClassifyOptions options) + { + labelDist = featureSets.GroupBy(x => x.Item1) + .Select(x => new Probability + { + Value = x.Key, + Freq = x.Count() + }) + .ToList(); + } + + public List> Classify(double[] features, ClassifyOptions options) + { + throw new NotImplementedException(); + } } public class FeaturesWithLabel diff --git a/BotSharp.NLP/Classify/SVMClassifier.cs b/BotSharp.NLP/Classify/SVMClassifier.cs index 99748ded..be97f77e 100644 --- a/BotSharp.NLP/Classify/SVMClassifier.cs +++ b/BotSharp.NLP/Classify/SVMClassifier.cs @@ -154,6 +154,16 @@ namespace BotSharp.NLP.Classify return labeledFeatureSet; } + + public void Train(List> featureSets, ClassifyOptions options) + { + throw new NotImplementedException(); + } + + public List> Classify(double[] features, ClassifyOptions options) + { + throw new NotImplementedException(); + } }