001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
015: */
016:
017: /*
018: * SingleClassifierEnhancer.java
019: * Copyright (C) 2004 University of Waikato, Hamilton, New Zealand
020: *
021: */
022:
023: package weka.classifiers;
024:
025: import weka.classifiers.rules.ZeroR;
026: import weka.core.Capabilities;
027: import weka.core.Option;
028: import weka.core.OptionHandler;
029: import weka.core.Utils;
030: import weka.core.Capabilities.Capability;
031:
032: import java.util.Enumeration;
033: import java.util.Vector;
034:
035: /**
036: * Abstract utility class for handling settings common to meta
037: * classifiers that use a single base learner.
038: *
039: * @author Eibe Frank (eibe@cs.waikato.ac.nz)
040: * @version $Revision: 1.7 $
041: */
042: public abstract class SingleClassifierEnhancer extends Classifier {
043:
044: /** for serialization */
045: private static final long serialVersionUID = -3665885256363525164L;
046:
047: /** The base classifier to use */
048: protected Classifier m_Classifier = new ZeroR();
049:
050: /**
051: * String describing default classifier.
052: */
053: protected String defaultClassifierString() {
054:
055: return "weka.classifiers.rules.ZeroR";
056: }
057:
058: /**
059: * Returns an enumeration describing the available options.
060: *
061: * @return an enumeration of all the available options.
062: */
063: public Enumeration listOptions() {
064:
065: Vector newVector = new Vector(3);
066:
067: Enumeration enu = super .listOptions();
068: while (enu.hasMoreElements()) {
069: newVector.addElement(enu.nextElement());
070: }
071:
072: newVector
073: .addElement(new Option(
074: "\tFull name of base classifier.\n"
075: + "\t(default: "
076: + defaultClassifierString() + ")", "W",
077: 1, "-W"));
078:
079: newVector.addElement(new Option("", "", 0,
080: "\nOptions specific to classifier "
081: + m_Classifier.getClass().getName() + ":"));
082: enu = ((OptionHandler) m_Classifier).listOptions();
083: while (enu.hasMoreElements()) {
084: newVector.addElement(enu.nextElement());
085: }
086:
087: return newVector.elements();
088: }
089:
090: /**
091: * Parses a given list of options. Valid options are:<p>
092: *
093: * -W classname <br>
094: * Specify the full class name of the base learner.<p>
095: *
096: * Options after -- are passed to the designated classifier.<p>
097: *
098: * @param options the list of options as an array of strings
099: * @exception Exception if an option is not supported
100: */
101: public void setOptions(String[] options) throws Exception {
102:
103: super .setOptions(options);
104:
105: String classifierName = Utils.getOption('W', options);
106:
107: if (classifierName.length() > 0) {
108:
109: // This is just to set the classifier in case the option
110: // parsing fails.
111: setClassifier(Classifier.forName(classifierName, null));
112: setClassifier(Classifier.forName(classifierName, Utils
113: .partitionOptions(options)));
114: } else {
115:
116: // This is just to set the classifier in case the option
117: // parsing fails.
118: setClassifier(Classifier.forName(defaultClassifierString(),
119: null));
120: setClassifier(Classifier.forName(defaultClassifierString(),
121: Utils.partitionOptions(options)));
122: }
123: }
124:
125: /**
126: * Gets the current settings of the Classifier.
127: *
128: * @return an array of strings suitable for passing to setOptions
129: */
130: public String[] getOptions() {
131:
132: String[] classifierOptions = ((OptionHandler) m_Classifier)
133: .getOptions();
134: int extraOptionsLength = classifierOptions.length;
135: if (extraOptionsLength > 0) {
136: extraOptionsLength++; // for the double hyphen
137: }
138:
139: String[] super Options = super .getOptions();
140: String[] options = new String[super Options.length
141: + extraOptionsLength + 2];
142:
143: int current = 0;
144: options[current++] = "-W";
145: options[current++] = getClassifier().getClass().getName();
146:
147: System.arraycopy(super Options, 0, options, current,
148: super Options.length);
149: current += super Options.length;
150:
151: if (classifierOptions.length > 0) {
152: options[current++] = "--";
153: System.arraycopy(classifierOptions, 0, options, current,
154: classifierOptions.length);
155: }
156:
157: return options;
158: }
159:
160: /**
161: * Returns the tip text for this property
162: * @return tip text for this property suitable for
163: * displaying in the explorer/experimenter gui
164: */
165: public String classifierTipText() {
166: return "The base classifier to be used.";
167: }
168:
169: /**
170: * Returns default capabilities of the base classifier.
171: *
172: * @return the capabilities of the base classifier
173: */
174: public Capabilities getCapabilities() {
175: Capabilities result;
176:
177: if (getClassifier() != null)
178: result = getClassifier().getCapabilities();
179: else
180: result = new Capabilities(this );
181:
182: // set dependencies
183: for (Capability cap : Capability.values())
184: result.enableDependency(cap);
185:
186: result.setOwner(this );
187:
188: return result;
189: }
190:
191: /**
192: * Set the base learner.
193: *
194: * @param newClassifier the classifier to use.
195: */
196: public void setClassifier(Classifier newClassifier) {
197:
198: m_Classifier = newClassifier;
199: }
200:
201: /**
202: * Get the classifier used as the base learner.
203: *
204: * @return the classifier used as the classifier
205: */
206: public Classifier getClassifier() {
207:
208: return m_Classifier;
209: }
210:
211: /**
212: * Gets the classifier specification string, which contains the class name of
213: * the classifier and any options to the classifier
214: *
215: * @return the classifier string
216: */
217: protected String getClassifierSpec() {
218:
219: Classifier c = getClassifier();
220: return c.getClass().getName() + " "
221: + Utils.joinOptions(((OptionHandler) c).getOptions());
222: }
223: }
|