BotSharp/BotSharp.NLP.UnitTest/NaiveBayesClassifierTest.cs

133 lines
4.3 KiB
C#
Raw Normal View History

using BotSharp.NLP.Classify;
using BotSharp.NLP.Corpus;
using BotSharp.NLP.Tokenize;
using Microsoft.Extensions.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
2018-09-09 01:47:53 +00:00
using System.Linq;
using System.Text;
2018-09-09 01:47:53 +00:00
using BotSharp.Algorithm.Extensions;
2018-09-11 03:35:33 +00:00
using BotSharp.NLP.Txt2Vec;
namespace BotSharp.NLP.UnitTest
{
[TestClass]
public class NaiveBayesClassifierTest : TestEssential
{
2018-09-09 03:59:01 +00:00
[TestMethod]
public void CookingTest()
{
var reader = new FasttextDataReader();
var sentences = reader.Read(new ReaderOptions
{
DataDir = Path.Combine(Configuration.GetValue<String>("MachineLearning:dataDir"), "Text Classification", "cooking.stackexchange"),
FileName = "cooking.stackexchange.txt"
});
2018-09-10 03:56:32 +00:00
var tokenizer = new TokenizerFactory<TreebankTokenizer>(new TokenizationOptions { }, SupportedLanguage.English);
var newSentences = tokenizer.Tokenize(sentences.Select(x => x.Text).ToList());
for(int i = 0; i < newSentences.Count; i++)
{
newSentences[i].Label = sentences[i].Label;
}
2018-09-11 03:35:33 +00:00
sentences = newSentences.ToList();
2018-09-10 22:25:41 +00:00
2018-09-09 03:59:01 +00:00
sentences.Shuffle();
2018-09-11 03:35:33 +00:00
var encoder = new OneHotEncoder();
encoder.Sentences = sentences;
encoder.EncodeAll();
2018-09-09 03:59:01 +00:00
var options = new ClassifyOptions
{
TrainingCorpusDir = Path.Combine(Configuration.GetValue<String>("MachineLearning:dataDir"), "Text Classification", "cooking.stackexchange")
};
2018-09-10 03:56:32 +00:00
var classifier = new ClassifierFactory<NaiveBayesClassifier, SentenceFeatureExtractor>(options, SupportedLanguage.English);
2018-09-10 22:25:41 +00:00
var dataset = sentences.Split(0.9M);
classifier.TrainInVector(dataset.Item1);
2018-09-09 03:59:01 +00:00
classifier.Train(dataset.Item1);
int correct = 0;
2018-09-10 22:25:41 +00:00
dataset.Item1.ToList().ForEach(td =>
2018-09-09 03:59:01 +00:00
{
var classes = classifier.Classify(td);
if (td.Label == classes[0].Item1)
{
correct++;
}
});
var accuracy = (float)correct / dataset.Item2.Count;
}
[TestMethod]
public void GenderTest()
{
var options = new ClassifyOptions
{
2018-09-06 22:32:51 +00:00
TrainingCorpusDir = Path.Combine(Configuration.GetValue<String>("MachineLearning:dataDir"), "Gender")
};
2018-09-10 03:56:32 +00:00
var classifier = new ClassifierFactory<NaiveBayesClassifier, WordFeatureExtractor>(options, SupportedLanguage.English);
var corpus = GetLabeledCorpus(options);
var tokenizer = new TokenizerFactory<RegexTokenizer>(new TokenizationOptions
{
Pattern = RegexTokenizer.WORD_PUNC
}, SupportedLanguage.English);
corpus.ForEach(x => x.Words = tokenizer.Tokenize(x.Text));
2018-09-09 14:36:22 +00:00
classifier.Train(corpus);
string text = "Bridget";
classifier.Classify(new Sentence { Text = text, Words = tokenizer.Tokenize(text) });
2018-09-09 01:47:53 +00:00
corpus.Shuffle();
var trainingData = corpus.Skip(2000).ToList();
classifier.Train(trainingData);
2018-09-09 01:47:53 +00:00
var testData = corpus.Take(2000).ToList();
int correct = 0;
testData.ForEach(td =>
{
var classes = classifier.Classify(td);
2018-09-09 03:59:01 +00:00
if(td.Label == classes[0].Item1)
2018-09-09 01:47:53 +00:00
{
correct++;
}
});
var accuracy = (float)correct / testData.Count;
}
private List<Sentence> GetLabeledCorpus(ClassifyOptions options)
{
var reader = new LabeledPerFileNameReader();
var genders = new List<Sentence>();
var female = reader.Read(new ReaderOptions
{
DataDir = options.TrainingCorpusDir,
FileName = "female.txt"
});
genders.AddRange(female);
var male = reader.Read(new ReaderOptions
{
DataDir = options.TrainingCorpusDir,
FileName = "male.txt"
});
genders.AddRange(male);
return genders;
}
}
}