2018-09-29 04:21:44 +00:00
|
|
|
|
using Bigtree.Algorithm.Features;
|
2018-09-04 02:05:57 +00:00
|
|
|
|
using BotSharp.NLP.Tokenize;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2018-09-26 11:45:35 +00:00
|
|
|
|
using System.Reflection;
|
2018-09-04 02:05:57 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.NLP.Classify
|
|
|
|
|
|
{
|
2018-09-26 11:45:35 +00:00
|
|
|
|
public class ClassifierFactory<IFeatureExtractor>
|
2018-09-10 03:56:32 +00:00
|
|
|
|
where IFeatureExtractor : ITextFeatureExtractor, new()
|
2018-09-04 02:05:57 +00:00
|
|
|
|
{
|
|
|
|
|
|
private SupportedLanguage _lang;
|
|
|
|
|
|
|
2018-09-26 11:45:35 +00:00
|
|
|
|
private IClassifier _classifier;
|
2018-09-04 02:05:57 +00:00
|
|
|
|
|
|
|
|
|
|
private ClassifyOptions _options;
|
|
|
|
|
|
|
2018-09-10 03:56:32 +00:00
|
|
|
|
private IFeatureExtractor featureExtractor;
|
|
|
|
|
|
|
2018-09-04 02:05:57 +00:00
|
|
|
|
public ClassifierFactory(ClassifyOptions options, SupportedLanguage lang)
|
|
|
|
|
|
{
|
|
|
|
|
|
_lang = lang;
|
|
|
|
|
|
_options = options;
|
2018-09-10 03:56:32 +00:00
|
|
|
|
featureExtractor = new IFeatureExtractor();
|
2018-09-04 02:05:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-26 11:45:35 +00:00
|
|
|
|
public IClassifier GetClassifer(string name)
|
|
|
|
|
|
{
|
|
|
|
|
|
List<Type> types = new List<Type>();
|
|
|
|
|
|
|
|
|
|
|
|
types.AddRange(Assembly.Load(new AssemblyName("BotSharp.Core"))
|
|
|
|
|
|
.GetTypes().Where(x => !x.IsAbstract && !x.FullName.StartsWith("<>f__AnonymousType")).ToList());
|
|
|
|
|
|
|
|
|
|
|
|
types.AddRange(Assembly.Load(new AssemblyName("BotSharp.NLP"))
|
|
|
|
|
|
.GetTypes().Where(x => !x.IsAbstract && !x.FullName.StartsWith("<>f__AnonymousType")).ToList());
|
|
|
|
|
|
|
|
|
|
|
|
Type type = types.FirstOrDefault(x => x.Name == name);
|
|
|
|
|
|
var instance = (IClassifier)Activator.CreateInstance(type);
|
|
|
|
|
|
|
|
|
|
|
|
return _classifier = instance;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 02:05:57 +00:00
|
|
|
|
public void Train(List<Sentence> sentences)
|
|
|
|
|
|
{
|
2018-09-12 20:31:20 +00:00
|
|
|
|
_classifier.Train(sentences, _options);
|
|
|
|
|
|
_classifier.SaveModel(_options);
|
2018-09-04 02:05:57 +00:00
|
|
|
|
}
|
2018-09-11 12:11:21 +00:00
|
|
|
|
|
2018-09-11 21:17:12 +00:00
|
|
|
|
public List<Tuple<string, double>> Classify(Sentence sentence)
|
2018-09-11 12:11:21 +00:00
|
|
|
|
{
|
2018-09-11 21:17:12 +00:00
|
|
|
|
var options = new ClassifyOptions
|
|
|
|
|
|
{
|
2018-09-26 22:09:39 +00:00
|
|
|
|
ModelFilePath = _options.ModelFilePath,
|
|
|
|
|
|
ModelDir = _options.ModelDir,
|
|
|
|
|
|
ModelName = _options.ModelName
|
2018-09-11 21:17:12 +00:00
|
|
|
|
};
|
2018-09-11 12:11:21 +00:00
|
|
|
|
|
2018-09-12 20:31:20 +00:00
|
|
|
|
_classifier.LoadModel(options);
|
|
|
|
|
|
|
|
|
|
|
|
var classes = _classifier.Classify(sentence, options);
|
2018-09-11 12:11:21 +00:00
|
|
|
|
|
2018-09-13 20:01:40 +00:00
|
|
|
|
classes = classes.OrderByDescending(x => x.Item2).ToList();
|
|
|
|
|
|
|
|
|
|
|
|
return classes;
|
2018-09-11 12:11:21 +00:00
|
|
|
|
}
|
2018-09-04 02:05:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|