| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| NominalItem |
|
| 2.5;2.5 |
| 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 3 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, see <http://www.gnu.org/licenses/>. | |
| 14 | */ | |
| 15 | ||
| 16 | /* | |
| 17 | * NominalItem.java | |
| 18 | * Copyright (C) 2010-2012 University of Waikato, Hamilton, New Zealand | |
| 19 | * | |
| 20 | */ | |
| 21 | ||
| 22 | package weka.associations; | |
| 23 | ||
| 24 | import java.io.Serializable; | |
| 25 | ||
| 26 | import weka.core.Attribute; | |
| 27 | ||
| 28 | ||
| 29 | /** | |
| 30 | * Class that encapsulates a nominal item. | |
| 31 | * | |
| 32 | * @author Mark Hall (mhall{[at]}pentaho{[dot]}com) | |
| 33 | * @version $Revision: 8034 $ | |
| 34 | */ | |
| 35 | public class NominalItem extends Item implements Serializable { | |
| 36 | ||
| 37 | /** For serialization */ | |
| 38 | private static final long serialVersionUID = 2182122099990462066L; | |
| 39 | ||
| 40 | /** The index of the value considered to be positive */ | |
| 41 | protected int m_valueIndex; | |
| 42 | ||
| 43 | /** | |
| 44 | * Constructs a new NominalItem. | |
| 45 | * | |
| 46 | * @param att the attribute that backs the item. | |
| 47 | * @param valueIndex the index of the value for this item. | |
| 48 | * @throws Exception if the NominalItem can't be constructed. | |
| 49 | */ | |
| 50 | public NominalItem(Attribute att, int valueIndex) throws Exception { | |
| 51 | ||
| 52 | 0 | super(att); |
| 53 | ||
| 54 | 0 | if (att.isNumeric()) { |
| 55 | 0 | throw new Exception("NominalItem must be constructed using a nominal attribute"); |
| 56 | } | |
| 57 | 0 | m_attribute = att; |
| 58 | 0 | if (m_attribute.numValues() == 1) { |
| 59 | 0 | m_valueIndex = 0; // unary attribute (? used to indicate absence from a basket) |
| 60 | } else { | |
| 61 | 0 | m_valueIndex = valueIndex; |
| 62 | } | |
| 63 | 0 | } |
| 64 | ||
| 65 | /** | |
| 66 | * Get the value index for this item. | |
| 67 | * | |
| 68 | * @return the value index. | |
| 69 | */ | |
| 70 | public int getValueIndex() { | |
| 71 | 0 | return m_valueIndex; |
| 72 | } | |
| 73 | ||
| 74 | /** | |
| 75 | * Get this item's value as a String. | |
| 76 | * | |
| 77 | * @return this item's value as a String. | |
| 78 | */ | |
| 79 | public String getItemValueAsString() { | |
| 80 | 0 | return m_attribute.value(m_valueIndex); |
| 81 | } | |
| 82 | ||
| 83 | /** | |
| 84 | * Get this item's comparison operator as a String. | |
| 85 | * | |
| 86 | * @return this item's comparison operator as a String. | |
| 87 | */ | |
| 88 | public String getComparisonAsString() { | |
| 89 | 0 | return "="; |
| 90 | } | |
| 91 | ||
| 92 | /** | |
| 93 | * A string representation of this item, (i.e. | |
| 94 | * <attribute name> <comparison operator> <item value>). | |
| 95 | * This default implementation just prints the attribute | |
| 96 | * name and (optionally) frequency information. | |
| 97 | * | |
| 98 | * @param freq true if the frequency should be included. | |
| 99 | * @return a string representation of this item. | |
| 100 | */ | |
| 101 | public String toString(boolean freq) { | |
| 102 | 0 | String result = m_attribute.name() + "=" + m_attribute.value(m_valueIndex); |
| 103 | 0 | if (freq) { |
| 104 | 0 | result += ":" + m_frequency; |
| 105 | } | |
| 106 | 0 | return result; |
| 107 | } | |
| 108 | ||
| 109 | /** | |
| 110 | * Equals. Just compares attribute and valueIndex. | |
| 111 | * @return true if this NominalItem is equal to the argument. | |
| 112 | */ | |
| 113 | public boolean equals(Object compareTo) { | |
| 114 | 0 | if (!(compareTo instanceof NominalItem)) { |
| 115 | 0 | return false; |
| 116 | } | |
| 117 | ||
| 118 | 0 | NominalItem b = (NominalItem)compareTo; |
| 119 | 0 | if (m_attribute.equals(b.getAttribute()) && |
| 120 | // m_frequency == b.getFrequency() && | |
| 121 | m_valueIndex == b.getValueIndex()) { | |
| 122 | 0 | return true; |
| 123 | } | |
| 124 | ||
| 125 | 0 | return false; |
| 126 | } | |
| 127 | } |