~ubuntu-branches/ubuntu/precise/weka/precise

« back to all changes in this revision

Viewing changes to weka/clusterers/SingleClustererEnhancer.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 * SingleClustererEnhancer.java
 
19
 * Copyright (C) 2006 University of Waikato, Hamilton, New Zealand
 
20
 *
 
21
 */
 
22
 
 
23
package weka.clusterers;
 
24
 
 
25
import weka.core.Capabilities;
 
26
import weka.core.Option;
 
27
import weka.core.OptionHandler;
 
28
import weka.core.Utils;
 
29
import weka.core.Capabilities.Capability;
 
30
 
 
31
import java.util.Enumeration;
 
32
import java.util.Vector;
 
33
 
 
34
/**
 
35
 * Meta-clusterer for enhancing a base clusterer.
 
36
 *
 
37
 * @author FracPete (fracpete at waikato dot ac dot nz)
 
38
 * @version $Revision: 1.3 $
 
39
 */
 
40
public abstract class SingleClustererEnhancer
 
41
  extends Clusterer
 
42
  implements OptionHandler {
 
43
 
 
44
  /** for serialization */
 
45
  private static final long serialVersionUID = 4893928362926428671L;
 
46
 
 
47
  /** the clusterer */
 
48
  protected Clusterer m_Clusterer = new SimpleKMeans();
 
49
 
 
50
  /**
 
51
   * String describing default clusterer.
 
52
   * 
 
53
   * @return            the default clusterer classname
 
54
   */
 
55
  protected String defaultClustererString() {
 
56
    return SimpleKMeans.class.getName();
 
57
  }
 
58
 
 
59
  /**
 
60
   * Returns an enumeration describing the available options.
 
61
   *
 
62
   * @return            an enumeration of all the available options.
 
63
   */
 
64
  public Enumeration listOptions() {
 
65
    Vector result = new Vector();
 
66
 
 
67
    result.addElement(new Option(
 
68
        "\tFull name of base clusterer.\n"
 
69
        + "\t(default: " + defaultClustererString() +")",
 
70
        "W", 1, "-W"));
 
71
 
 
72
    if (m_Clusterer instanceof OptionHandler) {
 
73
      result.addElement(new Option(
 
74
          "",
 
75
          "", 0, "\nOptions specific to clusterer "
 
76
          + m_Clusterer.getClass().getName() + ":"));
 
77
      Enumeration enu = ((OptionHandler) m_Clusterer).listOptions();
 
78
      while (enu.hasMoreElements()) {
 
79
        result.addElement(enu.nextElement());
 
80
      }
 
81
    }
 
82
 
 
83
    return result.elements();
 
84
  }
 
85
 
 
86
  /**
 
87
   * Parses a given list of options.
 
88
   *
 
89
   * @param options     the list of options as an array of strings
 
90
   * @throws Exception  if an option is not supported
 
91
   */
 
92
  public void setOptions(String[] options) throws Exception {
 
93
    String      tmpStr;
 
94
    
 
95
    tmpStr = Utils.getOption('W', options);
 
96
    if (tmpStr.length() > 0) { 
 
97
      // This is just to set the classifier in case the option 
 
98
      // parsing fails.
 
99
      setClusterer(Clusterer.forName(tmpStr, null));
 
100
      setClusterer(Clusterer.forName(tmpStr, Utils.partitionOptions(options)));
 
101
    } 
 
102
    else {
 
103
      // This is just to set the classifier in case the option 
 
104
      // parsing fails.
 
105
      setClusterer(Clusterer.forName(defaultClustererString(), null));
 
106
      setClusterer(Clusterer.forName(defaultClustererString(), Utils.partitionOptions(options)));
 
107
    }
 
108
  }
 
109
 
 
110
  /**
 
111
   * Gets the current settings of the clusterer.
 
112
   *
 
113
   * @return            an array of strings suitable for passing to setOptions
 
114
   */
 
115
  public String[] getOptions() {
 
116
    Vector      result;
 
117
    String[]    options;
 
118
    int         i;
 
119
    
 
120
    result = new Vector();
 
121
 
 
122
    result.add("-W");
 
123
    result.add(getClusterer().getClass().getName());
 
124
    
 
125
    if (getClusterer() instanceof OptionHandler) {
 
126
      result.add("--");
 
127
      options = ((OptionHandler) getClusterer()).getOptions();
 
128
      for (i = 0; i < options.length; i++)
 
129
        result.add(options[i]);
 
130
    }
 
131
    
 
132
    return (String[]) result.toArray(new String[result.size()]);
 
133
  }
 
134
  
 
135
  /**
 
136
   * Returns the tip text for this property
 
137
   * 
 
138
   * @return            tip text for this property suitable for
 
139
   *                    displaying in the explorer/experimenter gui
 
140
   */
 
141
  public String clustererTipText() {
 
142
    return "The base clusterer to be used.";
 
143
  }
 
144
 
 
145
  /**
 
146
   * Set the base clusterer.
 
147
   *
 
148
   * @param value       the classifier to use.
 
149
   */
 
150
  public void setClusterer(Clusterer value) {
 
151
    m_Clusterer = value;
 
152
  }
 
153
 
 
154
  /**
 
155
   * Get the clusterer used as the base clusterer.
 
156
   *
 
157
   * @return            the base clusterer
 
158
   */
 
159
  public Clusterer getClusterer() {
 
160
    return m_Clusterer;
 
161
  }
 
162
  
 
163
  /**
 
164
   * Gets the clusterer specification string, which contains the class name of
 
165
   * the clusterer and any options to the clusterer
 
166
   *
 
167
   * @return            the clusterer string
 
168
   */
 
169
  protected String getClustererSpec() {
 
170
    String      result;
 
171
    Clusterer   clusterer;
 
172
    
 
173
    clusterer = getClusterer();
 
174
    result    = clusterer.getClass().getName();
 
175
    
 
176
    if (clusterer instanceof OptionHandler)
 
177
      result += " " + Utils.joinOptions(((OptionHandler) clusterer).getOptions());
 
178
    
 
179
    return result;
 
180
  }
 
181
 
 
182
  /**
 
183
   * Returns default capabilities of the clusterer.
 
184
   *
 
185
   * @return            the capabilities of this clusterer
 
186
   */
 
187
  public Capabilities getCapabilities() {
 
188
    Capabilities        result;
 
189
    
 
190
    if (getClusterer() == null)
 
191
      result = super.getCapabilities();
 
192
    else
 
193
      result = getClusterer().getCapabilities();
 
194
    
 
195
    // set dependencies
 
196
    for (Capability cap: Capability.values())
 
197
      result.enableDependency(cap);
 
198
    
 
199
    return result;
 
200
  }
 
201
 
 
202
  /**
 
203
   * Returns the number of clusters.
 
204
   *
 
205
   * @return            the number of clusters generated for a training dataset.
 
206
   * @throws Exception  if number of clusters could not be returned
 
207
   *                    successfully
 
208
   */
 
209
  public int numberOfClusters() throws Exception {
 
210
    return m_Clusterer.numberOfClusters();
 
211
  }
 
212
}