Add word vector interface for NB text classifier.
This commit is contained in:
parent
f200554768
commit
b89ced3321
|
|
@ -47,6 +47,8 @@ namespace BotSharp.NLP.UnitTest
|
||||||
var classifier = new ClassifierFactory<NaiveBayesClassifier, SentenceFeatureExtractor>(options, SupportedLanguage.English);
|
var classifier = new ClassifierFactory<NaiveBayesClassifier, SentenceFeatureExtractor>(options, SupportedLanguage.English);
|
||||||
|
|
||||||
var dataset = sentences.Split(0.9M);
|
var dataset = sentences.Split(0.9M);
|
||||||
|
classifier.TrainInVector(dataset.Item1);
|
||||||
|
|
||||||
classifier.Train(dataset.Item1);
|
classifier.Train(dataset.Item1);
|
||||||
|
|
||||||
int correct = 0;
|
int correct = 0;
|
||||||
|
|
|
||||||
|
|
@ -50,5 +50,14 @@ namespace BotSharp.NLP.Classify
|
||||||
|
|
||||||
_classifier.Train(sents, _options);
|
_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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,5 +10,20 @@ namespace BotSharp.NLP.Classify
|
||||||
void Train(List<FeaturesWithLabel> featureSets, ClassifyOptions options);
|
void Train(List<FeaturesWithLabel> featureSets, ClassifyOptions options);
|
||||||
|
|
||||||
List<Tuple<string, double>> Classify(List<Feature> features, 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -138,6 +138,22 @@ namespace BotSharp.NLP.Classify
|
||||||
|
|
||||||
return labelDist.Select(x => new Tuple<string, double>(x.Value, x.Prob)).ToList();
|
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
|
public class FeaturesWithLabel
|
||||||
|
|
|
||||||
|
|
@ -154,6 +154,16 @@ namespace BotSharp.NLP.Classify
|
||||||
|
|
||||||
return labeledFeatureSet;
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue