Package trust.weka4jason.utils

Source Code of trust.weka4jason.utils.Datasetter

package trust.weka4jason.utils;

import java.util.ArrayList;

import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instance;
import weka.core.Instances;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.StringToNominal;

public class Datasetter {
 
  /**
   *  Translates Instances Dataset
   *   usage: <i> Instances newData = Dataset.fromStringToNominal(BeliefBase, 3 , 4 , 5 );</i>
   *  
   *   takes the Beliefbase dataset and change the lines 3,4,5 from String to Nominal.
   *  
   *    
   * @param data the initial dataset containing STRING attributes
   * @param objects  a list of parameters specifying the attribute
   *        indexes to translate from STRING to NOMINAL
   * @return  new dataset instances, where the i-th attributes have been translated from STRING to NOMINAL
   */
  public static Instances fromStringToNominal(Instances data, Object ...columns ){
 
   
    Instances translated_dataset = data;
    StringToNominal translator = new StringToNominal();    // new instance of filter
   
    for(int i=0; i<columns.length; i++){
     
      String[] options = new String[2];
      options[0] = "-R";                                    // "range"
      options[1] = columns[i].toString()
      try
        translator.setOptions(options);                           // set options
        translator.setInputFormat(translated_dataset);             // inform filter about dataset **AFTER** setting options
        translated_dataset = Filter.useFilter(translated_dataset, translator);   // apply filter
        
      }catch(Exception e){
        System.out.println("Exception in filtering the dataset:");
        e.printStackTrace();
        return null;
      }
    }
 
    return translated_dataset;
  }
 
  /**
   * Generate a new Instance entry shaped on a given Attribute structure
   * @param attrs list of attribute types of this entry
   * @param list  list of attribute values of this entry
   * @return
   */
  public static Instance newInstance(ArrayList<Attribute> attrs, Object ...list){
    Instance newInstance = new DenseInstance(list.length);
   
    Attribute att;
    for(int i=0; i<attrs.size(); i++){
         att = attrs.get(i);
         String listval = list[i].toString();

        // System.out.println(" ATTR. " + att + " VAL. " + listval);
         if(att.isNumeric()){
           try
             double dval = Double.parseDouble(listval);
             newInstance.setValue(att, dval );
           }catch(Exception ex){  }
         }
         else{
           try{
             newInstance.setValue(att, list[i].toString() );
           }catch(Exception e){
             e.printStackTrace();
          
         }
    }
   
    return newInstance;
  }
 
 
  /**
   * Add an instance to the instances dataset
   * @param bb dataset of instances
   * @param inst instance to add
   * @return the new dataset
   */
  public  static Instances addInstance(Instances bb, Object ...list){
   
    Instance newInstance = new DenseInstance(list.length);
   
    Attribute att;
    for(int i=0; i<bb.numAttributes(); i++){
         att = bb.attribute(i);
         String listval = list[i].toString();

         //System.out.println(" ATTR. " + att + " VAL. " + listval);
         if(att.isNumeric()){
           try
             double dval = Double.parseDouble(listval);
             newInstance.setValue(att, dval );
           }catch(Exception ex){  }
         }
         else{
           try{
             newInstance.setValue(att, list[i].toString() );
           }catch(Exception e){
             e.printStackTrace();
          
         }
    }
   
    bb.add(newInstance);
    return bb;
  }
 
 
 
 
 
  /**
   * add an instance to the instances dataset
   * @param bb dataset of instances
   * @param inst instance to add
   * @return the new dataset
   */
  public  static Instances addInstance(Instances bb, Instance inst){
    bb.add(inst);
    return bb;
  }
 
 
 
 
 
  public static String isType(Attribute att){
   
    String ret="";

      switch (att.type()) {
       case Attribute.NUMERIC:
        ret="numeric";
        break;
       case Attribute.NOMINAL:
        ret="nominal";
        break;
       case Attribute.STRING:
        ret="string";
        break;    
       case Attribute.DATE:
        ret="date";
        break;
       case Attribute.RELATIONAL:
        ret="relation-valued";
        break;
       default:
        ret="unknown-type";
        }
     
      return ret;
  
  }
}
TOP

Related Classes of trust.weka4jason.utils.Datasetter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.