package trust.weka4jason;
import weka.core.Instances;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.NominalToString;
import weka.filters.unsupervised.attribute.StringToNominal;
public class WekaUtils {
/**
* 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 dataset, Object ...columns ){
Instances translated_dataset = dataset;
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 translated_dataset;
}
/**
* 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 fromNominalToString(Instances dataset, Object ...columns ){
Instances translated_dataset = dataset;
NominalToString translator = new NominalToString(); // new instance of filter
String col = "";
for(Object c:columns)
col += c.toString()+",";
String[] options = new String[2];
options[0] = "-C"; // "range"
options[1] = col;
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 translated_dataset;
}
}