public override void ReadRules(string filepath) { //super crazy regex that should match any j48 rule Regex regexRuleStart = new Regex(@"([A-Za-z() |\.-])+(<|>|=|<=|>=) [0-9]+(\.[0-9]+)?(: [a-zA-Z]+ \([0-9]+(\.[0-9]+)?(\/[0-9]+(\.[0-9]+)?\))?)?"); using (StreamReader sr = new StreamReader(filepath)) { string currentLine; int ruleLevel = 0; int previousRuleLevel = -1; int currentRule = 0; Rules.Add(new Rule()); while ((currentLine = sr.ReadLine()) != null) { if (regexRuleStart.Match(currentLine).Success) { ruleLevel = currentLine.Count(c => c == '|'); //regex to match feature, operator, value, classification and accruacy of a rule Regex featureMatch = new Regex(@"([A-Za-z]+((( )|\.|-)*)?([(a-zA-z)]|[0-9]*)?(( )|\.|-)?)+"); Regex operatorMatch = new Regex(@"<=|>=|<|>|="); Regex valueMatch = new Regex(@"[0-9]+(\.[0-9]+)?"); string feature = featureMatch.Match(currentLine).ToString().Trim(); double value = Convert.ToDouble(valueMatch.Match(currentLine).ToString()); RuleItem ruleItem = new RuleItem(feature); ruleItem.SetOp(operatorMatch.Match(currentLine).ToString().Trim()); ruleItem.SetValue(Convert.ToDouble(valueMatch.Match(currentLine).ToString())); //If rule level is increasing, the ruleitem is part of the current rule. If not, create a new rule if (ruleLevel > previousRuleLevel) { Rules[currentRule].RuleItems.Add(ruleItem); } else { currentRule++; Rules.Add(new Rule()); for (int i = 0; i < ruleLevel; i++) { Rules[currentRule].RuleItems.Add(Rules[currentRule - 1].RuleItems[i]); } Rules[currentRule].RuleItems.Add(ruleItem); } SetClassificationAccuracy(currentLine, currentRule); previousRuleLevel = ruleLevel; } if (currentLine.Contains("Correctly Classified Instances")) { string accuracy = regexAccuracy.Match(currentLine).ToString(); Accuracy = Convert.ToDouble(accuracy.Remove(accuracy.Length - 1)); } } sr.Close(); } }
public override void ReadRules(string filepath) { //Regex for finding rules Regex regexRulesStart = new Regex(@"class [a-zA-z]+ IF : .*"); Regex regexClassification = new Regex(@"(?<=class\s).*(?=\sIF)"); Regex regexRuleItem = new Regex(@"([A-Za-z() _-]+)(=|<|>|<=|>=)([0-9](\.[0-9]))"); using (StreamReader sr = new StreamReader(filepath)) { string currentLine; int currentRule = 0; while ((currentLine = sr.ReadLine()) != null) { if (regexRulesStart.Match(currentLine).Success) { Rules.Add(new Rule()); Rules[currentRule].SetClassification(regexClassification.Match(currentLine).ToString()); //Regex for getting elemnts of rules Regex featureMatch = new Regex(pattern: @"([A-Za-z]+((( )|\.|-)*)?([(a-zA-Z)]|[0-9]*)?(( )|\.|-)?)+"); Regex operatorMatch = new Regex(pattern: @"<=|>=|<|>|="); Regex valueMatch = new Regex(pattern: @"[0-9]+\.[0-9]+"); //splitting up rule items, remove initial class text string[] ruleItems = currentLine.Split('^'); ruleItems[0] = Regex.Replace(ruleItems[0], @"class [a-zA-Z]+ IF : ", "").TrimStart(); foreach (var item in ruleItems) { MatchCollection ruleItemOperatorMatches = operatorMatch.Matches(item); MatchCollection ruleItemValueMatches = valueMatch.Matches(item); string op1 = ruleItemOperatorMatches[0].ToString(); string op2 = ""; if (ruleItemOperatorMatches.Count == 2 && ruleItemValueMatches.Count == 2) { op2 = ruleItemOperatorMatches[1].ToString(); if (ruleItemOperatorMatches[0].ToString() == "<=" || ruleItemOperatorMatches[0].ToString() == ">=") { op1 = ruleItemOperatorMatches[0].ToString().Replace('<', '>'); } } string feature = featureMatch.Match(item).ToString(); RuleItem ruleItem = new RuleItem(feature); double value = Convert.ToDouble(ruleItemValueMatches[0].ToString()); ruleItem.SetValue(value); if (ruleItemValueMatches.Count > 1) { double value2 = Convert.ToDouble(ruleItemValueMatches[1].ToString()); ruleItem.SetValue(value2); } ruleItem.SetOp(op1); if (ruleItemOperatorMatches.Count > 1) { ruleItem.SetOp(op2); } Rules[currentRule].RuleItems.Add(ruleItem); } currentRule++; } if (currentLine.Contains("Correctly Classified Instances")) { string accuracy = regexAccuracy.Match(currentLine).ToString(); Accuracy = Convert.ToDouble(accuracy.Remove(accuracy.Length - 1)); } } sr.Close(); } }
private List <Patient.Classification> WekaClassify() { //getting index of selected input patient int inputId = Convert.ToInt32(txtNum.Text); int inputIndex = inputList.FindIndex(x => x.ID == inputId); Patient selectedPatient = inputList[inputIndex]; Patient.Classification j48Classification = Patient.Classification.Unknown; Patient.Classification nngeClassification = Patient.Classification.Unknown; //iterating through models for (int modeli = 0; modeli < wekaModels.Count; modeli++) { //iterating through model rules for (int rulei = 0; rulei < wekaModels[modeli].Rules.Count; rulei++) { bool match = false; //iterating through ruleitems for (int ruleItemi = 0; ruleItemi < wekaModels[modeli].Rules[rulei].RuleItems.Count; ruleItemi++) { RuleItem ruleItem = wekaModels[modeli].Rules[rulei].RuleItems[ruleItemi]; int attributeIndex = patientFeatures.IndexOf(ruleItem.Feature) - 1; double patientValue = selectedPatient.GetSingleAttribute(attributeIndex); List <double> ruleVals = ruleItem.GetValues(); List <RuleItem.Operators> ruleOps = ruleItem.GetOps(); for (int i = 0; i < ruleVals.Count; i++) { if (ruleOps[i] == RuleItem.Operators.Equal) { if (patientValue == ruleVals[i]) { match = true; } else { match = false; break; } } else if (ruleOps[i] == RuleItem.Operators.Less) { if (patientValue < ruleVals[i]) { match = true; } else { match = false; break; } } else if (ruleOps[i] == RuleItem.Operators.Greater) { if (patientValue > ruleVals[i]) { match = true; } else { match = false; break; } } else if (ruleOps[i] == RuleItem.Operators.LessOrEqual) { if (patientValue <= ruleVals[i]) { match = true; } else { match = false; break; } } else if (ruleOps[i] == RuleItem.Operators.GreatorOrEqual) { if (patientValue >= ruleVals[i]) { match = true; } else { match = false; break; } } } if (!match) { break; } } if (match) { if (modeli == 0) { j48Classification = wekaModels[modeli].Rules[rulei].Classification; } else { nngeClassification = wekaModels[modeli].Rules[rulei].Classification; } break; } } } return(new List <Patient.Classification> { j48Classification, nngeClassification }); }
private List <Patient.Classification> WekaClassify() { //getting index of selected input patient int inputId = Convert.ToInt32(txtNum.Text); int inputIndex = inputList.FindIndex(x => x.ID == inputId); Patient selectedPatient = inputList[inputIndex]; List <Patient.Classification> j48Classification = new List <Patient.Classification>(); List <Patient.Classification> nngeClassification = new List <Patient.Classification>(); j48ClassifiedRule.Clear(); nngeClassifiedRule.Clear(); double j48Accuracy = 0; //iterating through models for (int modeli = 0; modeli < wekaModels.Count; modeli++) { if (modeli == 0) { inputList[inputIndex].J48ModelAccuracy = wekaModels[modeli].Accuracy; } else { inputList[inputIndex].NNgeAccuracy = wekaModels[modeli].Accuracy; } //iterating through model rules for (int rulei = 0; rulei < wekaModels[modeli].Rules.Count; rulei++) { bool match = false; //iterating through ruleitems for (int ruleItemi = 0; ruleItemi < wekaModels[modeli].Rules[rulei].RuleItems.Count; ruleItemi++) { RuleItem ruleItem = wekaModels[modeli].Rules[rulei].RuleItems[ruleItemi]; int attributeIndex = patientFeatures.IndexOf(ruleItem.Feature) - 1; double patientValue = selectedPatient.GetSingleAttribute(attributeIndex); List <double> ruleVals = ruleItem.GetValues(); List <RuleItem.Operators> ruleOps = ruleItem.GetOps(); for (int i = 0; i < ruleVals.Count; i++) { if (ruleOps[i] == RuleItem.Operators.Equal) { if (patientValue == ruleVals[i]) { match = true; } else { match = false; break; } } else if (ruleOps[i] == RuleItem.Operators.Less) { if (patientValue < ruleVals[i]) { match = true; } else { match = false; break; } } else if (ruleOps[i] == RuleItem.Operators.Greater) { if (patientValue > ruleVals[i]) { match = true; } else { match = false; break; } } else if (ruleOps[i] == RuleItem.Operators.LessOrEqual) { if (patientValue <= ruleVals[i]) { match = true; } else { match = false; break; } } else if (ruleOps[i] == RuleItem.Operators.GreatorOrEqual) { if (patientValue >= ruleVals[i]) { match = true; } else { match = false; break; } } } if (!match) { break; } } if (match) { if (modeli == 0) { j48Accuracy = wekaModels[modeli].Rules[rulei].Accuracy; inputList[inputIndex].J48RuleAccuracy = j48Accuracy; j48Classification.Add(wekaModels[modeli].Rules[rulei].Classification); j48ClassifiedRule.Add(rulei); } else { nngeClassification.Add(wekaModels[modeli].Rules[rulei].Classification); nngeClassifiedRule.Add(rulei); } } } } if (j48Classification.Count > 0) { inputList[inputIndex].J48Diagnosis = MostFrequentClass(j48Classification); } else { inputList[inputIndex].J48Diagnosis = Patient.Classification.Unknown; } if (nngeClassification.Count > 0) { inputList[inputIndex].NngeDiagnosis = MostFrequentClass(nngeClassification); } else { inputList[inputIndex].NngeDiagnosis = Patient.Classification.Unknown; } return(new List <Patient.Classification> { inputList[inputIndex].J48Diagnosis, inputList[inputIndex].NngeDiagnosis }); }