Add word vector interface for NB text classifier.

This commit is contained in:
Esther2013 2018-09-11 07:11:21 -05:00
parent f200554768
commit b89ced3321
5 changed files with 52 additions and 0 deletions

View file

@ -47,6 +47,8 @@ namespace BotSharp.NLP.UnitTest
var classifier = new ClassifierFactory<NaiveBayesClassifier, SentenceFeatureExtractor>(options, SupportedLanguage.English);
var dataset = sentences.Split(0.9M);
classifier.TrainInVector(dataset.Item1);
classifier.Train(dataset.Item1);
int correct = 0;

View file

@ -50,5 +50,14 @@ namespace BotSharp.NLP.Classify
_classifier.Train(sents, _options);
}
public void TrainInVector(List<Sentence> sentences)
{
var vectors = new List<Tuple<string, double[]>>();
var sents = sentences.Select(x => new Tuple<string, double[]>(x.Label, x.Vector)).ToList();
_classifier.Train(sents, _options);
}
}
}

View file

@ -10,5 +10,20 @@ namespace BotSharp.NLP.Classify
void Train(List<FeaturesWithLabel> featureSets, ClassifyOptions options);
List<Tuple<string, double>> Classify(List<Feature> features, ClassifyOptions options);
/// <summary>
/// Training by feature vector
/// </summary>
/// <param name="featureSets"></param>
/// <param name="options"></param>
void Train(List<Tuple<string, double[]>> featureSets, ClassifyOptions options);
/// <summary>
/// Predict by feature vector
/// </summary>
/// <param name="features"></param>
/// <param name="options"></param>
/// <returns></returns>
List<Tuple<string, double>> Classify(double[] features, ClassifyOptions options);
}
}

View file

@ -138,6 +138,22 @@ namespace BotSharp.NLP.Classify
return labelDist.Select(x => new Tuple<string, double>(x.Value, x.Prob)).ToList();
}
public void Train(List<Tuple<string, double[]>> featureSets, ClassifyOptions options)
{
labelDist = featureSets.GroupBy(x => x.Item1)
.Select(x => new Probability
{
Value = x.Key,
Freq = x.Count()
})
.ToList();
}
public List<Tuple<string, double>> Classify(double[] features, ClassifyOptions options)
{
throw new NotImplementedException();
}
}
public class FeaturesWithLabel

View file

@ -154,6 +154,16 @@ namespace BotSharp.NLP.Classify
return labeledFeatureSet;
}
public void Train(List<Tuple<string, double[]>> featureSets, ClassifyOptions options)
{
throw new NotImplementedException();
}
public List<Tuple<string, double>> Classify(double[] features, ClassifyOptions options)
{
throw new NotImplementedException();
}
}