Repository navigation
Expand file tree
/
Copy pathSVMClassifier.cs
More file actions
235 lines (188 loc) · 8.52 KB
/
Copy pathSVMClassifier.cs
File metadata and controls
235 lines (188 loc) · 8.52 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* BotSharp.NLP Library
* Copyright (C) 2018 Bo Peng
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Bigtree.Algorithm.Features;
using Bigtree.Algorithm.SVM;
using BotSharp.NLP.Featuring;
using BotSharp.NLP.Txt2Vec;
using Newtonsoft.Json;
using Txt2Vec;
namespace BotSharp.NLP.Classify
{
/// <summary>
/// This is a simple (naive) classification method based on Support Vector Machine (SVM)
/// </summary>
public class SVMClassifier : IClassifier
{
private List<string> features;
private List<Tuple<string, int>> dictionary;
private List<string> categories;
private RangeTransform transform;
private Bigtree.Algorithm.SVM.Model model;
private List<string> featuresInTfIdf;
public void Train(List<Sentence> sentences, ClassifyOptions options)
{
SVMClassifierTrain(sentences, options);
}
public void SVMClassifierTrain(List<Sentence> sentences, ClassifyOptions options, SvmType svm = SvmType.C_SVC, KernelType kernel = KernelType.RBF, bool probability = true, string outputFile = null)
{
var tfidf = new TfIdfFeatureExtractor();
tfidf.Dimension = options.Dimension;
tfidf.Sentences = sentences;
tfidf.CalBasedOnCategory();
featuresInTfIdf = tfidf.Keywords();
// copy test multiclass Model
Problem train = new Problem();
train.X = GetData(sentences, options).ToArray();
train.Y = GetLabels(sentences).ToArray();
train.Count = train.X.Count();
train.MaxIndex = train.X[0].Count();//int.MaxValue;
Parameter param = new Parameter();
transform = RangeTransform.Compute(train);
Problem scaled = transform.Scale(train);
param.Gamma = 1.0 / 3;
param.SvmType = svm;
param.KernelType = kernel;
param.Probability = probability;
int numberOfClasses = train.Y.OrderBy(x => x).Distinct().Count();
if (numberOfClasses == 1)
{
Console.Write("Number of classes must greater than one!");
}
if (svm == SvmType.C_SVC)
{
for (int i = 0; i < numberOfClasses; i++)
param.Weights[i] = 1;
}
model = Training.Train(scaled, param);
Console.Write("Training finished!");
}
public List<Tuple<string, double>> Classify(Sentence sentence, ClassifyOptions options)
{
var categoryList = new List<Tuple<string, double>>();
var result = Predict(sentence, options).FirstOrDefault();
for(int i = 0; i < result.Length; i++)
{
categoryList.Add(new Tuple<string, double>(categories[i], result[i]));
}
return categoryList;
}
public double[][] Predict(Sentence sentence, ClassifyOptions options)
{
Problem predict = new Problem();
predict.X = GetData(new List<Sentence> { sentence }, options).ToArray();
predict.Y = new double[1];
predict.Count = predict.X.Count();
predict.MaxIndex = features.Count;
transform = options.Transform;
Problem scaled = transform.Scale(predict);
return Prediction.PredictLabelsProbability(model, scaled);
}
public List<double> GetLabels(List<Sentence> sentences)
{
categories = sentences.Select(x => x.Label).Distinct().OrderBy(x => x).ToList();
List<double> labels = new List<double>();
foreach (var sentence in sentences)
{
var labelId = categories.IndexOf(sentence.Label).ToString();
labels.Add(double.Parse(labelId));
}
return labels;
}
public List<Node[]> GetData(List<Sentence> sentences, ClassifyOptions options)
{
//var extractor = new CountFeatureExtractor();
var extractor = new Word2VecFeatureExtractor();
extractor.ModelFile = options.Word2VecFilePath;
extractor.Sentences = sentences;
if(features != null)
{
extractor.Features = features;
}
if(dictionary != null)
{
extractor.Dictionary = dictionary;
}
extractor.Vectorize(featuresInTfIdf);
if(features == null)
{
features = extractor.Features;
}
if(dictionary == null)
{
dictionary = extractor.Dictionary;
}
List<Node[]> datas = new List<Node[]>();
foreach (var sentence in sentences)
{
List<Node> curNodes = new List<Node>();
for(int i = 0; i < extractor.Features.Count; i++)
{
int name = i;
/*var xx = sentence.Words.Find(x => x.Lemma == extractor.Features[i]);
if (xx == null)
{
curNodes.Add(new Node(name, 0));
}
else
{
curNodes.Add(new Node(name, xx.Vector));
}*/
curNodes.Add(new Node(i, sentence.Vector[i]));
}
datas.Add(curNodes.ToArray());
}
return datas;
}
public string SaveModel(ClassifyOptions options)
{
options.TransformFilePath = Path.Combine(options.ModelDir, "transform");
options.FeaturesFileName = Path.Combine(options.ModelDir, "features");
options.DictionaryFileName = Path.Combine(options.ModelDir, "dictionary");
options.CategoriesFileName = Path.Combine(options.ModelDir, "categories");
options.FeaturesInTfIdfFileName = Path.Combine(options.ModelDir, "featuresInTfIdf");
File.WriteAllText(options.FeaturesFileName, JsonConvert.SerializeObject(features));
File.WriteAllText(options.FeaturesInTfIdfFileName, JsonConvert.SerializeObject(featuresInTfIdf));
File.WriteAllText(options.DictionaryFileName, JsonConvert.SerializeObject(dictionary));
File.WriteAllText(options.CategoriesFileName, JsonConvert.SerializeObject(categories));
RangeTransform.Write(options.TransformFilePath, transform);
Bigtree.Algorithm.SVM.Model.Write(options.ModelFilePath, model);
return options.ModelFilePath;
}
object IClassifier.LoadModel(ClassifyOptions options)
{
options.FeaturesFileName = Path.Combine(options.ModelDir, "features");
options.DictionaryFileName = Path.Combine(options.ModelDir, "dictionary");
options.ModelFilePath = Path.Combine(options.ModelDir, options.ModelName);
options.TransformFilePath = Path.Combine(options.ModelDir, "transform");
options.CategoriesFileName = Path.Combine(options.ModelDir, "categories");
options.FeaturesInTfIdfFileName = Path.Combine(options.ModelDir, "featuresInTfIdf");
features = JsonConvert.DeserializeObject<List<String>>(File.ReadAllText(options.FeaturesFileName));
featuresInTfIdf = JsonConvert.DeserializeObject<List<String>>(File.ReadAllText(options.FeaturesInTfIdfFileName));
dictionary = JsonConvert.DeserializeObject<List<Tuple<string, int>>>(File.ReadAllText(options.DictionaryFileName));
categories = JsonConvert.DeserializeObject<List<String>>(File.ReadAllText(options.CategoriesFileName));
model = Bigtree.Algorithm.SVM.Model.Read(options.ModelFilePath);
options.Transform = RangeTransform.Read(options.TransformFilePath);
return model;
}
}
}