示例#1
0
  /**
   * Test the class from the command line. The instance query should be specified with -Q sql_query
   *
   * @param args contains options for the instance query
   */
  public static void main(String args[]) {

    try {
      InstanceQuery iq = new InstanceQuery();
      String query = Utils.getOption('Q', args);
      if (query.length() == 0) {
        iq.setQuery("select * from Experiment_index");
      } else {
        iq.setQuery(query);
      }
      iq.setOptions(args);
      try {
        Utils.checkForRemainingOptions(args);
      } catch (Exception e) {
        System.err.println("Options for weka.experiment.InstanceQuery:\n");
        Enumeration en = iq.listOptions();
        while (en.hasMoreElements()) {
          Option o = (Option) en.nextElement();
          System.err.println(o.synopsis() + "\n" + o.description());
        }
        System.exit(1);
      }

      Instances aha = iq.retrieveInstances();
      iq.disconnectFromDatabase();
      // query returned no result -> exit
      if (aha == null) return;
      // The dataset may be large, so to make things easier we'll
      // output an instance at a time (rather than having to convert
      // the entire dataset to one large string)
      System.out.println(new Instances(aha, 0));
      for (int i = 0; i < aha.numInstances(); i++) {
        System.out.println(aha.instance(i));
      }
    } catch (Exception e) {
      e.printStackTrace();
      System.err.println(e.getMessage());
    }
  }
