示例#1
0
  @Override
  public Dataset buildDataset() {
    Dataset ret = new Dataset(template, Utils.relativize(file));

    int inputDim = template.inputTemplate.getDescriptionLength();
    int outputDim = template.outputTemplate.getDescriptionLength();

    if (file.exists()) {
      try {
        int index = 0, count = 0;
        BufferedReader reader = new BufferedReader(new FileReader(file));
        for (String line = reader.readLine();
            line != null && count < instanceNumber;
            line = reader.readLine(), index++) {
          if (index < startIndex) continue;
          String[] tokens = line.split(separators);
          Object input = getData(Arrays.copyOfRange(tokens, 0, inputDim), template.inputTemplate);
          Object output =
              getData(
                  Arrays.copyOfRange(tokens, inputDim, inputDim + outputDim),
                  template.outputTemplate);
          ret.add(template.newInstance(input, output));
          count++;
        }
        reader.close();
        ret.setReadyState();
      } catch (IOException e) {
      }
    }

    return ret;
  }
示例#2
0
  @Override
  public Dataset buildDataset() {
    Dataset ret = new Dataset(datasetTemplate);

    int sourceDim = datasetTemplate.sourceTemplate.getDescriptionLength();
    int targetDim = datasetTemplate.targetTemplate.getDescriptionLength();

    if (file.exists()) {
      try {
        int index = 0, count = 0;
        BufferedReader reader = new BufferedReader(new FileReader(file));
        if (hasHeader) reader.readLine();
        for (String line = reader.readLine();
            line != null && count < getInstanceNumber();
            line = reader.readLine(), index++) {
          if (index < startIndex) continue;
          String[] tokens = line.split(separators);
          if (tokens.length != (sourceDim + targetDim)) {
            reader.close();
            throw new RuntimeException(
                "Expected " + (sourceDim + targetDim) + " tokens, found " + tokens.length);
          }

          Data source = new Data();
          try {
            source.add(
                datasetTemplate.sourceTemplate.loadElement(
                    Arrays.asList(Arrays.copyOfRange(tokens, 0, sourceDim))));
          } catch (Exception ex) {
            System.out.println(line);
            reader.close();
            throw ex;
          }
          Data target = new Data();
          target.add(
              datasetTemplate.targetTemplate.loadElement(
                  Arrays.asList(Arrays.copyOfRange(tokens, sourceDim, tokens.length))));
          ret.add(new Instance(source, target));
          count++;
        }
        reader.close();
      } catch (IOException e) {
      }
    }

    return ret;
  }