-
Notifications
You must be signed in to change notification settings - Fork 0
/
accord_svm.cs
576 lines (513 loc) · 22.5 KB
/
accord_svm.cs
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Accord.IO;
using Accord.MachineLearning.VectorMachines.Learning;
using Accord.Statistics;
using Accord.MachineLearning;
using System.IO;
using Accord.MachineLearning.VectorMachines;
using Accord.Statistics.Kernels;
using System.Net;
using System.Text.RegularExpressions;
using OpenNLP.Tools.SentenceDetect;
using Annytab.Stemmer;
using Accord.Statistics.Analysis;
using System.Diagnostics;
using Accord.MachineLearning.Performance;
using Accord.Math.Optimization.Losses;
using Word2vec.Tools;
/*
Machine Learning Application that performs Sentiment Classification(Fine-Grained, emotions)
with sparse text representations(TFIDF) or pre-trained dense word vectors(Word2Vec),
on Supervised Linear Model(SVM). The Classification result is later dumped
in local storage as Confusion Matrix.
*/
namespace SVM_MACHINE_LEARNING
{
/*
Support class for convenient dataset pre-process, extracting pairs
(text-emotion-line) from source dataset.
*/
public class pair
{
public int pair_line { get; set; }
public string pair_text { get; set; }
public string pair_emot { get; set; }
public override string ToString()
{
return "Sentence: " + this.pair_text + " Emotion: " + this.pair_emot + " LineNo: " + this.pair_line;
}
}
/*
SVM Logic Execution of One-Vs-Rest Classifier. Text Representations are either sparse(TFIDF) or dense
(Word2Vec) Vector Space Models(VSMs).
*/
class svm_execute
{
public static string[] model = System.Configuration.ConfigurationManager.AppSettings["model"].Split(',');
public static string output_location = System.Configuration.ConfigurationManager.AppSettings["output_location"];
private static List<string> Missing_Tokens = new List<string>();
static void Main(string[] args)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
svm_classifier Classifier = new svm_classifier();
List<ConfusionMatrix> conf_matrix = new List<ConfusionMatrix>();
string training_set = System.Configuration.ConfigurationManager.AppSettings["training_set"];
string testing_set = System.Configuration.ConfigurationManager.AppSettings["testing_set"];
switch (System.Configuration.ConfigurationManager.AppSettings["indexing_method"])
{
case "TFIDF":
TF_IDF Indexation = new TF_IDF();
Classifier.init_trainSamples(Indexation.Calculate("train_data"));
Classifier.init_testSamples(Indexation.Calculate("test_data"));
break;
case "W2V":
string w2v_preTrained = System.Configuration.ConfigurationManager.AppSettings["w2v_preTrained"];
W2V.load(w2v_preTrained);
Classifier.init_trainSamples(W2V.Vectorize(W2V.Extract_sentences("train_data")));
Classifier.init_testSamples(W2V.Vectorize(W2V.Extract_sentences("test_data")));
break;
}
Console.WriteLine("Training Data: " + Classifier.get_trainData().Count());
Console.WriteLine("Testing Data: " + Classifier.get_testData().Count());
Console.WriteLine("Begin Training! -- Trainset: " + training_set);
Console.WriteLine("_______________");
foreach (string _emotion in model)
Classifier.train(svm_preprocess.create_pairs("train_data"), _emotion);
Console.WriteLine("Begin Testing! -- Testset " + testing_set);
Console.WriteLine("_______________");
conf_matrix.Clear();
foreach (string _emotion in model)
conf_matrix.Add(Classifier.test(svm_preprocess.create_pairs("test_data"), _emotion));
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
Console.WriteLine("Serializing!");
Console.WriteLine("_______________");
ToFile.Dump(Path.Combine(output_location, "corpus[Train " + training_set +
" -- " + "Test " + testing_set + "]" + ".txt"), conf_matrix, model, elapsedTime);
}
}
// Sentiment Dataset pre-process, IO operations, Text Preprocess
static class svm_preprocess
{
internal static List<pair> create_pairs(string data_type)
{
string dataset_location = System.Configuration.ConfigurationManager.AppSettings["dataset_location"];
string training_set = System.Configuration.ConfigurationManager.AppSettings["training_set"];
string testing_set = System.Configuration.ConfigurationManager.AppSettings["testing_set"];
string d_index = System.Configuration.ConfigurationManager.AppSettings["delimeter"];
string training_folder = training_set + "/"; string testing_folder = testing_set + "/";
Dictionary<string, char> Delimeter = new Dictionary<string, char>()
{
{"alpha", '@'},
{"tab", '\t'},
{"whitespace", ' '}
};
List<pair> tmp_pairs = new List<pair>();
string file = "";
switch (data_type)
{
case "IDF_Dictionary":
file = dataset_location + training_folder + training_set + "[TFIDF]" + ".txt";
break;
case "train_data":
file = dataset_location + training_folder + training_set + "[train]" + ".txt";
break;
case "test_data":
file = dataset_location + testing_folder + testing_set + "[test]" + ".txt";
break;
default:
Console.WriteLine("Unavailable Dataset Location");
file = "FileNotFound";
break;
}
/*
Read The File and Extract the Emotion, Text Fields and File Line Fields
*/
int line_counter = 0;
string delimeter = System.Configuration.ConfigurationManager.AppSettings["delimeter"];
string emotion = System.Configuration.ConfigurationManager.AppSettings["emotion"];
string sentence = System.Configuration.ConfigurationManager.AppSettings["sentence"];
foreach (string line in File.ReadLines(@file))
{
++line_counter;
tmp_pairs.Add(new pair()
{
pair_emot = line.Split(Delimeter[d_index])[Int32.Parse(emotion)],
pair_text = line.Split(Delimeter[d_index])[Int32.Parse(sentence)],
pair_line = line_counter
});
}
return tmp_pairs;
}
/*
We use ReadAllLines method for the reason that the dataset we load on memory is countable/static
*/
internal static string[] Get_StopW()
{
return File.ReadAllLines(@"../../Dataset/StopWords_Filter.txt", Encoding.UTF8).ToArray();
}
internal static List<pair> PreProcess(List<pair> pair_list)
{
/*
Pre-processing Steps:
#) Apply Stop Words Removal
#) For non Sto-Words Strings perform Stemming and concatenate them into single String
#) Recycle the old entry
*/
int index_counter = 0;
List<string> str_dump = new List<string>();
IStemmer stemmer = new EnglishStemmer();
foreach (pair item in pair_list)
{
var Stop_Words = Get_StopW();
str_dump.Clear();
foreach (string word in item.pair_text.Split())
{
if (Stop_Words.Contains(word.ToLower()))
continue;
else
{
if (word.Length > 2)
{
if (System.Configuration.ConfigurationManager.AppSettings["indexing_method"] == "W2V")
str_dump.Add(word.ToLower());
else
str_dump.Add(stemmer.GetSteamWord(word.ToLower()));
}
else
continue;
}
}
// Concatenation & Recycling
pair_list[index_counter].pair_text = str_dump.Aggregate((i, j) => i + ' ' + j);
index_counter++;
}
return pair_list;
}
}
/*
Apply on demand different optimization functions(see S.M.O) on Linear
classifier's training algorithm(SMO, LCD), train-test serialization
and produce Confusion Matrix
*/
class svm_classifier
{
public List<double[]> train_sample = new List<double[]>();
public List<double[]> test_sample = new List<double[]>();
string Filepath = "";
List<int> svm_feed_test = new List<int>();
List<int> svm_feed_train = new List<int>();
string trained_location = System.Configuration.ConfigurationManager.AppSettings["trained_location"];
string kernel = System.Configuration.ConfigurationManager.AppSettings["kernel"];
SequentialMinimalOptimization<Linear> svm_learner_smo;
LinearCoordinateDescent<Linear> svm_learner_lcd;
dynamic learner;
List<double[]> idf_weights = new List<double[]>();
public svm_classifier() : base()
{
if (System.Configuration.ConfigurationManager.AppSettings["train_function"] == "SMO")
{
learner = new SequentialMinimalOptimization<Linear>()
{
UseComplexityHeuristic = true
};
}
else if (System.Configuration.ConfigurationManager.AppSettings["train_function"] == "LCD")
{
learner = new LinearCoordinateDescent<Linear>()
{
UseComplexityHeuristic = true
};
}
}
public void init_trainSamples(List<double[]> train_set) {
this.train_sample = train_set;
}
public void init_testSamples(List<double[]> test_set) {
this.test_sample = test_set;
}
public void train(List<pair> train_data, string emotion)
{
if (Boolean.Parse(System.Configuration.ConfigurationManager.AppSettings["rep_results"]) == true)
Accord.Math.Random.Generator.Seed = 0;
this.svm_feed_train.Clear();
for (int _counter = 0; _counter < train_data.Count; _counter++)
{
if (train_data[_counter].pair_emot == emotion)
this.svm_feed_train.Add(1);
else
this.svm_feed_train.Add(0);
}
if (Boolean.Parse(System.Configuration.ConfigurationManager.AppSettings["use_weights"]) == true)
learner.WeightRatio = Double.Parse(System.Configuration.ConfigurationManager.AppSettings["weight_ratio"]);
SupportVectorMachine<Linear> svm = learner.Learn(this.train_sample.ToArray(),this.svm_feed_train.ToArray());
this.Filepath = Path.Combine(trained_location, emotion + "__" + kernel);
Serializer.Save(svm, this.Filepath);
}
public ConfusionMatrix test(List<pair> test_data, string emotion)
{
if (Boolean.Parse(System.Configuration.ConfigurationManager.AppSettings["rep_results"]) == true)
Accord.Math.Random.Generator.Seed = 0;
this.Filepath = Path.Combine(trained_location, emotion + "__" + kernel);
SupportVectorMachine<Linear> svm = Serializer.Load<SupportVectorMachine<Linear>>(this.Filepath);
bool[] prediction = svm.Decide(this.test_sample.ToArray());
int[] results = prediction.ToZeroOne();
this.svm_feed_test.Clear();
for (int _counter = 0; _counter < test_data.Count; _counter++)
{
if (test_data[_counter].pair_emot == emotion)
this.svm_feed_test.Add(1);
else
this.svm_feed_test.Add(0);
}
return (new ConfusionMatrix(results, this.svm_feed_test.ToArray(), 1, 0));
}
public List<double[]> get_trainData()
{
return this.train_sample;
}
public List<double[]> get_testData()
{
return this.test_sample;
}
}
#region l2norm + ToFile
public static class L2Norm
{
internal static double[] Normalize(double[] vector)
{
List<double> result = new List<double>();
double sumSquared = 0;
foreach (var value in vector)
sumSquared += value * value;
double SqrtSumSquared = Math.Sqrt(sumSquared);
foreach (var value in vector)
{
// L2-norm: Xi = Xi / Sqrt(X0^2 + X1^2 + .. + Xn^2)
result.Add(value / SqrtSumSquared);
}
return result.ToArray();
}
}
/*
Dump To File, Confusion Matrix elements(EM, TP, TN, FN, PR, RE, F1)
*/
public static class ToFile
{
internal static void Dump(string pathToFile, List<ConfusionMatrix> conf_matrix, string[] Emotions, string elapsedT)
{
int counter = 0;
using (StreamWriter outputFile = new StreamWriter(pathToFile, false))
{
outputFile.Write("EM" + "\t");
outputFile.Flush();
outputFile.Write("TP" + "\t");
outputFile.Flush();
outputFile.Write("FP" + "\t");
outputFile.Flush();
outputFile.Write("TN" + "\t");
outputFile.Flush();
outputFile.Write("FN" + "\t");
outputFile.Flush();
outputFile.Write("PR" + "\t");
outputFile.Flush();
outputFile.Write("RE" + "\t");
outputFile.Flush();
outputFile.Write("F1" + "\t");
outputFile.Flush();
outputFile.WriteLine();
foreach (ConfusionMatrix cm in conf_matrix)
{
outputFile.Write(Emotions[counter++] + "\t");
outputFile.Flush();
outputFile.Write(cm.TruePositives + "\t");
outputFile.Flush();
outputFile.Write(cm.FalsePositives + "\t");
outputFile.Flush();
outputFile.Write(cm.TrueNegatives + "\t");
outputFile.Flush();
outputFile.Write(cm.FalseNegatives + "\t");
outputFile.Flush();
outputFile.Write(String.Format("{0:0.00}", cm.Precision) + "\t");
outputFile.Flush();
outputFile.Write(String.Format("{0:0.00}", cm.Recall) + "\t");
outputFile.Flush();
outputFile.Write(String.Format("{0:0.00}", cm.FScore) + "\t");
outputFile.Flush();
outputFile.WriteLine();
}
outputFile.Flush();
outputFile.WriteLine("Runtime:__ " + elapsedT);
outputFile.Close();
}
}
}
#endregion
//Custom TFIDF Implementation
#region TFIDF
public class TF_IDF
{
TFIDF codebook;
public TF_IDF()
{
string[][] sentences = { };
string data_type = "IDF_Dictionary";
// Initialize TFIDF
this.codebook = new TFIDF()
{
Tf = TermFrequency.Log,
Idf = InverseDocumentFrequency.Default
};
sentences = Extract_sentences(data_type);
// TFIDF Document Train
codebook.Learn(sentences);
}
public string[][] Extract_sentences(string data_type)
{
List<string> _text = new List<string>();
foreach (pair tmp_pair in svm_preprocess.create_pairs(data_type))
_text.Add(tmp_pair.pair_text);
return (string[][])_text.ToArray<string>().Tokenize();
}
public List<double[]> Calculate(string dataset)
{
List<double[]> idf_weights = new List<double[]>();
List<double> _preprocessing = new List<double>();
string[][] sentences = { };
sentences = Extract_sentences(dataset);
int _counter = 0;
idf_weights.Clear();
do
{
_preprocessing.Clear();
foreach (double _temp in codebook.Transform(sentences[_counter]))
_preprocessing.Add(_temp);
idf_weights.Add(_preprocessing.ToArray<double>());
} while (_counter++ < sentences.Length - 1);
//L2 Normalization
_counter = 0;
do
{
idf_weights[_counter] = L2Norm.Normalize(idf_weights[_counter]);
} while (_counter++ < idf_weights.Count - 1);
return idf_weights;
}
}
#endregion
#region W2V
//Word2Vec pre-trained word vector variant for Text Representation, instead of TFIDF
public static class W2V
{
static List<string> Missing_Tokens = new List<string>();
static Vocabulary w2v_vocabulary;
static double mw_percentage, tokens, miss_tokens = 0.0;
public static void load(string path) {
w2v_vocabulary = new Word2VecTextReader().Read(path);
}
public static List<string[]> Extract_sentences(string data_type)
{
List<string[]> _text = new List<string[]>();
foreach (pair tmp_pair in svm_preprocess.create_pairs(data_type))
_text.Add(tmp_pair.pair_text.Split());
return _text;
}
public static List<double[]> Vectorize(List<string[]> Document)
{
List<double[]> reslt = new List<double[]>();
foreach (string[] sentence in Document)
{
List<double> sentence_vec = new List<double>();
List<double[]> wrd_vec = new List<double[]>();
foreach (string w in sentence)
{
tokens++;
if (w2v_vocabulary.ContainsWord(w))
wrd_vec.Add(Array.ConvertAll<float,double>(w2v_vocabulary.GetRepresentationFor(w).NumericVector, x => (double)x));
else if (!Missing_Tokens.Contains(w))
Missing_Tokens.Add(w);
else
miss_tokens++;
}
for (int i = 0; i<w2v_vocabulary.VectorDimensionsCount; i++)
{
if (wrd_vec.Count >= 1)
{
double avg = 0;
for (int j = 0; j < wrd_vec.Count; j++)
{
avg = (double)wrd_vec[j][i] + (double)avg;
//Averaging
if (j + 1 == wrd_vec.Count)
avg = (double)(avg / (double)wrd_vec.Count);
}
sentence_vec.Add(avg);
}
else
{
sentence_vec.Add((double)0);
}
}
reslt.Add(sentence_vec.ToArray());
}
return reslt;
}
}
#endregion
}
//________________________________ CROSSVALIDATION ________________________________________
this.crossvalidation = new Accord.MachineLearning.Performance.CrossValidation<SupportVectorMachine<Linear, double[]>, double[]>()
// If needed, control the parallelization degree
this.crossvalidation.ParallelOptions.MaxDegreeOfParallelism = 1;
{
// Cross-Validation Folds
K = 10,
// Indicate how learning algorithms for the models should be created
Learner = (s) => new SequentialMinimalOptimization<Linear, double[]>()
{
Complexity = 100,
UseKernelEstimation = true
},
// Indicate how the performance of those models will be measured
Loss = (expected, actual, p) => new Accord.Math.Optimization.Losses.ZeroOneLoss(expected).Loss(actual),
Stratify = false, // do not force balancing of classes
};
/* _________________________________TFIDF-DEBUG___________________________________________
int _counter_line = 0;
int _counter_2 = 0;
int _counter_zero = 0;
int _counter_NewLine = 75;
do
{
_counter_2 = 0;
_counter_zero = 0;
foreach (double idf_element in idf_weights[_counter_line++])
{
if (idf_element == 0)
_counter_zero++;
if (_counter_2 % _counter_NewLine == 0)
Console.WriteLine();
Console.Write(" " + Math.Round(idf_element, 2));
_counter_2++;
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Dict Elements: " + _counter_2);
Console.WriteLine("Non-Zero Elements: " + _counter_zero);
Console.WriteLine("Line No: " + _counter_line);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
} while (_counter_line < _counter);
Console.Read();
Console.Read();
Console.Read();
*/