public static void replaceValue(ExampleSet exampleSet,Attribute attribute,String oldValue,String newValue){
  if (!attribute.isNominal()) {
    throw new RuntimeException("Example-Tools: replaceValue is only supported for nominal attributes.");
  }
  NominalMapping mapping=attribute.getMapping();
  if (oldValue.equals("?")) {
    for (    Example example : exampleSet) {
      if (Double.isNaN(example.getValue(attribute))) {
        example.setValue(attribute,mapping.mapString(newValue));
      }
    }
  }
 else {
    int oldIndex=mapping.getIndex(oldValue);
    if (oldIndex < 0) {
      throw new RuntimeException("Example-Tools: replaceValue cannot be performed since old value was not defined in the attribute.");
    }
    if (newValue.equals("?")) {
      for (      Example example : exampleSet) {
        int index=mapping.getIndex(example.getValueAsString(attribute));
        if (index == oldIndex) {
          example.setValue(attribute,Double.NaN);
        }
      }
      return;
    }
    int newIndex=mapping.getIndex(newValue);
    if (newIndex >= 0) {
      for (      Example example : exampleSet) {
        int index=mapping.getIndex(example.getValueAsString(attribute));
        if (index == oldIndex) {
          example.setValue(attribute,newIndex);
        }
      }
    }
 else {
      mapping.setMapping(newValue,oldIndex);
    }
  }
}

public static void replaceValue ( ExampleSet exampleSet , Attribute attribute , String oldValue , String newValue ) { if ( ! attribute . isNominal ( ) ) { throw new RuntimeException ( "Example-Tools: replaceValue is only supported for nominal attributes." ) ; } NominalMapping mapping = attribute . getMapping ( ) ; if ( oldValue . equals ( "?" ) ) { for ( Example example : exampleSet ) { if ( Double . isNaN ( example . getValue ( attribute ) ) ) { example . setValue ( attribute , mapping . mapString ( newValue ) ) ; } } } else { int oldIndex = mapping . getIndex ( oldValue ) ; if ( oldIndex < 0 ) { throw new RuntimeException ( "Example-Tools: replaceValue cannot be performed since old value was not defined in the attribute." ) ; } if ( newValue . equals ( "?" ) ) { for ( Example example : exampleSet ) { int index = mapping . getIndex ( example . getValueAsString ( attribute ) ) ; if ( index == oldIndex ) { example . setValue ( attribute , Double . NaN ) ; } } return ; } int newIndex = mapping . getIndex ( newValue ) ; if ( newIndex >= 0 ) { for ( Example example : exampleSet ) { int index = mapping . getIndex ( example . getValueAsString ( attribute ) ) ; if ( index == oldIndex ) { example . setValue ( attribute , newIndex ) ; } } } else { mapping . setMapping ( newValue , oldIndex ) ; } } }
replaces the given value by the new one . this method will only work for nominal attributes .