示例#2
0
  /**
   * Parses a given list of options.
   * <!-- options-start -->
   * Valid options are:
   *
   * <p>
   *
   * <pre> -Q &lt;query&gt;
   *  SQL query to execute.</pre>
   *
   * <pre> -S
   *  Return sparse rather than normal instances.</pre>
   *
   * <pre> -U &lt;username&gt;
   *  The username to use for connecting.</pre>
   *
   * <pre> -P &lt;password&gt;
   *  The password to use for connecting.</pre>
   *
   * <pre> -D
   *  Enables debug output.</pre>
   *
   * <!-- options-end -->
   *
   * @param options the list of options as an array of strings
   * @throws Exception if an option is not supported
   */
  public void setOptions(String[] options) throws Exception {

    String tmpStr;

    setSparseData(Utils.getFlag('S', options));

    tmpStr = Utils.getOption('Q', options);
    if (tmpStr.length() != 0) setQuery(tmpStr);

    tmpStr = Utils.getOption('U', options);
    if (tmpStr.length() != 0) setUsername(tmpStr);

    tmpStr = Utils.getOption('P', options);
    if (tmpStr.length() != 0) setPassword(tmpStr);

    setDebug(Utils.getFlag('D', options));
  }
  /**
   * Parses a given list of options.
   * <!-- options-start -->
   * Valid options are:
   *
   * <p>
   *
   * <pre> -Q &lt;query&gt;
   *  SQL query to execute.</pre>
   *
   * <pre> -S
   *  Return sparse rather than normal instances.</pre>
   *
   * <pre> -U &lt;username&gt;
   *  The username to use for connecting.</pre>
   *
   * <pre> -P &lt;password&gt;
   *  The password to use for connecting.</pre>
   *
   * <pre> -D
   *  Enables debug output.</pre>
   *
   * <!-- options-end -->
   *
   * @param options the list of options as an array of strings
   * @throws Exception if an option is not supported
   */
  public void setOptions(String[] options) throws Exception {

    String tmpStr;

    setSparseData(Utils.getFlag('S', options));

    tmpStr = Utils.getOption('Q', options);
    if (tmpStr.length() != 0) setQuery(tmpStr);

    tmpStr = Utils.getOption('U', options);
    if (tmpStr.length() != 0) setUsername(tmpStr);

    tmpStr = Utils.getOption('P', options);
    if (tmpStr.length() != 0) setPassword(tmpStr);

    tmpStr = Utils.getOption("custom-props", options);
    if (tmpStr.length() == 0) setCustomPropsFile(null);
    else setCustomPropsFile(new File(tmpStr));

    setDebug(Utils.getFlag('D', options));
  }
  public static Instances readDB(String sqlQuery, String tableName, int sourceIndex) {
    /*
    proper mysql query is needed.
    sqlQuery : "SELECT * FROM AccelerometerSensorProbe"
    */
    if (!sqlQuery.contains(tableName)) {
      System.out.println("tableName must be included in sqlQuery!");
      System.out.println("tableName : " + tableName + " / sqlQuery : " + sqlQuery);
      System.exit(-1);
    }
    ProjectEvaluator.tableUsed(tableName);

    Instances dataSet = null;
    try {
      InstanceQuery query = new InstanceQuery();
      if (sourceIndex == 0) {
        query.setDatabaseURL(DBConn.url);
        query.setUsername(DBConn.user);
        query.setPassword(DBConn.pwd);
      } else if (sourceIndex == 1) {
        query.setDatabaseURL(DBConn.url_validation);
        query.setUsername(DBConn.user_validation);
        query.setPassword(DBConn.pwd_validation);
      } else if (sourceIndex == 2) {
        query.setDatabaseURL(ProjectEvaluator.testUrl);
        query.setUsername(ProjectEvaluator.testUser);
        query.setPassword(ProjectEvaluator.testPwd);
      } else {
        throw new Exception("source index must be 0, 1 or 2!");
      }
      query.setQuery(sqlQuery);
      dataSet = query.retrieveInstances();
      dataSet.setClassIndex(dataSet.numAttributes() - 1);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return dataSet;
  }
  public void processFolder(File folder) throws Exception {

    if (!folder.isDirectory()) {
      // manipulate file here
      String fileName = folder.getName();
      System.out.println(fileName);
      // String extension = getFileExtension(fileName);
      testdata = new Instances(new BufferedReader(new FileReader(folder)));
      if (!fileName.startsWith(".") && (fileName.contains(".csv") || fileName.contains(".xls"))) {
        CSVLoader loader = new CSVLoader();
        loader.setSource(new File(folder.getAbsolutePath()));
        traindata = loader.getDataSet();
        System.out.println(traindata.toSummaryString());
        this.chooseClassifier();
      } else if (!fileName.startsWith(".") && fileName.contains(".txt")) {

        TextDirectoryLoader loader = new TextDirectoryLoader();
        System.out.println("About to load text file " + fileName);
        System.out.println("Name of path " + folder.getAbsolutePath());
        loader.setSource(folder);
        traindata = loader.getDataSet();
        System.out.println(traindata.toSummaryString());
        this.chooseClassifier();

      } else if (!fileName.startsWith(".") && fileName.contains(".json")) {
        JSONLoader loader = new JSONLoader();
        loader.setSource(new File(folder.getAbsolutePath()));
        traindata = loader.getDataSet();
        System.out.println(traindata.toSummaryString());
        this.chooseClassifier();
      } else if (!fileName.startsWith(".") && fileName.contains(".xrff")) {
        XRFFLoader loader = new XRFFLoader();
        loader.setSource(new File(folder.getAbsolutePath()));
        traindata = loader.getDataSet();
        System.out.println(traindata.toSummaryString());
        this.chooseClassifier();
      } else if (!fileName.startsWith(".") && fileName.contains(".arff")) {
        traindata = new Instances(new BufferedReader(new FileReader(folder.getAbsolutePath())));
        testdata = new Instances(new BufferedReader(new FileReader(folder)));
        System.out.println(traindata.toSummaryString());
        this.chooseClassifier();
      } else if (!fileName.startsWith(".") && fileName.contains(".mdf")) {
        DatabaseConnection loader = new DatabaseConnection();
        loader.connectToDatabase();
        InstanceQuery query = new InstanceQuery();
        query.setUsername("lamogha");
        query.setPassword("l@mmyPHD");
        query.setQuery("select * from customers");
        // You can declare that your data set is sparse
        // query.setSparseData(true);
        Instances data = query.retrieveInstances();
        System.out.println(data.toSummaryString());
        this.chooseClassifier();
      }
    } else {

      for (final File fileEntry : folder.listFiles()) {
        if (fileEntry.isDirectory()) {
          this.processFolder(fileEntry);
        } else {
          // manipulate file here
          String fileName = fileEntry.getName();
          System.out.println(fileName);

          if (!fileName.startsWith(".")
              && (fileName.contains(".csv") || fileName.contains(".xls"))) {
            CSVLoader loader = new CSVLoader();
            loader.setSource(new File(fileEntry.getAbsolutePath()));
            traindata = loader.getDataSet();
            System.out.println(traindata.toSummaryString());
            this.chooseClassifier();
          } else if (!fileName.startsWith(".") && fileName.contains(".txt")) {

            TextDirectoryLoader loader = new TextDirectoryLoader();
            System.out.println("About to load text file " + fileName);
            System.out.println("Name of path " + fileEntry.getAbsolutePath());
            loader.setSource(folder);
            traindata = loader.getDataSet();
            System.out.println(traindata.toSummaryString());
            this.chooseClassifier();

          } else if (!fileName.startsWith(".") && fileName.contains(".json")) {
            JSONLoader loader = new JSONLoader();
            loader.setSource(new File(fileEntry.getAbsolutePath()));
            traindata = loader.getDataSet();
            System.out.println(traindata.toSummaryString());
            this.chooseClassifier();
          } else if (!fileName.startsWith(".") && fileName.contains(".xrff")) {
            XRFFLoader loader = new XRFFLoader();
            loader.setSource(new File(fileEntry.getAbsolutePath()));
            traindata = loader.getDataSet();
            System.out.println(traindata.toSummaryString());
            this.chooseClassifier();
          } else if (!fileName.startsWith(".")) {
            traindata =
                new Instances(new BufferedReader(new FileReader(fileEntry.getAbsolutePath())));
            System.out.println(traindata.toSummaryString());
            this.chooseClassifier();
          } else if (!fileName.startsWith(".") && fileName.contains(".mdf")) {
            DatabaseConnection loader = new DatabaseConnection();
            loader.connectToDatabase();
            InstanceQuery query = new InstanceQuery();
            query.setUsername("lamogha");
            query.setPassword("l@mmyPHD");
            query.setQuery("select * from customers");
            // You can declare that your data set is sparse
            // query.setSparseData(true);
            Instances data = query.retrieveInstances();
            System.out.println(data.toSummaryString());
            this.chooseClassifier();
          }
        }
      }
      // System.exit(0);
    }
  }