List of usage examples for edu.stanford.nlp.io IOUtils getPrintWriter
public static PrintWriter getPrintWriter(String filename) throws IOException
From source file:ilcc.ccgparser.nnparser.IncNNParser.java
public void writeModelFile(String modelFile) { try {//from www. j a v a 2 s. c o m double[][] W1 = classifier.getW1(); double[] b1 = classifier.getb1(); double[][] W2 = classifier.getW2(); double[][] E = classifier.getE(); Writer output = IOUtils.getPrintWriter(modelFile); HashMap<String, ArrayList<CCGJRuleInfo>> uRules = srparser.treebankRules.getUnaryRules(); HashMap<String, ArrayList<CCGJRuleInfo>> bRules = srparser.treebankRules.getBinaryRules(); HashMap<String, ArrayList<CCGJRuleInfo>> rRules = srparser.treebankRules.getRevealRules(); output.write("dict=" + knownWords.size() + "\n"); output.write("pos=" + knownPos.size() + "\n"); output.write("ccg cats=" + knownCCGCats.size() + "\n"); output.write("embeddingSize=" + E[0].length + "\n"); output.write("hiddenSize=" + b1.length + "\n"); output.write("numTokens=" + (W1[0].length / E[0].length) + "\n"); output.write("preComputed=" + preComputed.size() + "\n"); output.write("classes=" + actsMap.size() + "\n"); output.write("UnaryRules=" + uRules.size() + "\n"); output.write("BinaryRules=" + bRules.size() + "\n"); output.write("RevealRules=" + rRules.size() + "\n"); int index = 0; // Classes for (ArcJAction act : actsList) output.write(act.toString() + "\n"); // Unary and Binary Rules for (String key : uRules.keySet()) { ArrayList<CCGJRuleInfo> list = uRules.get(key); output.write(key); for (CCGJRuleInfo info : list) { output.write(" " + info.toString()); } output.write("\n"); } for (String key : bRules.keySet()) { ArrayList<CCGJRuleInfo> list = bRules.get(key); output.write(key); for (CCGJRuleInfo info : list) { output.write(" " + info.toString()); } output.write("\n"); } for (String key : rRules.keySet()) { ArrayList<CCGJRuleInfo> list = rRules.get(key); output.write(key); for (CCGJRuleInfo info : list) { output.write(" " + info.toString()); } output.write("\n"); } // First write word / POS / label embeddings for (String word : knownWords) { output.write(word); for (int k = 0; k < E[index].length; ++k) output.write(" " + E[index][k]); output.write("\n"); index = index + 1; } for (String pos : knownPos) { output.write(pos); for (int k = 0; k < E[index].length; ++k) output.write(" " + E[index][k]); output.write("\n"); index = index + 1; } for (String label : knownCCGCats) { output.write(label); for (int k = 0; k < E[index].length; ++k) output.write(" " + E[index][k]); output.write("\n"); index = index + 1; } // Now write classifier weights for (int j = 0; j < W1[0].length; ++j) for (int i = 0; i < W1.length; ++i) { output.write("" + W1[i][j]); if (i == W1.length - 1) output.write("\n"); else output.write(" "); } for (int i = 0; i < b1.length; ++i) { output.write("" + b1[i]); if (i == b1.length - 1) output.write("\n"); else output.write(" "); } for (int j = 0; j < W2[0].length; ++j) for (int i = 0; i < W2.length; ++i) { output.write("" + W2[i][j]); if (i == W2.length - 1) output.write("\n"); else output.write(" "); } // Finish with pre-computation info for (int i = 0; i < preComputed.size(); ++i) { output.write("" + preComputed.get(i)); if ((i + 1) % 100 == 0 || i == preComputed.size() - 1) output.write("\n"); else output.write(" "); } output.close(); } catch (IOException e) { throw new RuntimeIOException(e); } }
From source file:knu.univ.lingvo.coref.SieveCoreferenceSystem.java
License:Open Source License
public static double runAndScoreCoref(SieveCoreferenceSystem corefSystem, MentionExtractor mentionExtractor, Properties props, String timeStamp) throws Exception { // prepare conll output PrintWriter writerGold = null; PrintWriter writerPredicted = null; PrintWriter writerPredictedCoref = null; String conllOutputMentionGoldFile = null; String conllOutputMentionPredictedFile = null; String conllOutputMentionCorefPredictedFile = null; String conllMentionEvalFile = null; String conllMentionEvalErrFile = null; String conllMentionCorefEvalFile = null; String conllMentionCorefEvalErrFile = null; if (Constants.PRINT_CONLL_OUTPUT || corefSystem.replicateCoNLL) { String conllOutput = props.getProperty(Constants.CONLL_OUTPUT_PROP, "conlloutput"); conllOutputMentionGoldFile = conllOutput + "-" + timeStamp + ".gold.txt"; conllOutputMentionPredictedFile = conllOutput + "-" + timeStamp + ".predicted.txt"; conllOutputMentionCorefPredictedFile = conllOutput + "-" + timeStamp + ".coref.predicted.txt"; conllMentionEvalFile = conllOutput + "-" + timeStamp + ".eval.txt"; conllMentionEvalErrFile = conllOutput + "-" + timeStamp + ".eval.err.txt"; conllMentionCorefEvalFile = conllOutput + "-" + timeStamp + ".coref.eval.txt"; conllMentionCorefEvalErrFile = conllOutput + "-" + timeStamp + ".coref.eval.err.txt"; logger.info("CONLL MENTION GOLD FILE: " + conllOutputMentionGoldFile); logger.info("CONLL MENTION PREDICTED FILE: " + conllOutputMentionPredictedFile); logger.info("CONLL MENTION EVAL FILE: " + conllMentionEvalFile); if (!Constants.SKIP_COREF) { logger.info("CONLL MENTION PREDICTED WITH COREF FILE: " + conllOutputMentionCorefPredictedFile); logger.info("CONLL MENTION WITH COREF EVAL FILE: " + conllMentionCorefEvalFile); }// ww w .ja v a 2s .c o m writerGold = new PrintWriter(new FileOutputStream(conllOutputMentionGoldFile)); writerPredicted = new PrintWriter(new FileOutputStream(conllOutputMentionPredictedFile)); writerPredictedCoref = new PrintWriter(new FileOutputStream(conllOutputMentionCorefPredictedFile)); } mentionExtractor.resetDocs(); if (corefSystem.doScore()) { corefSystem.initScorers(); } // // Parse one document at a time, and do single-doc coreference resolution in each. // // In one iteration, orderedMentionsBySentence contains a list of all // mentions in one document. Each mention has properties (annotations): // its surface form (Word), NER Tag, POS Tag, Index, etc. // while (true) { Document document = mentionExtractor.nextDoc(); if (document == null) { break; } if (!props.containsKey(Constants.MUC_PROP)) { printRawDoc(document, true); printRawDoc(document, false); } printDiscourseStructure(document); if (corefSystem.doScore()) { document.extractGoldCorefClusters(); } if (Constants.PRINT_CONLL_OUTPUT || corefSystem.replicateCoNLL) { // Not doing coref - print conll output here printConllOutput(document, writerGold, true); printConllOutput(document, writerPredicted, false); } // run mention detection only if (Constants.SKIP_COREF) { continue; } corefSystem.coref(document); // Do Coreference Resolution if (corefSystem.doScore()) { //Identifying possible coreferring mentions in the corpus along with any recall/precision errors with gold corpus corefSystem.printTopK(logger, document, corefSystem.semantics); logger.fine("pairwise score for this doc: "); corefSystem.scoreSingleDoc.get(corefSystem.sieves.length - 1).printF1(logger); logger.fine("accumulated score: "); corefSystem.printF1(true); logger.fine("\n"); } if (Constants.PRINT_CONLL_OUTPUT || corefSystem.replicateCoNLL) { printConllOutput(document, writerPredictedCoref, false, true); } } double finalScore = 0; if (Constants.PRINT_CONLL_OUTPUT || corefSystem.replicateCoNLL) { writerGold.close(); writerPredicted.close(); writerPredictedCoref.close(); //if(props.containsKey(Constants.CONLL_SCORER)) { if (corefSystem.conllMentionEvalScript != null) { // runConllEval(corefSystem.conllMentionEvalScript, conllOutputMentionGoldFile, conllOutputMentionPredictedFile, conllMentionEvalFile, conllMentionEvalErrFile); String summary = getConllEvalSummary(corefSystem.conllMentionEvalScript, conllOutputMentionGoldFile, conllOutputMentionPredictedFile); logger.info("\nCONLL EVAL SUMMARY (Before COREF)"); printScoreSummary(summary, logger, false); if (!Constants.SKIP_COREF) { // runConllEval(corefSystem.conllMentionEvalScript, conllOutputMentionGoldFile, conllOutputMentionCorefPredictedFile, conllMentionCorefEvalFile, conllMentionCorefEvalErrFile); summary = getConllEvalSummary(corefSystem.conllMentionEvalScript, conllOutputMentionGoldFile, conllOutputMentionCorefPredictedFile); logger.info("\nCONLL EVAL SUMMARY (After COREF)"); printScoreSummary(summary, logger, true); finalScore = printFinalConllScore(summary); if (false || corefSystem.optimizeConllScore) { //finalScore = getFinalConllScore(summary, corefSystem.optimizeMetricType, corefSystem.optimizeSubScoreType.toString()); } } } } if (true || !corefSystem.optimizeConllScore && corefSystem.doScore()) { //finalScore = corefSystem.getFinalScore(corefSystem.optimizeMetricType, corefSystem.optimizeSubScoreType); } String scoresFile = props.getProperty(Constants.SCORE_FILE_PROP); if (scoresFile != null) { PrintWriter pw = IOUtils.getPrintWriter(scoresFile); pw.println((new DecimalFormat("#.##")).format(finalScore)); pw.close(); } if (corefSystem.optimizeSieves) { logger.info("Final reported score for sieve optimization " + corefSystem.optimizeScoreType + " : " + finalScore); } return finalScore; }
From source file:knu.univ.lingvo.coref.SieveCoreferenceSystem.java
License:Open Source License
/** * Run and score coref distributed/*ww w.j a v a 2 s. co m*/ */ public static void runAndScoreCorefDist(String runDistCmd, Properties props, String propsFile) throws Exception { PrintWriter pw = IOUtils.getPrintWriter(propsFile); props.store(pw, null); pw.close(); /* Run coref job in a distributed manner, score is written to file */ List<String> cmd = new ArrayList<String>(); cmd.addAll(Arrays.asList(runDistCmd.split("\\s+"))); cmd.add("-props"); cmd.add(propsFile); ProcessBuilder pb = new ProcessBuilder(cmd); // Copy environment variables over Map<String, String> curEnv = System.getenv(); Map<String, String> pbEnv = pb.environment(); pbEnv.putAll(curEnv); logger.info("Running distributed coref:" + StringUtils.join(pb.command(), " ")); StringWriter outSos = new StringWriter(); StringWriter errSos = new StringWriter(); PrintWriter out = new PrintWriter(new BufferedWriter(outSos)); PrintWriter err = new PrintWriter(new BufferedWriter(errSos)); SystemUtils.run(pb, out, err); out.close(); err.close(); String outStr = outSos.toString(); String errStr = errSos.toString(); logger.info("Finished distributed coref: " + runDistCmd + ", props=" + propsFile); logger.info("Output: " + outStr); if (errStr.length() > 0) { logger.info("Error: " + errStr); } }