📄 attribute.java
字号:
generatingFunctionArguments[1].getConstructionDescription()+")"; } else { String cd = generatingFunctionName + "("; for (int i = 0; i < generatingFunctionArguments.length; i++) cd += (i==0 ? "":", ") + generatingFunctionArguments[i].getConstructionDescription(); return cd + ")"; } } /** Returns true if the attribute was constructed the same way o was constructed. */ public boolean equals(Object o) { if (!(o instanceof Attribute)) return false; Attribute a = (Attribute)o; if (!generatingFunctionName.equals(a.generatingFunctionName)) return false; if ((generatingFunctionArguments == null) && (a.generatingFunctionArguments == null)) return true; if ((generatingFunctionArguments != null) && (a.generatingFunctionArguments != null)) { if (generatingFunctionArguments.length != a.generatingFunctionArguments.length) return false; for (int i = 0; i < generatingFunctionArguments.length; i++) if (!generatingFunctionArguments[i].equals(a.generatingFunctionArguments[i])) return false; return true; } else { return false; } } /** Get the value of blockType. * @return value of blockType. */ public int getBlockType() { return blockType; } /** Set the value of blockType. * @param b Value to assign to valueType. */ public void setBlockType(int b) { this.blockType = b; } /** Get the value of valueType. * @return value of valueType. */ public int getValueType() { return valueType; } /** Set the value of valueType. * @param v Value to assign to valueType. */ public void setValueType(int v) { this.valueType = v; if (Ontology.ATTRIBUTE_VALUE_TYPE.isA(valueType, Ontology.NOMINAL)) { if (symbolToIndexMap == null) { this.symbolToIndexMap = new HashMap(); this.indexToSymbolMap = new HashMap(); } if (Ontology.ATTRIBUTE_VALUE_TYPE.isA(valueType, Ontology.CLASSIFICATION)) { mapString(POSITIVE_CLASS); mapString(NEGATIVE_CLASS); } } } /** Get the value of blockNr. * @return value of blockNr. */ public int getBlockNr() { return blockNr; } /** Returns true if value and block types of this attribute are subtypes of value and block type of a. */ public boolean compatible(Attribute a) { return (Ontology.ATTRIBUTE_VALUE_TYPE.isA(this.valueType, a.valueType)) && (Ontology.ATTRIBUTE_BLOCK_TYPE.isA(this.blockType, a.blockType)); } /** Returns true if the units are equal. */ public boolean compatibleUnit(Attribute a) { for (int i = 0; i < unit.length; i++) if (unit[i]!= a.unit[i]) return false; return true; } /** Returns true if block type is a value series. */ public boolean isSeries() { return Ontology.ATTRIBUTE_BLOCK_TYPE.isA(blockType, Ontology.VALUE_SERIES); } /** Returns true if block type is a value series. */ public boolean isInterval() { return Ontology.ATTRIBUTE_VALUE_TYPE.isA(blockType, Ontology.INTERVAL); } /** Returns true if this attribute is nominal. */ public boolean isNominal() { return Ontology.ATTRIBUTE_VALUE_TYPE.isA(getValueType(), Ontology.NOMINAL); } /** Returns true if this attribute is the start attribute of its block. */ public boolean isBlockStart() { return (Ontology.ATTRIBUTE_BLOCK_TYPE.isA(blockType, Ontology.INTERVAL_START) || Ontology.ATTRIBUTE_BLOCK_TYPE.isA(blockType, Ontology.VALUE_SERIES_START)); } /** Returns the index for the nominal attribute value <code>str</code>. If * the string is unknown, a new index value is assigned. Returns -1, * if str is null. */ public int mapString(String str) { ensureNominal(); if (str == null) return -1; // lookup string in hashtable Integer index = (Integer)symbolToIndexMap.get(str); // if string is not yet in the map, add it if (index == null) { // new string -> insert index = new Integer(nextFreeSymbolMapIndex++); symbolToIndexMap.put(str, index); indexToSymbolMap.put(index, str); } return index.intValue(); } /** Returns the attribute value, that is associated with this index. */ public String mapIndex(int nr) { ensureNominal(); Object index = indexToSymbolMap.get(new Integer(nr)); if (index != null) { return ((String)index); } else return null; } /** Returns the number of different values. */ public int getNumberOfClasses() { ensureNominal(); return symbolToIndexMap.size(); } /** Returns a string representation of value if type is numerical or maps * the value to a string if type is nominal. */ public String getAsString(double value) { if (Double.isNaN(value)) return "?"; if (Ontology.ATTRIBUTE_VALUE_TYPE.isA(getValueType(), Ontology.NUMERICAL)) { return value + ""; } else { return mapIndex((int)value); } } /** Returns the values of the attribute as an enumeration of strings. */ public Collection getValuesAsString() { ensureNominal(); return indexToSymbolMap.values(); } /** Creates a new unsused attribute name with a given prefix. */ public static String createName(String prefix) { return prefix + genSym++; } /** Creates a new unsused attribute name. */ private static String createName() { return createName(GENSYM_PREFIX); } /** Returns the index in the example table. */ public int getIndex() { return index; } /** Sets the index in the example table. */ public void setIndex(int i) { this.index = i; } /** Returns the name of the function that generated this attribute. */ public String getFunctionName() { return generatingFunctionName; } /** Returns the arguments that were used to generate this attribute. */ public Attribute[] getArguments() { return generatingFunctionArguments; } /** Returns a human readable string that describes this attribute. */ public String toString() { String str = "#" + index + ": " + name + ":=" + getConstructionDescription() + "["+unitToString()+"]: " + Ontology.ATTRIBUTE_VALUE_TYPE.mapIndex(valueType) + "/" + Ontology.ATTRIBUTE_BLOCK_TYPE.mapIndex(blockType) + "/" + (blockNr == -1 ? "no block" : "block=" + blockNr); if (isNominal()) { str += " ["; for (int i = 0; i < getNumberOfClasses(); i++) { if (i>0) str += ","; str += mapIndex(i+FIRST_CLASS_INDEX); } str += "]"; } else { str += minimum+"-"+maximum+"; avg="+average; } return str; } /** Returns a html representation (table row) of this attribute. */ public String toHTML() { String str = "<tr><td>"+index+"</td>"+ "<td>" + name + "</td>" + "<td>" + getConstructionDescription() + "</td>" + "<td>"+unitToString()+"</td>" + "<td>"+Ontology.ATTRIBUTE_VALUE_TYPE.mapIndex(valueType) + "</td>" + "<td>"+Ontology.ATTRIBUTE_BLOCK_TYPE.mapIndex(blockType) + "</td>" + "<td>"+((Ontology.ATTRIBUTE_BLOCK_TYPE.isA(blockType, Ontology.VALUE_SERIES) || Ontology.ATTRIBUTE_BLOCK_TYPE.isA(blockType, Ontology.INTERVAL)) ? "" + blockNr : "-") + "</td>" + "<td>"; if (isNominal()) { for (int i = 0; i < getNumberOfClasses(); i++) { if (i>0) str += ","; str += mapIndex(i+FIRST_CLASS_INDEX); } } else { str += minimum+"-"+maximum+"; avg="+average; } str += "</td></tr>"; return str; } public String unitToString() { String s = ""; for (int i = 0; i < unit.length; i++) if (unit[i] != 0) { s += UNIT_NAME[i]; if (unit[i] != 1) s += unit[i]; } return s; } public static int[] multiplyUnits(int[] unit1, int[] unit2) { int unit[] = new int[unit1.length]; for (int i = 0; i < unit1.length; i++) unit[i] = unit1[i]+unit2[i]; return unit; } public static int[] divideUnits(int[] unit1, int[] unit2) { int unit[] = new int[unit1.length]; for (int i = 0; i < unit1.length; i++) unit[i] = unit1[i]-unit2[i]; return unit; } /** Reads the units from a string as generated by unitToString. */ public void parseUnits(String unitString) { if (unitString == null) { for (int i = 0;i < unit.length; i++) unit[i] = 0; return; } for (int i = 0; i < UNIT_NAME.length; i++) { int pos = unitString.indexOf(UNIT_NAME[i]); if (pos != -1) { pos += UNIT_NAME[i].length(); int endPos = pos; while ((endPos < unitString.length()) && (Character.isDigit(unitString.charAt(endPos)) || (unitString.charAt(endPos) == '-'))) { endPos++; } if (endPos == pos) unit[i] = 1; else unit[i] = Integer.parseInt(unitString.substring(pos,endPos)); } else { unit[i] = 0; } } } /** Throws a runtime exception if this attribute is not nominal. */ private void ensureNominal() { if (!Ontology.ATTRIBUTE_VALUE_TYPE.isA(getValueType(), Ontology.NOMINAL)) throw new RuntimeException("Attribute "+this.toString()+" is not a nominal attribute!"); } /** Returns true iff value is the default value for this attribute. The default for * numerical attributes is 0.0, the default for nominal attributes is * {@link Attribute#FIRST_CLASS_INDEX}. */ public boolean isDefault(double value) { if (isNominal()) { return value == FIRST_CLASS_INDEX; } else { return value == 0.0; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -