Example #1
0
  /**
   * @param args
   * @throws Exception
   */
  public static void main(String[] args) throws Exception {
    /* Parse command line arguments */
    Getopt g = new Getopt("gateExtractor", args, "i:r:te");
    g.setOpterr(false);

    String inputPath = "";
    String outputPath = "";

    boolean train = false;
    boolean eval = false;
    boolean run = false;

    int c;
    String arg;
    while ((c = g.getopt()) != -1) {
      switch (c) {
        case 'i':
          arg = g.getOptarg();
          if (arg == null || arg.isEmpty()) {
            usage("Please provide an input path");
          }
          inputPath = arg;
          break;
        case 'r':
          run = true;
          arg = g.getOptarg();
          if (arg == null || arg.isEmpty()) {
            usage("Please provide an output path");
          }
          outputPath = arg;
          break;
        case 't':
          train = true;
          break;
        case 'e':
          eval = true;
          break;
        case '?':
        default:
          usage(null);
      }
    }

    if (args.length == 0 || (!run && !train && !eval)) {
      usage("Nothing to do.");
    }

    if (inputPath == null || inputPath.isEmpty()) {
      usage("Please provide an input path");
    }

    if (run && (outputPath == null || outputPath.isEmpty())) {
      usage("Please provide an output directory!");
    }

    if (train && eval) {
      usage("Only one mode allowed at a time");
    }

    if (train && run) {
      usage("Only one mode allowed at a time");
    }

    if (eval && run) {
      usage("Only one mode allowed at a time");
    }

    /* Initialize GATE */
    String location =
        new File(Main.class.getProtectionDomain().getCodeSource().getLocation().getPath())
            .getParent();
    String resourcesFolder = location + "/resources";
    Gate.setGateHome(new File(resourcesFolder));

    /* Create ml-config.xml with threads */

    createConfig(resourcesFolder + File.separator);
    Gate.init();

    /* Load Corpus */
    log.info("Loading Corpus ... ");
    Corpus corpus = Factory.newCorpus("Training Corpus");
    File directory = new File(inputPath);
    URL url = directory.toURI().toURL();
    corpus.populate(url, null, null, true);
    log.info("Done loading Corpus!");

    Pipeline pipeline = null;

    /* Do Tagging */
    pipeline = new Tagger();
    pipeline.run(corpus, resourcesFolder);

    /* Train */
    if (train) {
      pipeline = new Trainer();
      pipeline.run(corpus, resourcesFolder);
    }

    /* Apply learned rules */
    if (run) {
      pipeline = new Extractor();
      pipeline.run(corpus, resourcesFolder);

      ExecutorService executorService = Executors.newFixedThreadPool(20);
      for (int i = 0; i < corpus.size(); i++) {
        executorService.execute(new OutputGenerator(outputPath, corpus.get(i)));
      }
      executorService.shutdown();
      executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
    }

    /* Evaluate results */
    if (eval) {
      pipeline = new Evaluator();
      pipeline.run(corpus, resourcesFolder);
    }

    /* Clean up */
    Factory.deleteResource(corpus);
    outputFile_mlConfigThreads.delete();
  }
Example #2
0
  @Override
  public String execute(String uuid, String... params) throws RemoteException {
    try {
      LongOpt[] longopts = new LongOpt[4];
      longopts[0] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
      longopts[1] = new LongOpt("project", LongOpt.REQUIRED_ARGUMENT, null, 'p');
      longopts[2] = new LongOpt("file", LongOpt.REQUIRED_ARGUMENT, null, 'f');
      longopts[3] = new LongOpt("dest", LongOpt.REQUIRED_ARGUMENT, null, 'd');

      Getopt g = new Getopt("put", params, "", longopts);
      g.setOpterr(false);

      String project = null;
      String path = null;
      String[] parents = null;
      String dest = null;
      int c;
      while ((c = g.getopt()) != -1) {
        switch (c) {
          case 'p':
            project = g.getOptarg();
            break;
          case 'f':
            path = g.getOptarg();
            break;
          case 'd':
            dest = g.getOptarg();
            if (!".".equals(dest)) {
              parents = CLIUtils.convertAbsolutePathToArray(dest, false, false);
            }
            break;
          case 'h':
            return usage(uuid);
          default:
            return CLIResultCodes.INTERNAL_SERVER_ERROR
                + "::Could not execute the command \n "
                + usage(uuid);
        }
      }

      if (uuid == null) {
        return CLIResultCodes.USER_NOT_AUTHORIZED + "::--uuid parameter is required!";
      }

      if (project == null) {
        return CLIResultCodes.USER_ERROR + "::--project parameter is required!";
      }

      if (path == null) {
        return CLIResultCodes.USER_ERROR + "::--file parameter is required!";
      }

      if (dest == null) {
        return CLIResultCodes.USER_ERROR + "::--dest parameter is required!";
      }

      if (!fileService.exists(uuid, project, null, null)) {
        return CLIResultCodes.PROJECT_NOT_FOUND
            + "::Could not find project ["
            + project
            + "] for the current user";
      }

      File src = new File(path);

      if (!src.exists()) {
        return CLIResultCodes.FILE_DOES_NOT_EXIST
            + "::File ["
            + src.getName()
            + "] does not exist!";
      }

      String fileName = src.getName();
      if (src.isDirectory()) {
        fileService.create(uuid, project, parents, fileName, true);
        String childrenPath = fileName;
        if (parents != null) {
          childrenPath = dest + "/" + fileName;
        }
        createDir(uuid, project, childrenPath, src.listFiles());
        return CLIResultCodes.OK
            + "::Put the directory ["
            + src.getName()
            + "] to the destination ["
            + dest
            + "]";
      } else {
        fileService.upload(uuid, project, parents, fileName, src, true);
        return CLIResultCodes.OK
            + "::Put the file ["
            + src.getName()
            + "] to the destination ["
            + dest
            + "]";
      }
    } catch (UserNotAuthorizedException e) {
      CLIUtils.handleException(e, logger);
      return CLIResultCodes.USER_NOT_AUTHORIZED + "::User not authorized!";
    } catch (Exception e) {
      CLIUtils.handleException(e, logger);
      return CLIResultCodes.OK + "::Could not execute the command \n " + usage(uuid);
    }
  }