/* (non-Javadoc) * @see lexelt.ILexeltWriter#getInstances(lexelt.ILexelt) */ public Object getInstances(ILexelt p_Lexelt) throws ClassNotFoundException { svm_problem retVal = new svm_problem(); ArrayList<svm_node[]> featureVectors = new ArrayList<svm_node[]>(); ArrayList<Double> classes = new ArrayList<Double>(); int[][] indice = this.loadStatistic(p_Lexelt); if (indice == null) { throw new IllegalArgumentException("the input lexelt should not be null."); } IStatistic stat = p_Lexelt.getStatistic(); int size = p_Lexelt.size(); // instance count for (int i = 0; i < size; i++) { IInstance instance = p_Lexelt.getInstance(i); svm_node[] featureVector = this.getVector(instance, stat, indice); ArrayList<String> tags = instance.getTag(); if (tags.size() > 0) { for (String tag : tags) { double c = Double.parseDouble(tag); featureVectors.add(Arrays.copyOf(featureVector, featureVector.length)); classes.add(c); } } else { featureVectors.add(featureVector); classes.add(new Double(0)); } } retVal.l = featureVectors.size(); retVal.x = new svm_node[retVal.l][]; retVal.y = new double[retVal.l]; for (int i = 0; i < featureVectors.size(); i++) { retVal.x[i] = featureVectors.get(i); retVal.y[i] = classes.get(i); } return retVal; }
/** * @param p_Instance * @param p_Stat * @return */ private String toString(IInstance p_Instance, IStatistic p_Stat, int[][] p_Indice) { StringBuilder featureBuilder = new StringBuilder(); svm_node[] features = this.getVector(p_Instance, p_Stat, p_Indice); for (int i = 0; i < features.length; i++) { featureBuilder.append(" "); featureBuilder.append(features[i].index); featureBuilder.append(":"); featureBuilder.append(features[i].value); } String featureOnly = featureBuilder.toString(); StringBuilder builder = new StringBuilder(); for (Integer tag : this.processTags(p_Stat, p_Instance.getTag())) { builder.append(tag.toString()); builder.append(featureOnly); builder.append("\n"); } return builder.toString(); }