public Arff Parse(StreamReader reader) { string line = null; Arff arff = new Arff (); bool parsingData = false; int attributeIdx = 0; while ((line = reader.ReadLine()) != null) { if (relation.IsMatch (line)) { Match match = relation.Match (line); arff.Relation = new Relation (match.Value); continue; } if (attribute.IsMatch (line)) { Match match = attribute.Match (line); string name = match.Groups ["name"].Value; List<Value> values = match .Groups ["values"] .Value .Split (new char[] {','}, StringSplitOptions.None) .ToList () .Select (x => new Value(x.Trim ())) .ToList(); var att = new Attribute (attributeIdx, name, values); arff.Attributes.Add (att); arff.Target = att; attributeIdx++; continue; } if (data.IsMatch (line)) { parsingData = true; continue; } if (parsingData) { arff.Data.Add (new Data (arff.Attributes, line)); continue; } } if (arff.Relation == null || arff.Attributes.Count == 0 || arff.Data.Count == 0) { throw new ParserException (); } // Remove the last attribute which is the Target attribute... arff.Attributes.RemoveAt (arff.Attributes.Count - 1); return arff; }
public static void Main(string[] args) { if (args.Length > 2 || args.Length == 0) { Console.WriteLine("Bad usage. Use:\t./decision-tree.exe <arff file> \t[prune]"); Console.WriteLine("Example: \t./decision-tree.exe data/example.arff \tprune"); Environment.Exit(1); } bool prune = false; string filename = args [0]; if (args.Length == 2) { prune = args [1].Equals("prune"); if (!prune) { Console.WriteLine("Pruning is disabled, could not parse \"prune\"."); } } if (!File.Exists(filename)) { Console.WriteLine("File \"{0}\" does not exist.", filename); Environment.Exit(1); } Console.WriteLine("File: \t\t{0}\nPruning: \t{1}", filename, prune); ArffReader reader = new ArffReader(); Arff arff = reader.Parse(new StreamReader(filename)); Classifier c = new Classifier(arff, prune); c.DrawTree(); }
public Classifier(Arff arff, bool prune = false) { this.arff = arff; this.builder = new DecisionBuilder(this.arff); this.prune = prune; }
public DecisionBuilder(Arff arff) { this.arff = arff; }
public Classifier(Arff arff, bool prune=false) { this.arff = arff; this.builder = new DecisionBuilder (this.arff); this.prune = prune; }
public Arff Parse(StreamReader reader) { string line = null; Arff arff = new Arff(); bool parsingData = false; int attributeIdx = 0; while ((line = reader.ReadLine()) != null) { if (relation.IsMatch(line)) { Match match = relation.Match(line); arff.Relation = new Relation(match.Value); continue; } if (attribute.IsMatch(line)) { Match match = attribute.Match(line); string name = match.Groups ["name"].Value; List <Value> values = match .Groups ["values"] .Value .Split(new char[] { ',' }, StringSplitOptions.None) .ToList() .Select(x => new Value(x.Trim())) .ToList(); var att = new Attribute(attributeIdx, name, values); arff.Attributes.Add(att); arff.Target = att; attributeIdx++; continue; } if (data.IsMatch(line)) { parsingData = true; continue; } if (parsingData) { arff.Data.Add(new Data(arff.Attributes, line)); continue; } } if (arff.Relation == null || arff.Attributes.Count == 0 || arff.Data.Count == 0) { throw new ParserException(); } // Remove the last attribute which is the Target attribute... arff.Attributes.RemoveAt(arff.Attributes.Count - 1); return(arff); }
public DecisionBuilder(Arff arff) { this.arff = arff; }