/// <summary> /// Writes the results to .csv file /// </summary> /// <param name="testingSet">Testing set with given special coverages</param> public void WriteResults(TestingSet testingSet) { StringBuilder csv = new StringBuilder(); //String builder for csv file with results csv.AppendLine("id;specialCoverage"); //Header line foreach (Article article in testingSet.articles.Values) { csv.AppendLine(string.Format("{0};{1}", article.id[0], article.specialCoverage[0])); } File.WriteAllText(@path, csv.ToString()); }
/// <summary> /// Constructor that creates object with given training set and testing set /// </summary> /// <param name="trainingSet">Training set loaded from a file</param> /// <param name="testingSet">Testing set loaded from a file</param> public SVMClassifier(TrainingSet trainingSet, TestingSet testingSet) { this.trainingSet = trainingSet; vocabulary = new HashSet <string>(); x = new List <string>(); y = new List <double>(); foreach (Article article in trainingSet.articles.Values) //load data from the training set { string features = ArticleFeatures(article); //add features and special coverages to lists AddFeaturesToVocabulary(features); x.Add(features); y.Add(article.specialCoverage[0]); } foreach (Article article in testingSet.articles.Values) //load articles with given specialCoverage from the testing set { if (article.specialCoverage != null) { string features = ArticleFeatures(article); //add features and special coverages to lists AddFeaturesToVocabulary(features); x.Add(features); y.Add(article.specialCoverage[0]); } } //create new problem ProblemBuilder problemBuilder = new ProblemBuilder(); var problem = problemBuilder.CreateProblem(x, y.ToArray(), vocabulary.ToList()); //create new model using linear kernel const int C = 1; //C parameter for C_SVC model = new C_SVC(problem, KernelHelper.LinearKernel(), C); }
static void Main(string[] args) { trainingSet = new TrainingSet(); testingSet = new TestingSet(); System.Console.WriteLine("Data loaded"); System.Console.WriteLine("Training set: " + trainingSet.articles.Count); System.Console.WriteLine("Testing set: " + testingSet.articles.Count); sureClassifiers = new SureClassifiers(trainingSet, testingSet); //1. Looking for previous articles foreach (Article article in testingSet.articles.Values.OrderBy(key => key.id[0])) { if (article.specialCoverage == null) //if article doesn't have specialCoverage { int specialCoverage = sureClassifiers.PreviousArticle(article); if (specialCoverage > 0) //if specialCoverage was given by the method { article.specialCoverage = new int[1]; article.specialCoverage[0] = specialCoverage; } } } //2. Looking for next articles foreach (Article article in testingSet.articles.Values.OrderByDescending(key => key.id[0])) { if (article.specialCoverage == null) //if article doesn't have specialCoverage { int specialCoverage = sureClassifiers.NextArticle(article); if (specialCoverage > 0) //if specialCoverage was given by the method { article.specialCoverage = new int[1]; article.specialCoverage[0] = specialCoverage; } } } //3. Looking for related articles foreach (Article article in testingSet.articles.Values.OrderBy(key => key.id[0])) { if (article.specialCoverage == null) //if article doesn't have specialCoverage { int specialCoverage = sureClassifiers.RelatedArticles(article); if (specialCoverage > 0) //if specialCoverage was given by the method { article.specialCoverage = new int[1]; article.specialCoverage[0] = specialCoverage; } } } // 4. Getting special coverage from a SVM classifier svmClassifier = new SVMClassifier(trainingSet, testingSet); foreach (Article article in testingSet.articles.Values) { if (article.specialCoverage == null) //if article doesn't have specialCoverage { article.specialCoverage = new int[1]; article.specialCoverage[0] = svmClassifier.Classify(article); } } System.Console.WriteLine("Special coverages given"); //Write the results to .csv ResultsWriter resultsWriter = new ResultsWriter(); resultsWriter.WriteResults(testingSet); System.Console.WriteLine("Results written"); System.Console.ReadKey(); }