Repository navigation
Expand file tree
/
Copy pathClassifierFactory.cs
More file actions
69 lines (53 loc) · 2.08 KB
/
Copy pathClassifierFactory.cs
File metadata and controls
69 lines (53 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using Bigtree.Algorithm.Features;
using BotSharp.NLP.Tokenize;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace BotSharp.NLP.Classify
{
public class ClassifierFactory<IFeatureExtractor>
where IFeatureExtractor : ITextFeatureExtractor, new()
{
private SupportedLanguage _lang;
private IClassifier _classifier;
private ClassifyOptions _options;
private IFeatureExtractor featureExtractor;
public ClassifierFactory(ClassifyOptions options, SupportedLanguage lang)
{
_lang = lang;
_options = options;
featureExtractor = new IFeatureExtractor();
}
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;
}
public void Train(List<Sentence> sentences)
{
_classifier.Train(sentences, _options);
_classifier.SaveModel(_options);
}
public List<Tuple<string, double>> Classify(Sentence sentence)
{
var options = new ClassifyOptions
{
ModelFilePath = _options.ModelFilePath,
ModelDir = _options.ModelDir,
ModelName = _options.ModelName
};
_classifier.LoadModel(options);
var classes = _classifier.Classify(sentence, options);
classes = classes.OrderByDescending(x => x.Item2).ToList();
return classes;
}
}
}