Integration of RapidMiner in Java application

前端 未结 3 843
北荒
北荒 2021-01-03 04:00

I have a text classification process in RapidMiner. It reads the test data from specified excel ssheet and does the classification. I have also a small Java application whic

相关标签:
3条回答
  • 2021-01-03 04:37

    I see two ways to do that.

    The first one would be to change programatically the XML definition of your process. Rapidminer processes are specified by an XML file with .rmp extension. In the file you will find the definition of the operator you wish to change. This is an excerpt from a simple process specifiing the Read Excel operator:

    <operator activated="true" class="read_excel" compatibility="5.3.005" expanded="true" height="60" name="Read Excel" width="90" x="313" y="75">
        <parameter key="excel_file" value="D:\file.xls"/>    <!-- HERE IS THE FILE PATH -->
        <parameter key="sheet_number" value="1"/>
        <parameter key="imported_cell_range" value="A1"/>
        <parameter key="encoding" value="SYSTEM"/>
        <parameter key="first_row_as_names" value="true"/>
        <list key="annotations"/>
        <parameter key="date_format" value=""/>
        <parameter key="time_zone" value="SYSTEM"/>
        <parameter key="locale" value="English (United States)"/>
        <list key="data_set_meta_data_information"/>
        <parameter key="read_not_matching_values_as_missings" value="true"/>
        <parameter key="datamanagement" value="double_array"/>
    </operator>
    

    I highlighted the part where the path to the excel file is. You can overwrite that in your application. Just be careful not to break the XML file.


    The other way is to modify the operator after you load the process in your java application. You can get a reference to your operator by Process#getOperator(String name) or Process#getAllOperators(). I guess it should be of one of these classes:

    com.rapidminer.operator.io.ExcelExampleSource
    com.rapidminer.operator.nio.ExcelExampleSource
    

    When you find the correct operator you modify the path by Operator#setParameter(String key, String Value).

    This code works for me with RapidMiner 5.3: (the process is just a Read Excel operator and a Write CSV operator)

    package sorapid;
    
    import com.rapidminer.Process;
    import com.rapidminer.RapidMiner;
    import com.rapidminer.operator.Operator;
    import com.rapidminer.operator.OperatorException;
    import com.rapidminer.operator.io.ExcelExampleSource;
    import com.rapidminer.tools.XMLException;
    import java.io.File;
    import java.io.IOException;
    
    public class SOrapid {
    
      public static void main(String[] args) {
        try {
          RapidMiner.setExecutionMode(RapidMiner.ExecutionMode.COMMAND_LINE);
          RapidMiner.init();
    
          Process process = new Process(new File("c:\\Users\\Matlab\\.RapidMiner5\\repositories\\Local Repository\\processes\\test.rmp"));
          Operator op = process.getOperator("Read Excel");
          op.setParameter(ExcelExampleSource.PARAMETER_EXCEL_FILE, "d:\\excel.xls");
          process.run();
    
        } catch (IOException | XMLException | OperatorException ex) {
          ex.printStackTrace();
        }
      }
    }
    
    0 讨论(0)
  • 2021-01-03 04:44

    Works fine for me:

    • Download Rapidminer (and unzip the file)
    • Into "lib" directory, you need:
      1. rapidminer.jar
      2. launcher.jar
      3. All jar in "/lib/freehep" directory.
    • Put libs 1, 2 and 3 in your classpath java project (libraries)
    • Copy this code and run:
    
    
        import com.rapidminer.Process;
        import com.rapidminer.RapidMiner;
        import com.rapidminer.operator.Operator;
        import com.rapidminer.operator.OperatorException;
        import com.rapidminer.operator.io.ExcelExampleSource;
        import com.rapidminer.tools.XMLException;
        import java.io.File;
        import java.io.IOException;
        import java.lang.Object;
    
        public class ReadRapidminerProcess {
          public static void main(String[] args) {
            try {
              RapidMiner.setExecutionMode(RapidMiner.ExecutionMode.COMMAND_LINE);
              RapidMiner.init();
    
              Process process = new Process(new File("/your_path/your_file.rmp"));
              process.run();
    
            } catch (IOException | XMLException | OperatorException ex) {
              ex.printStackTrace();
            }
          }
        }
    
    

    I hope to help you, I searched a lot before finding the answer.

    0 讨论(0)
  • 2021-01-03 04:58

    Try this:

    private SimpleExampleSet ReadExcel( File processXMLFile_, File excelFile_ ) throws IOException, XMLException, OperatorException
    {
        IOContainer outParameters   = null;
        Process     readExcel       = new Process( processXMLFile_ );
        IOObject    inObject        = new SimpleFileObject( excelFile_ );
        IOContainer inParameters    = new IOContainer( inObject );
    
        outParameters   = readExcel.run( inParameters );
    
        SimpleExampleSet    result  = (SimpleExampleSet) outParameters.getElementAt( 0 );
    
        return result;
    
    }
    

    Sorry, I cannot post image with RapidMiner script if you need, I can send it to email.

    0 讨论(0)
提交回复
热议问题