public void Test(string testFile, string resultFile) { StreamWriter sw = new StreamWriter(resultFile); sw.AutoFlush = true; // write the first line, which specifies names of attributes foreach (Attribute attr in Globals.attrs) sw.Write(attr.name + "\t"); sw.WriteLine(); StreamReader sr = new StreamReader(testFile); sr.ReadLine(); // ignore first line string line = null; while ((line = sr.ReadLine()) != null) { string[] words = line.Split('\t'); Tuple t = new Tuple(); for (int i=0; i<words.Length; ++i) { t.values[i] = Globals.attrs[i].values[words[i]]; sw.Write(words[i] + "\t"); } int classLabel = ClassifyTuple(t); Attribute classAttr = Globals.attrs[Globals.attrCount-1]; foreach (string val in classAttr.values.Keys) { if (classLabel == classAttr.values[val]) { sw.WriteLine(val); } } } sr.Close(); sw.Close(); }
public void AddTuple(Tuple t) { tuples.Add(t); }
public int Traverse(Tuple t) { int attrIdx = splitAttrIdx; int attrVal = t.values[attrIdx]; /* When there are no child nodes */ if (childNodes.Count == 0) return classLabel; // majority voting foreach (Node childNode in childNodes) { if (childNode.splitAttrVal == attrVal) { return childNode.Traverse(t); } } /* Whgen not matched value with child nodes */ return classLabel; // majority voting }
/* Add tuples to the root node */ private void AddTuplesForRootNode() { // return to start position trainSr.BaseStream.Position = 0; trainSr.DiscardBufferedData(); trainSr.ReadLine(); // ignore the first line string line = null; while ((line = trainSr.ReadLine()) != null) { Tuple t = new Tuple(); string[] words = line.Split('\t'); for (int i=0, length=Globals.attrCount; i<length; ++i) { try { t.values[i] = Globals.attrs[i].values[words[i]]; } catch (IndexOutOfRangeException e) { Console.WriteLine(e.StackTrace); System.Environment.Exit(-1); } } rootNode.AddTuple(t); } }
public int ClassifyTuple(Tuple t) { return rootNode.Traverse(t); }