📄 attribute.java
字号:
/* * 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 2 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, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* * Attribute.java * Copyright (C) 1999 University of Waikato, Hamilton, New Zealand * */package weka.core;import java.io.Serializable;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Enumeration;import java.util.Hashtable;import java.util.Properties;import java.io.StreamTokenizer;import java.io.StringReader;import java.io.IOException;/** * Class for handling an attribute. Once an attribute has been created, * it can't be changed. <p> * * The following attribute types are supported: * <ul> * <li> numeric: <br/> * This type of attribute represents a floating-point number. * </li> * <li> nominal: <br/> * This type of attribute represents a fixed set of nominal values. * </li> * <li> string: <br/> * This type of attribute represents a dynamically expanding set of * nominal values. Usually used in text classification. * </li> * <li> date: <br/> * This type of attribute represents a date, internally represented as * floating-point number storing the milliseconds since January 1, * 1970, 00:00:00 GMT. The string representation of the date must be * <a href="http://www.iso.org/iso/en/prods-services/popstds/datesandtime.html" target="_blank"> * ISO-8601</a> compliant, the default is <code>yyyy-MM-dd'T'HH:mm:ss</code>. * </li> * <li> relational: <br/> * This type of attribute can contain other attributes and is, e.g., * used for representing Multi-Instance data. (Multi-Instance data * consists of a nominal attribute containing the bag-id, then a * relational attribute with all the attributes of the bag, and * finally the class attribute.) * </li> * </ul> * * Typical usage (code from the main() method of this class): <p> * * <code> * ... <br> * * // Create numeric attributes "length" and "weight" <br> * Attribute length = new Attribute("length"); <br> * Attribute weight = new Attribute("weight"); <br><br> * * // Create vector to hold nominal values "first", "second", "third" <br> * FastVector my_nominal_values = new FastVector(3); <br> * my_nominal_values.addElement("first"); <br> * my_nominal_values.addElement("second"); <br> * my_nominal_values.addElement("third"); <br><br> * * // Create nominal attribute "position" <br> * Attribute position = new Attribute("position", my_nominal_values);<br> * * ... <br> * </code><p> * * @author Eibe Frank (eibe@cs.waikato.ac.nz) * @version $Revision: 1.44 $ */public class Attribute implements Copyable, Serializable { /** for serialization */ static final long serialVersionUID = -742180568732916383L; /** Constant set for numeric attributes. */ public static final int NUMERIC = 0; /** Constant set for nominal attributes. */ public static final int NOMINAL = 1; /** Constant set for attributes with string values. */ public static final int STRING = 2; /** Constant set for attributes with date values. */ public static final int DATE = 3; /** Constant set for relation-valued attributes. */ public static final int RELATIONAL = 4; /** Constant set for symbolic attributes. */ public static final int ORDERING_SYMBOLIC = 0; /** Constant set for ordered attributes. */ public static final int ORDERING_ORDERED = 1; /** Constant set for modulo-ordered attributes. */ public static final int ORDERING_MODULO = 2; /** The keyword used to denote the start of an arff attribute declaration */ public final static String ARFF_ATTRIBUTE = "@attribute"; /** A keyword used to denote a numeric attribute */ public final static String ARFF_ATTRIBUTE_INTEGER = "integer"; /** A keyword used to denote a numeric attribute */ public final static String ARFF_ATTRIBUTE_REAL = "real"; /** A keyword used to denote a numeric attribute */ public final static String ARFF_ATTRIBUTE_NUMERIC = "numeric"; /** The keyword used to denote a string attribute */ public final static String ARFF_ATTRIBUTE_STRING = "string"; /** The keyword used to denote a date attribute */ public final static String ARFF_ATTRIBUTE_DATE = "date"; /** The keyword used to denote a relation-valued attribute */ public final static String ARFF_ATTRIBUTE_RELATIONAL = "relational"; /** The keyword used to denote the end of the declaration of a subrelation */ public final static String ARFF_END_SUBRELATION = "@end"; /** Strings longer than this will be stored compressed. */ private static final int STRING_COMPRESS_THRESHOLD = 200; /** The attribute's name. */ private /*@ spec_public non_null @*/ String m_Name; /** The attribute's type. */ private /*@ spec_public @*/ int m_Type; /*@ invariant m_Type == NUMERIC || m_Type == DATE || m_Type == STRING || m_Type == NOMINAL || m_Type == RELATIONAL; */ /** The attribute's values (if nominal or string). */ private /*@ spec_public @*/ FastVector m_Values; /** Mapping of values to indices (if nominal or string). */ private Hashtable m_Hashtable; /** The header information for a relation-valued attribute. */ private Instances m_Header; /** Date format specification for date attributes */ private SimpleDateFormat m_DateFormat; /** The attribute's index. */ private /*@ spec_public @*/ int m_Index; /** The attribute's metadata. */ private ProtectedProperties m_Metadata; /** The attribute's ordering. */ private int m_Ordering; /** Whether the attribute is regular. */ private boolean m_IsRegular; /** Whether the attribute is averagable. */ private boolean m_IsAveragable; /** Whether the attribute has a zeropoint. */ private boolean m_HasZeropoint; /** The attribute's weight. */ private double m_Weight; /** The attribute's lower numeric bound. */ private double m_LowerBound; /** Whether the lower bound is open. */ private boolean m_LowerBoundIsOpen; /** The attribute's upper numeric bound. */ private double m_UpperBound; /** Whether the upper bound is open */ private boolean m_UpperBoundIsOpen; /** * Constructor for a numeric attribute. * * @param attributeName the name for the attribute */ //@ requires attributeName != null; //@ ensures m_Name == attributeName; public Attribute(String attributeName) { this(attributeName, new ProtectedProperties(new Properties())); } /** * Constructor for a numeric attribute, where metadata is supplied. * * @param attributeName the name for the attribute * @param metadata the attribute's properties */ //@ requires attributeName != null; //@ requires metadata != null; //@ ensures m_Name == attributeName; public Attribute(String attributeName, ProtectedProperties metadata) { m_Name = attributeName; m_Index = -1; m_Values = null; m_Hashtable = null; m_Header = null; m_Type = NUMERIC; setMetadata(metadata); } /** * Constructor for a date attribute. * * @param attributeName the name for the attribute * @param dateFormat a string suitable for use with * SimpleDateFormatter for parsing dates. */ //@ requires attributeName != null; //@ requires dateFormat != null; //@ ensures m_Name == attributeName; public Attribute(String attributeName, String dateFormat) { this(attributeName, dateFormat, new ProtectedProperties(new Properties())); } /** * Constructor for a date attribute, where metadata is supplied. * * @param attributeName the name for the attribute * @param dateFormat a string suitable for use with * SimpleDateFormatter for parsing dates. * @param metadata the attribute's properties */ //@ requires attributeName != null; //@ requires dateFormat != null; //@ requires metadata != null; //@ ensures m_Name == attributeName; public Attribute(String attributeName, String dateFormat, ProtectedProperties metadata) { m_Name = attributeName; m_Index = -1; m_Values = null; m_Hashtable = null; m_Header = null; m_Type = DATE; if (dateFormat != null) { m_DateFormat = new SimpleDateFormat(dateFormat); } else { m_DateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); } m_DateFormat.setLenient(false); setMetadata(metadata); } /** * Constructor for nominal attributes and string attributes. * If a null vector of attribute values is passed to the method, * the attribute is assumed to be a string. * * @param attributeName the name for the attribute * @param attributeValues a vector of strings denoting the * attribute values. Null if the attribute is a string attribute. */ //@ requires attributeName != null; //@ ensures m_Name == attributeName; public Attribute(String attributeName, FastVector attributeValues) { this(attributeName, attributeValues, new ProtectedProperties(new Properties())); } /** * Constructor for nominal attributes and string attributes, where * metadata is supplied. If a null vector of attribute values is passed * to the method, the attribute is assumed to be a string. * * @param attributeName the name for the attribute * @param attributeValues a vector of strings denoting the * attribute values. Null if the attribute is a string attribute. * @param metadata the attribute's properties */ //@ requires attributeName != null; //@ requires metadata != null; /*@ ensures m_Name == attributeName; ensures m_Index == -1; ensures attributeValues == null && m_Type == STRING || attributeValues != null && m_Type == NOMINAL && m_Values.size() == attributeValues.size(); signals (IllegalArgumentException ex) (* if duplicate strings in attributeValues *); */ public Attribute(String attributeName, FastVector attributeValues, ProtectedProperties metadata) { m_Name = attributeName; m_Index = -1; if (attributeValues == null) { m_Values = new FastVector(); m_Hashtable = new Hashtable(); m_Header = null; m_Type = STRING; } else { m_Values = new FastVector(attributeValues.size()); m_Hashtable = new Hashtable(attributeValues.size()); m_Header = null; for (int i = 0; i < attributeValues.size(); i++) { Object store = attributeValues.elementAt(i); if (((String)store).length() > STRING_COMPRESS_THRESHOLD) { try { store = new SerializedObject(attributeValues.elementAt(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.addElement(store); m_Hashtable.put(store, new Integer(i)); } m_Type = NOMINAL; } setMetadata(metadata); } /** * Constructor for relation-valued attributes. * * @param attributeName the name for the attribute * @param header an Instances object specifying the header of the relation. */ public Attribute(String attributeName, Instances header) { this(attributeName, header, new ProtectedProperties(new Properties())); } /** * Constructor for relation-valued attributes. * * @param attributeName the name for the attribute * @param header an Instances object specifying the header of the relation. * @param metadata the attribute's properties */ public Attribute(String attributeName, Instances header, ProtectedProperties metadata) { if (header.numInstances() > 0) { throw new IllegalArgumentException("Header for relation-valued " + "attribute should not contain " + "any instances"); } m_Name = attributeName; m_Index = -1; m_Values = new FastVector(); m_Hashtable = new Hashtable(); m_Header = header; m_Type = RELATIONAL; setMetadata(metadata); } /** * Produces a shallow copy of this attribute. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -