001 /* 002 * Created on Aug 2, 2005 003 * 004 */ 005 package aima.learning.inductive; 006 007 /** 008 * @author Ravi Mohan 009 * 010 */ 011 import java.util.Hashtable; 012 013 import aima.learning.framework.DataSet; 014 import aima.learning.framework.Example; 015 016 public class DLTest { 017 018 // represents a single test in the Decision List 019 private Hashtable<String, String> attrValues; 020 021 public DLTest() { 022 attrValues = new Hashtable<String, String>(); 023 } 024 025 public void add(String nta, String ntaValue) { 026 attrValues.put(nta, ntaValue); 027 028 } 029 030 public boolean matches(Example e) { 031 for (String key : attrValues.keySet()) { 032 if (!(attrValues.get(key).equals(e.getAttributeValueAsString(key)))) { 033 return false; 034 } 035 } 036 return true; 037 // return e.targetValue().equals(targetValue); 038 } 039 040 public DataSet matchedExamples(DataSet ds) { 041 DataSet matched = ds.emptyDataSet(); 042 for (Example e : ds.examples) { 043 if (matches(e)) { 044 matched.add(e); 045 } 046 } 047 return matched; 048 } 049 050 public DataSet unmatchedExamples(DataSet ds) { 051 DataSet unmatched = ds.emptyDataSet(); 052 for (Example e : ds.examples) { 053 if (!(matches(e))) { 054 unmatched.add(e); 055 } 056 } 057 return unmatched; 058 } 059 060 @Override 061 public String toString() { 062 StringBuffer buf = new StringBuffer(); 063 buf.append("IF "); 064 for (String key : attrValues.keySet()) { 065 buf.append(key + " = "); 066 buf.append(attrValues.get(key) + " "); 067 } 068 buf.append(" DECISION "); 069 return buf.toString(); 070 } 071 072 }