private void Train(HashSet <string> options) { if (options == null) { options = new HashSet <string>(new string [] { "b" }); } // train with bayes model if (options.Contains("b")) { var trainer = new BayesModel((string)GlobalParameter.Get(DefaultParameter.Field.train_feature_file), (string)GlobalParameter.Get(DefaultParameter.Field.model_file)); try { trainer.Train(); } catch (Exception e) { Console.WriteLine("Error occur during train for " + e.Message); throw new Exception(); } } }
public void Test() { if (model == null) { Initial(); } var fields = BayesModel.GetFields(sourceFile); FileReader reader = new LargeFileReader(sourceFile); FileWriter writer = new LargeFileWriter(resultFile, FileMode.Create); // actual label-->(prediced label-->times) var detailDic = new Dictionary <string, Dictionary <string, int> >(); var positiveNums = new Dictionary <string, int>(); // positive number by type var predictedNums = new Dictionary <string, int>(); // predicted number by type var actualNums = new Dictionary <string, int>(); // actual number by type Dictionary <string, int> dic = null; Pair <string, Dictionary <string, object> > feature = null; var i = 0; while ((feature = BayesModel.GetFeatureItem(reader, fields)) != null) { i++; var label = feature.first; string predictedLabel = null; try { predictedLabel = Predict(feature.second); } catch (Exception) { Console.WriteLine("Wrong!"); writer.WriteLine(i + "\t" + label + "\tNULL"); continue; } writer.Write(string.Format("{0}\t{1, -30}", i, label)); foreach (var score in this.scores) { writer.Write(string.Format("{0,30}:{1,-10:F2}", score.first, score.second)); } writer.Write("\r"); if (label.Equals(predictedLabel)) { try { positiveNums[label] += 1; } catch (Exception) { positiveNums[label] = 1; } } try { // update predicted number predictedNums[predictedLabel] += 1; } catch (Exception) { predictedNums[predictedLabel] = 1; } try { // update actually number actualNums[label] += 1; } catch (Exception) { actualNums[label] = 1; } // update detail dictionary try { dic = detailDic[label]; } catch (Exception) { dic = new Dictionary <string, int>(); detailDic[label] = dic; } try { dic[predictedLabel] += 1; } catch (Exception) { dic[predictedLabel] = 1; } } var buffer = new StringBuilder(); buffer.Append(string.Format("{0,-30}", "actual label |predicted type")); foreach (var key in this.labels) { buffer.Append(string.Format("{0,-30}", key)); } buffer.Append(string.Format("{0,-30}\r", "recall")); foreach (var key in this.labels) { buffer.Append(string.Format("{0,-30}", key)); dic = detailDic[key]; foreach (var k in this.labels) { buffer.Append(string.Format("{0,-30}", dic[k])); } // recall buffer.Append(string.Format("{0,-30}\r", 1.0 * positiveNums[key] / actualNums[key])); } buffer.Append(string.Format("{0,-30}", "precision")); foreach (var key in this.labels) { buffer.Append(string.Format("{0,-30:f5}", 1.0 * positiveNums[key] / predictedNums[key])); } buffer.Append("\r"); writer.WriteLine(buffer.ToString()); writer.Close(); }