source: branches/MetisMQI/src/main/java/weka/classifiers/SingleClassifierEnhancer.java @ 34

Last change on this file since 34 was 29, checked in by gnappo, 14 years ago

Taggata versione per la demo e aggiunto branch.

File size: 6.1 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 *    SingleClassifierEnhancer.java
19 *    Copyright (C) 2004 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.classifiers;
24
25import weka.classifiers.rules.ZeroR;
26import weka.core.Capabilities;
27import weka.core.Option;
28import weka.core.OptionHandler;
29import weka.core.Utils;
30import weka.core.Capabilities.Capability;
31
32import java.util.Enumeration;
33import java.util.Vector;
34
35/**
36 * Abstract utility class for handling settings common to meta
37 * classifiers that use a single base learner.
38 *
39 * @author Eibe Frank (eibe@cs.waikato.ac.nz)
40 * @version $Revision: 6041 $
41 */
42public abstract class SingleClassifierEnhancer extends AbstractClassifier {
43
44  /** for serialization */
45  private static final long serialVersionUID = -3665885256363525164L;
46
47  /** The base classifier to use */
48  protected Classifier m_Classifier = new ZeroR();
49
50  /**
51   * String describing default classifier.
52   */
53  protected String defaultClassifierString() {
54
55    return "weka.classifiers.rules.ZeroR";
56  }
57
58  /**
59   * Returns an enumeration describing the available options.
60   *
61   * @return an enumeration of all the available options.
62   */
63  public Enumeration listOptions() {
64
65    Vector newVector = new Vector(3);
66
67    Enumeration enu = super.listOptions();
68    while (enu.hasMoreElements()) {
69      newVector.addElement(enu.nextElement());
70    }
71
72    newVector.addElement(new Option(
73          "\tFull name of base classifier.\n"
74          + "\t(default: " + defaultClassifierString() +")",
75          "W", 1, "-W"));
76
77    newVector.addElement(new Option(
78          "",
79          "", 0, "\nOptions specific to classifier "
80          + m_Classifier.getClass().getName() + ":"));
81    enu = ((OptionHandler)m_Classifier).listOptions();
82    while (enu.hasMoreElements()) {
83      newVector.addElement(enu.nextElement());
84    }
85
86    return newVector.elements();
87  }
88
89  /**
90   * Parses a given list of options. Valid options are:<p>
91   *
92   * -W classname <br>
93   * Specify the full class name of the base learner.<p>
94   *
95   * Options after -- are passed to the designated classifier.<p>
96   *
97   * @param options the list of options as an array of strings
98   * @exception Exception if an option is not supported
99   */
100  public void setOptions(String[] options) throws Exception {
101
102    super.setOptions(options);
103
104    String classifierName = Utils.getOption('W', options);
105
106    if (classifierName.length() > 0) {
107
108      // This is just to set the classifier in case the option
109      // parsing fails.
110      setClassifier(AbstractClassifier.forName(classifierName, null));
111      setClassifier(AbstractClassifier.forName(classifierName,
112            Utils.partitionOptions(options)));
113    } else {
114
115      // This is just to set the classifier in case the option
116      // parsing fails.
117      setClassifier(AbstractClassifier.forName(defaultClassifierString(), null));
118      setClassifier(AbstractClassifier.forName(defaultClassifierString(),
119            Utils.partitionOptions(options)));
120    }
121  }
122
123  /**
124   * Gets the current settings of the Classifier.
125   *
126   * @return an array of strings suitable for passing to setOptions
127   */
128  public String [] getOptions() {
129
130    String [] classifierOptions = ((OptionHandler)m_Classifier).getOptions();
131    int extraOptionsLength = classifierOptions.length;
132    if (extraOptionsLength > 0) {
133      extraOptionsLength++; // for the double hyphen
134    }
135
136    String [] superOptions = super.getOptions();
137    String [] options = new String [superOptions.length +
138      extraOptionsLength + 2];
139
140    int current = 0;
141    options[current++] = "-W";
142    options[current++] = getClassifier().getClass().getName();
143
144    System.arraycopy(superOptions, 0, options, current,
145        superOptions.length);
146    current += superOptions.length;
147
148    if (classifierOptions.length > 0) {
149      options[current++] = "--";
150      System.arraycopy(classifierOptions, 0, options, current,
151          classifierOptions.length);
152    }
153
154    return options;
155  }
156
157  /**
158   * Returns the tip text for this property
159   * @return tip text for this property suitable for
160   * displaying in the explorer/experimenter gui
161   */
162  public String classifierTipText() {
163    return "The base classifier to be used.";
164  }
165
166  /**
167   * Returns default capabilities of the base classifier.
168   *
169   * @return      the capabilities of the base classifier
170   */
171  public Capabilities getCapabilities() {
172    Capabilities        result;
173
174    if (getClassifier() != null) {
175      result = getClassifier().getCapabilities();
176    } else {
177      result = new Capabilities(this);
178      result.disableAll();
179    }
180
181    // set dependencies
182    for (Capability cap: Capability.values())
183      result.enableDependency(cap);
184
185    result.setOwner(this);
186
187    return result;
188  }
189
190  /**
191   * Set the base learner.
192   *
193   * @param newClassifier the classifier to use.
194   */
195  public void setClassifier(Classifier newClassifier) {
196
197    m_Classifier = newClassifier;
198  }
199
200  /**
201   * Get the classifier used as the base learner.
202   *
203   * @return the classifier used as the classifier
204   */
205  public Classifier getClassifier() {
206
207    return m_Classifier;
208  }
209
210  /**
211   * Gets the classifier specification string, which contains the class name of
212   * the classifier and any options to the classifier
213   *
214   * @return the classifier string
215   */
216  protected String getClassifierSpec() {
217
218    Classifier c = getClassifier();
219    return c.getClass().getName() + " "
220      + Utils.joinOptions(((OptionHandler)c).getOptions());
221  }
222}
Note: See TracBrowser for help on using the repository browser.