BotSharp/BotSharp.NLP/Classify/ClassifierFactory.cs

55 lines
1.5 KiB
C#
Raw Normal View History

2018-09-10 03:56:32 +00:00
using BotSharp.Algorithm.Features;
using BotSharp.NLP.Tokenize;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BotSharp.NLP.Classify
{
2018-09-10 03:56:32 +00:00
public class ClassifierFactory<IClassify, IFeatureExtractor>
where IClassify : IClassifier, new()
where IFeatureExtractor : ITextFeatureExtractor, new()
{
private SupportedLanguage _lang;
private IClassify _classifier;
private ClassifyOptions _options;
2018-09-10 03:56:32 +00:00
private IFeatureExtractor featureExtractor;
public ClassifierFactory(ClassifyOptions options, SupportedLanguage lang)
{
_lang = lang;
_options = options;
_classifier = new IClassify();
2018-09-10 03:56:32 +00:00
featureExtractor = new IFeatureExtractor();
}
2018-09-09 01:47:53 +00:00
public List<Tuple<string, double>> Classify(Sentence sentence)
{
2018-09-10 22:25:41 +00:00
var options = new ClassifyOptions
{
2018-09-10 22:25:41 +00:00
};
var features = featureExtractor.GetFeatures(sentence.Words);
var classes = _classifier.Classify(features, options);
2018-09-09 01:47:53 +00:00
return classes.OrderByDescending(x => x.Item2).ToList();
}
public void Train(List<Sentence> sentences)
{
2018-09-10 22:25:41 +00:00
var sents = sentences.Select(x => new FeaturesWithLabel
{
2018-09-09 03:59:01 +00:00
Label = x.Label,
2018-09-10 03:56:32 +00:00
Features = featureExtractor.GetFeatures(x.Words)
2018-09-10 22:25:41 +00:00
}).ToList();
_classifier.Train(sents, _options);
}
}
}