Search in sources :

Example 1 with TeeStream

use of edu.stanford.nlp.io.TeeStream in project CoreNLP by stanfordnlp.

the class BuildLexicalizedParserITest method buildAndTest.

public static void buildAndTest(ParserTestCase test) throws IOException {
    PrintStream originalOut = System.out;
    PrintStream originalErr = System.err;
    System.out.println("Training:");
    System.out.println(StringUtils.join(test.trainCommandLine));
    ByteArrayOutputStream savedOutput = new ByteArrayOutputStream();
    TeeStream teeOut = new TeeStream(savedOutput, System.out);
    PrintStream teeOutPS = new PrintStream(teeOut);
    TeeStream teeErr = new TeeStream(savedOutput, System.err);
    PrintStream teeErrPS = new PrintStream(teeErr);
    System.setOut(teeOutPS);
    System.setErr(teeErrPS);
    LexicalizedParser.main(test.trainCommandLine);
    teeOutPS.flush();
    teeErrPS.flush();
    teeOut.flush();
    teeErr.flush();
    String[] outputLines = savedOutput.toString().split("(?:\\n|\\r)+");
    String perfLine = outputLines[outputLines.length - 5];
    System.out.println(perfLine);
    assertEquals("factor LP/LR summary evalb: LP: 100.0 LR: 100.0 F1: 100.0 Exact: 100.0 N: 1", perfLine.trim());
    Formatter commandLineFormatter = new Formatter();
    commandLineFormatter.format(baseTestSerCommandLine, test.parserFile.getPath(), test.testPath);
    String[] testCommandLine = commandLineFormatter.toString().split("\\s");
    System.out.println("Testing:");
    System.out.println(StringUtils.join(testCommandLine));
    LexicalizedParser.main(testCommandLine);
    commandLineFormatter = new Formatter();
    commandLineFormatter.format(baseTestTextCommandLine, test.textFile.getPath(), test.testPath);
    testCommandLine = commandLineFormatter.toString().split("\\s");
    System.out.println("Testing:");
    System.out.println(StringUtils.join(testCommandLine));
    LexicalizedParser.main(testCommandLine);
    teeOutPS.flush();
    teeErrPS.flush();
    teeOut.flush();
    teeErr.flush();
    System.setOut(originalOut);
    System.setErr(originalErr);
}
Also used : PrintStream(java.io.PrintStream) Formatter(java.util.Formatter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TeeStream(edu.stanford.nlp.io.TeeStream)

Example 2 with TeeStream

use of edu.stanford.nlp.io.TeeStream in project CoreNLP by stanfordnlp.

the class SUTimeMain method processTempEval2.

public static void processTempEval2(AnnotationPipeline pipeline, String in, String out, String eval, String dct) throws IOException, ParseException {
    Map<String, String> docDates = (dct != null) ? IOUtils.readMap(dct) : IOUtils.readMap(in + "/dct.txt");
    if (requiredDocDateFormat != null) {
        // convert from yyyyMMdd to requiredDocDateFormat
        DateFormat defaultFormatter = new SimpleDateFormat("yyyyMMdd");
        DateFormat requiredFormatter = new SimpleDateFormat(requiredDocDateFormat);
        for (Map.Entry<String, String> docDateEntry : docDates.entrySet()) {
            Date date = defaultFormatter.parse(docDateEntry.getValue());
            docDates.put(docDateEntry.getKey(), requiredFormatter.format(date));
        }
    }
    processTempEval2Tab(pipeline, in, out, docDates);
    if (eval != null) {
        List<String> command = new ArrayList<>();
        if (PYTHON != null) {
            command.add(PYTHON);
        }
        command.add(eval);
        command.add(in + "/base-segmentation.tab");
        command.add(in + "/timex-extents.tab");
        command.add(out + "/timex-extents.res.tab");
        command.add(in + "/timex-attributes.tab");
        command.add(out + "/timex-attrs.res.tab");
        ProcessBuilder pb = new ProcessBuilder(command);
        FileOutputStream evalFileOutput = new FileOutputStream(out + "/scores.txt");
        Writer output = new OutputStreamWriter(new TeeStream(System.out, evalFileOutput));
        SystemUtils.run(pb, output, null);
        evalFileOutput.close();
    }
}
Also used : TeeStream(edu.stanford.nlp.io.TeeStream) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

TeeStream (edu.stanford.nlp.io.TeeStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Formatter (java.util.Formatter)1