All Downloads are FREE. Search and download functionalities are using the official Maven repository.

weka.core.NominalAttributeInfo Maven / Gradle / Ivy

Go to download

The Waikato Environment for Knowledge Analysis (WEKA), a machine learning workbench. This version represents the developer version, the "bleeding edge" of development, you could say. New functionality gets added to this version.

There is a newer version: 3.9.6
Show newest version
/*
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see .
 */

/*
 *    NominalAttributeInfo.java
 *    Copyright (C) 2014 University of Waikato, Hamilton, New Zealand
 *
 */
package weka.core;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

/**
 * Stores information for nominal and string attributes.
 */
public class NominalAttributeInfo implements AttributeInfo {

  /** The attribute's values. */
  protected/* @ spec_public @ */ArrayList m_Values;

  /** Mapping of values to indices. */
  protected Hashtable m_Hashtable;

  /**
   * Constructs the info based on argument.
   */
  public NominalAttributeInfo(List attributeValues, String attributeName) {

    if (attributeValues == null) {
      m_Values = new ArrayList();
      m_Hashtable = new Hashtable();
    } else {
      m_Values = new ArrayList(attributeValues.size());
      m_Hashtable = new Hashtable(attributeValues.size());
      for (int i = 0; i < attributeValues.size(); i++) {
        Object store = attributeValues.get(i);
        if (((String) store).length() > Attribute.STRING_COMPRESS_THRESHOLD) {
          try {
            store = new SerializedObject(attributeValues.get(i), true);
          } catch (Exception ex) {
            System.err.println("Couldn't compress nominal attribute value -"
              + " storing uncompressed.");
          }
        }
        if (m_Hashtable.containsKey(store)) {
          throw new IllegalArgumentException("A nominal attribute ("
            + attributeName + ") cannot" + " have duplicate labels (" + store
            + ").");
        }
        m_Values.add(store);
        m_Hashtable.put(store, new Integer(i));
      }
    }
  }
}