Example usage for weka.core.converters TextDirectoryLoader setOutputFilename

List of usage examples for weka.core.converters TextDirectoryLoader setOutputFilename

Introduction

In this page you can find the example usage for weka.core.converters TextDirectoryLoader setOutputFilename.

Prototype

public void setOutputFilename(boolean value) 

Source Link

Document

Sets whether the filename will be stored as an extra attribute.

Usage

From source file:adams.flow.transformer.WekaTextDirectoryReader.java

License:Open Source License

/**
 * Executes the flow item./*from   w  w w  . j  a v a 2s  . c  om*/
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    TextDirectoryLoader loader;
    Instances data;
    File file;

    result = null;

    try {
        if (m_InputToken.getPayload() instanceof File)
            file = (File) m_InputToken.getPayload();
        else
            file = new PlaceholderFile((String) m_InputToken.getPayload());

        if (file.isDirectory()) {
            loader = new TextDirectoryLoader();
            loader.setDirectory(file);
            loader.setOutputFilename(m_StoreFilename);
            loader.setCharSet(m_CharSet);
            data = loader.getDataSet();
            m_OutputToken = new Token(data);
            updateProvenance(m_OutputToken);
        } else {
            result = "Input is not a directory: " + file;
        }
    } catch (Exception e) {
        result = handleException("Failed to load directory with text files: ", e);
    }

    return result;
}

From source file:org.ml.classifier.TextDirectoryToArff.java

License:Open Source License

public Instances createDataset(String directoryPath) throws Exception {

    //    FastVector atts = new FastVector(2);
    //    atts.addElement(new Attribute("filename", (FastVector) null));
    //    atts.addElement(new Attribute("contents", (FastVector) null));
    //    Instances data = new Instances("text_files_in_" + directoryPath, atts, 0);
    ///*from w w  w .ja va 2  s.  c  o  m*/
    //    File dir = new File(directoryPath);
    //    String[] files = dir.list();
    //    for (int i = 0; i < files.length; i++) {
    //      if (files[i].endsWith(".txt")) {
    //   try {
    //     double[] newInst = new double[2];
    //     newInst[0] = (double)data.attribute(0).addStringValue(files[i]);
    //     File txt = new File(directoryPath + File.separator + files[i]);  
    //     InputStreamReader is;
    //     is = new InputStreamReader(new FileInputStream(txt));
    //     StringBuffer txtStr = new StringBuffer();
    //     int c;
    //     while ((c = is.read()) != -1) {
    //       txtStr.append((char)c);
    //     }
    //     newInst[1] = (double)data.attribute(1).addStringValue(txtStr.toString());
    //     data.add(new Instance(1.0, newInst));
    //   } catch (Exception e) {
    //     //System.err.println("failed to convert file: " + directoryPath + File.separator + files[i]);
    //   }
    //      }
    //    }

    // convert the directory into a dataset
    TextDirectoryLoader loader = new TextDirectoryLoader();
    loader.setDirectory(new File(directoryPath));
    loader.setOutputFilename(false);

    Instances data = loader.getDataSet();
    //System.out.println("\n\nImported data:\n\n" + dataRaw);

    return data;
}