BotSharp/BotSharp.NLP/Classify/ClassifierFactory.cs

53 lines
1.4 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();
}
public void Train(List<Sentence> sentences)
{
_classifier.Train(sentences, _options);
_classifier.SaveModel(_options);
}
2018-09-11 21:17:12 +00:00
public List<Tuple<string, double>> Classify(Sentence sentence)
{
2018-09-11 21:17:12 +00:00
var options = new ClassifyOptions
{
ModelFilePath = _options.ModelFilePath
2018-09-11 21:17:12 +00:00
};
_classifier.LoadModel(options);
var classes = _classifier.Classify(sentence, options);
classes = classes.OrderByDescending(x => x.Item2).ToList();
return classes;
}
}
}