All Downloads are FREE. Search and download functionalities are using the official Maven repository.

opennlp.tools.cmdline.GenerateManualTool Maven / Gradle / Ivy

There is a newer version: 2.5.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package opennlp.tools.cmdline;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import opennlp.tools.cmdline.ArgumentParser.Argument;

public class GenerateManualTool {

  private static final int MAX_LINE_LENGTH = 110; // optimized for printing

  public static void main(String[] args) throws FileNotFoundException {

    if (args.length != 1) {
      System.out.print(getUsage());
      System.exit(0);
    }

    StringBuilder sb = new StringBuilder();

    appendHeader(sb);

    // organize by package name
    LinkedHashMap> packageNameToolMap = new LinkedHashMap<>();
    for (String toolName : CLI.getToolLookupMap().keySet()) {
      CmdLineTool tool = CLI.getToolLookupMap().get(toolName);
      String packageName = tool.getClass().getPackage().getName();
      packageName = packageName.substring(packageName.lastIndexOf(".") + 1);

      if (!packageNameToolMap.containsKey(packageName)) {
        packageNameToolMap.put(packageName,
            new LinkedHashMap());
      }
      packageNameToolMap.get(packageName).put(toolName, tool);
    }

    // add tools grouped by package
    for (String grouName : packageNameToolMap.keySet()) {
      appendToolGroup(grouName, packageNameToolMap.get(grouName), sb);
    }

    // footer
    appendFooter(sb);

    // output to file
    try (PrintWriter out = new PrintWriter(args[0])) {
      out.println(sb);
    }
  }

  /**
   * @return this tool usage
   */
  private static String getUsage() {
    return "Requires one argument: \n" +
            "  Path to the output XML file \n";
  }

  /**
   * Appends a group of tools, based on the tool package name
   *
   * @param groupName
   * @param toolsMap
   * @param sb
   */
  private static void appendToolGroup(String groupName,
      Map toolsMap, StringBuilder sb) {
    sb.append("
\n\n"); sb.append("").append(firstCaps(groupName)).append("\n\n"); for (String toolName : toolsMap.keySet()) { appendTool(groupName, toolName, toolsMap.get(toolName), sb); } sb.append("
\n\n"); } /** * Appends a tool * * @param groupName * @param toolName * @param tool * @param sb */ private static void appendTool(String groupName, String toolName, CmdLineTool tool, StringBuilder sb) { sb.append("
\n\n"); sb.append("").append(toolName).append("\n\n"); sb.append("").append(firstCaps(tool.getShortDescription())) .append("\n\n"); appendCode(tool.getHelp(), sb); if (TypedCmdLineTool.class.isAssignableFrom(tool.getClass())) { appendHelpForTool((TypedCmdLineTool) tool, sb); } sb.append("
\n\n"); } @SuppressWarnings("unchecked") private static void appendHelpForTool(TypedCmdLineTool tool, StringBuilder sb) { Class type = tool.type; Set formats = StreamFactoryRegistry.getFactories(type).keySet(); sb.append("The supported formats and arguments are:\n\n"); Map> formatArguments = new LinkedHashMap<>(); for (String formatName : formats) { if (!StreamFactoryRegistry.DEFAULT_FORMAT.equals(formatName)) { ObjectStreamFactory format = tool.getStreamFactory(formatName); formatArguments.put(formatName, ArgumentParser.createArguments(format.getParameters())); } } appendArgumentTable(formatArguments, sb); } private static void appendArgumentTable( Map> formatArguments, StringBuilder sb) { sb.append( "\n"); sb.append( "FormatArgumentValue" + "OptionalDescription\n"); sb.append("\n"); for (String format : formatArguments.keySet()) { List arguments = formatArguments.get(format); int i = 0; for (Argument argument : arguments) { sb.append("\n"); if (i == 0) { sb.append("").append(format).append("\n"); } sb.append("").append(argument.getArgument()) .append("\n"); sb.append("").append(argument.getValue()).append("\n"); sb.append("").append(yes(argument.getOptional())) .append("\n"); sb.append("").append(firstCaps(argument.getDescription())) .append("\n"); sb.append("\n"); i++; } } sb.append("\n"); sb.append("\n\n"); } private static void appendHeader(StringBuilder sb) { sb.append("\n" + "\n" + "\n" + "\n\n" + "\n\n" + "\n\n" + "The Command Line Interface\n\n" + "" + "This section details the available tools and parameters of the Command Line Interface. " + "For a introduction in its usage please refer to . " + "\n\n"); } private static void appendFooter(StringBuilder sb) { sb.append("\n\n"); } private static String firstCaps(String str) { if (str.length() > 1) { return str.substring(0, 1).toUpperCase() + str.substring(1); } else { return str; } } private static String yes(boolean optional) { if (optional) { return "Yes"; } return "No"; } private static void appendCode(String help, StringBuilder sb) { sb.append("\n" + "\n").append(" \n"); } /** * Prevents long lines. Lines are optimized for printing. * * @param stringBlock * @return */ private static String splitLongLines(String stringBlock) { StringBuilder sb = new StringBuilder(); String line; try { BufferedReader reader = new BufferedReader(new StringReader(stringBlock)); while ((line = reader.readLine()) != null) { if (line.length() <= MAX_LINE_LENGTH) { sb.append(line).append("\n"); } else { StringTokenizer tok = new StringTokenizer(line, " "); int lineLen = 0; while (tok.hasMoreTokens()) { String word = tok.nextToken() + " "; if (lineLen + word.length() > MAX_LINE_LENGTH) { sb.append("\n "); lineLen = 8; } sb.append(word); lineLen += word.length(); } } } } catch (Exception e) { // nothing to do } return sb.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy