예제 #1
0
  /**
   * XMLを標準出力する。
   *
   * @param my_doc XMLの Document Node 。
   * @param sortflag ソートするか否か。
   */
  public static void output(Document my_doc, boolean sortflag, UDF_Env env) {
    try {
      if (sortflag) com.udfv.util.UDF_XML.sortDocumentbyEnv(my_doc, env);

      OutputFormat format = new OutputFormat(my_doc, "UTF-8", true);
      format.setLineWidth(0);
      OutputStreamWriter osw = new OutputStreamWriter(System.out, "UTF-8");
      XMLSerializer serial = new XMLSerializer(osw, format);
      serial.serialize(my_doc.getDocumentElement());
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
  public String format_using(OutputFormat format) {
    if (format.isJavaFamily()) return format_java_family(format);

    if (format == OutputFormat.DAIKON) return format_daikon();
    if (format == OutputFormat.ESCJAVA) return format_esc();
    if (format == OutputFormat.SIMPLIFY) return format_simplify();

    return format_unimplemented(format);
  }
예제 #3
0
  public String format_using(OutputFormat format) {
    if (format == OutputFormat.DAIKON) return format_daikon();
    if (format == OutputFormat.IOA) return format_ioa();
    if (format == OutputFormat.SIMPLIFY) return format_simplify();
    if (format.isJavaFamily()) {
      return format_unimplemented(format);
    }

    return format_unimplemented(format);
  }
예제 #4
0
  /**
   * XMLをファイルに出力する。
   *
   * @param my_doc XMLの Document Node 。
   * @param os 出力先のストリーム。
   * @param sortflag ソートするか否か。
   */
  public static void output(Document my_doc, OutputStream os, boolean sortflag, UDF_Env env) {
    if (sortflag) {
      try {
        com.udfv.util.UDF_XML.sortDocumentbyEnv(my_doc, env);
      } catch (Exception e1) {
        e1.printStackTrace();
      }
    }

    try {
      OutputFormat format = new OutputFormat(my_doc, "UTF-8", true);
      format.setLineWidth(0);
      //            Writer out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
      Writer out = new OutputStreamWriter(os, "UTF-8");
      XMLSerializer serial = new XMLSerializer(out, format);
      serial.serialize(my_doc.getDocumentElement());
      out.close();
    } catch (Exception e2) {
      e2.printStackTrace();
    }
  }
예제 #5
0
  /**
   * エラーリストを XMLとして出力する。
   *
   * @param el エラーリスト
   */
  public static void outputError(UDF_ErrorList el) {

    Document doc = UDF_Util.genDocument();
    Element root = doc.createElement("udf-error");

    for (int i = 0; i < el.getSize(); i++) {
      el.getError(i).toXML(doc, root);
    }

    doc.appendChild(root);

    try {
      OutputFormat format = new OutputFormat(doc, "UTF-8", true);
      format.setLineWidth(0);
      OutputStreamWriter osw = new OutputStreamWriter(System.out, "UTF-8");
      XMLSerializer serial = new XMLSerializer(osw, format);
      serial.serialize(doc.getDocumentElement());
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
예제 #6
0
  /**
   * Prints out all matches of a semgrex pattern on a file of dependencies. <br>
   * Usage:<br>
   * java edu.stanford.nlp.semgraph.semgrex.SemgrexPattern [args] <br>
   * See the help() function for a list of possible arguments to provide.
   */
  public static void main(String[] args) throws IOException {
    Map<String, Integer> flagMap = Generics.newHashMap();

    flagMap.put(PATTERN, 1);
    flagMap.put(TREE_FILE, 1);
    flagMap.put(MODE, 1);
    flagMap.put(EXTRAS, 1);
    flagMap.put(CONLLU_FILE, 1);
    flagMap.put(OUTPUT_FORMAT_OPTION, 1);

    Map<String, String[]> argsMap = StringUtils.argsToMap(args, flagMap);
    args = argsMap.get(null);

    // TODO: allow patterns to be extracted from a file
    if (!(argsMap.containsKey(PATTERN)) || argsMap.get(PATTERN).length == 0) {
      help();
      System.exit(2);
    }
    SemgrexPattern semgrex = SemgrexPattern.compile(argsMap.get(PATTERN)[0]);

    String modeString = DEFAULT_MODE;
    if (argsMap.containsKey(MODE) && argsMap.get(MODE).length > 0) {
      modeString = argsMap.get(MODE)[0].toUpperCase();
    }
    SemanticGraphFactory.Mode mode = SemanticGraphFactory.Mode.valueOf(modeString);

    String outputFormatString = DEFAULT_OUTPUT_FORMAT;
    if (argsMap.containsKey(OUTPUT_FORMAT_OPTION) && argsMap.get(OUTPUT_FORMAT_OPTION).length > 0) {
      outputFormatString = argsMap.get(OUTPUT_FORMAT_OPTION)[0].toUpperCase();
    }
    OutputFormat outputFormat = OutputFormat.valueOf(outputFormatString);

    boolean useExtras = true;
    if (argsMap.containsKey(EXTRAS) && argsMap.get(EXTRAS).length > 0) {
      useExtras = Boolean.valueOf(argsMap.get(EXTRAS)[0]);
    }

    List<SemanticGraph> graphs = Generics.newArrayList();
    // TODO: allow other sources of graphs, such as dependency files
    if (argsMap.containsKey(TREE_FILE) && argsMap.get(TREE_FILE).length > 0) {
      for (String treeFile : argsMap.get(TREE_FILE)) {
        System.err.println("Loading file " + treeFile);
        MemoryTreebank treebank = new MemoryTreebank(new TreeNormalizer());
        treebank.loadPath(treeFile);
        for (Tree tree : treebank) {
          // TODO: allow other languages... this defaults to English
          SemanticGraph graph =
              SemanticGraphFactory.makeFromTree(
                  tree,
                  mode,
                  useExtras
                      ? GrammaticalStructure.Extras.MAXIMAL
                      : GrammaticalStructure.Extras.NONE,
                  true);
          graphs.add(graph);
        }
      }
    }

    if (argsMap.containsKey(CONLLU_FILE) && argsMap.get(CONLLU_FILE).length > 0) {
      CoNLLUDocumentReader reader = new CoNLLUDocumentReader();
      for (String conlluFile : argsMap.get(CONLLU_FILE)) {
        System.err.println("Loading file " + conlluFile);
        Iterator<SemanticGraph> it = reader.getIterator(IOUtils.readerFromString(conlluFile));

        while (it.hasNext()) {
          SemanticGraph graph = it.next();
          graphs.add(graph);
        }
      }
    }

    for (SemanticGraph graph : graphs) {
      SemgrexMatcher matcher = semgrex.matcher(graph);
      if (!(matcher.find())) {
        continue;
      }

      if (outputFormat == OutputFormat.LIST) {
        System.err.println("Matched graph:");
        System.err.println(graph.toString(SemanticGraph.OutputFormat.LIST));
        boolean found = true;
        while (found) {
          System.err.println(
              "Matches at: " + matcher.getMatch().value() + "-" + matcher.getMatch().index());
          List<String> nodeNames = Generics.newArrayList();
          nodeNames.addAll(matcher.getNodeNames());
          Collections.sort(nodeNames);
          for (String name : nodeNames) {
            System.err.println(
                "  "
                    + name
                    + ": "
                    + matcher.getNode(name).value()
                    + "-"
                    + matcher.getNode(name).index());
          }
          System.err.println();
          found = matcher.find();
        }
      } else if (outputFormat == OutputFormat.OFFSET) {
        if (graph.vertexListSorted().isEmpty()) {
          continue;
        }
        System.out.printf(
            "+%d %s%n",
            graph.vertexListSorted().get(0).get(CoreAnnotations.LineNumberAnnotation.class),
            argsMap.get(CONLLU_FILE)[0]);
      }
    }
  }