Exemplo n.º 1
0
  /**
   * Convert to the Mallet format.
   *
   * @param train
   * @return
   * @throws IOException
   */
  public static InstanceList convert(Relation<?> train) {
    boolean isTrain = (train instanceof TrainRelation) ? true : false;

    StringBuffer sb = new StringBuffer();
    for (FeatureVector<?> fv : train) {
      sb.append(fv.getId() + "\t");

      if (isTrain) {
        sb.append(((LabeledFeatureVector<?>) fv).getLabel().toString() + "\t");
      } else {
        sb.append("X\t");
      }

      for (String feature : train.getMetadata().keySet()) {
        if (fv.containsKey(feature)) {
          sb.append(feature + "=" + fv.get(feature).getValue() + " ");
        }
      }
      sb.append("\n");
    }

    InstanceList il = loadInstances(sb.toString());

    return il;
  }
Exemplo n.º 2
0
  /**
   * Convert to the Mallet format.
   *
   * @param fv
   * @param m
   * @return
   */
  public static Instance convert(FeatureVector<?> fv, Metadata m) {
    boolean isTrain = (fv instanceof LabeledFeatureVector) ? true : false;

    StringBuffer sb = new StringBuffer(fv.getId() + "\t");

    if (isTrain) {
      sb.append(((LabeledFeatureVector<?>) fv).getLabel().toString() + "\t");
    } else {
      sb.append("1\t");
    }

    for (String feature : m.keySet()) {
      if (fv.containsKey(feature)) {
        sb.append(feature + "=" + fv.get(feature).getValue() + " ");
      }
    }

    InstanceList il = loadInstances(sb.toString());

    return il.get(0);
  }