use of java.io.PrintWriter in project CoreNLP by stanfordnlp.
the class ArabicSegmenter method evaluate.
/**
* Evaluate accuracy when the input is gold segmented text *with* segmentation
* markers and morphological analyses. In other words, the evaluation file has the
* same format as the training data.
*
* @param pwOut
*/
private void evaluate(PrintWriter pwOut) {
log.info("Starting evaluation...");
boolean hasSegmentationMarkers = true;
boolean hasTags = true;
DocumentReaderAndWriter<CoreLabel> docReader = new ArabicDocumentReaderAndWriter(hasSegmentationMarkers, hasTags, hasDomainLabels, domain, tf);
ObjectBank<List<CoreLabel>> lines = classifier.makeObjectBankFromFile(flags.testFile, docReader);
PrintWriter tedEvalGoldTree = null, tedEvalParseTree = null;
PrintWriter tedEvalGoldSeg = null, tedEvalParseSeg = null;
if (tedEvalPrefix != null) {
try {
tedEvalGoldTree = new PrintWriter(tedEvalPrefix + "_gold.ftree");
tedEvalGoldSeg = new PrintWriter(tedEvalPrefix + "_gold.segmentation");
tedEvalParseTree = new PrintWriter(tedEvalPrefix + "_parse.ftree");
tedEvalParseSeg = new PrintWriter(tedEvalPrefix + "_parse.segmentation");
} catch (FileNotFoundException e) {
System.err.printf("%s: %s%n", ArabicSegmenter.class.getName(), e.getMessage());
}
}
Counter<String> labelTotal = new ClassicCounter<>();
Counter<String> labelCorrect = new ClassicCounter<>();
int total = 0;
int correct = 0;
for (List<CoreLabel> line : lines) {
final String[] inputTokens = tedEvalSanitize(IOBUtils.IOBToString(line).replaceAll(":", "#pm#")).split(" ");
final String[] goldTokens = tedEvalSanitize(IOBUtils.IOBToString(line, ":")).split(" ");
line = classifier.classify(line);
final String[] parseTokens = tedEvalSanitize(IOBUtils.IOBToString(line, ":")).split(" ");
for (CoreLabel label : line) {
// Do not evaluate labeling of whitespace
String observation = label.get(CoreAnnotations.CharAnnotation.class);
if (!observation.equals(IOBUtils.getBoundaryCharacter())) {
total++;
String hypothesis = label.get(CoreAnnotations.AnswerAnnotation.class);
String reference = label.get(CoreAnnotations.GoldAnswerAnnotation.class);
labelTotal.incrementCount(reference);
if (hypothesis.equals(reference)) {
correct++;
labelCorrect.incrementCount(reference);
}
}
}
if (tedEvalParseSeg != null) {
tedEvalGoldTree.printf("(root");
tedEvalParseTree.printf("(root");
int safeLength = inputTokens.length;
if (inputTokens.length != goldTokens.length) {
log.info("In generating TEDEval files: Input and gold do not have the same number of tokens");
log.info(" (ignoring any extras)");
log.info(" input: " + Arrays.toString(inputTokens));
log.info(" gold: " + Arrays.toString(goldTokens));
safeLength = Math.min(inputTokens.length, goldTokens.length);
}
if (inputTokens.length != parseTokens.length) {
log.info("In generating TEDEval files: Input and parse do not have the same number of tokens");
log.info(" (ignoring any extras)");
log.info(" input: " + Arrays.toString(inputTokens));
log.info(" parse: " + Arrays.toString(parseTokens));
safeLength = Math.min(inputTokens.length, parseTokens.length);
}
for (int i = 0; i < safeLength; i++) {
for (String segment : goldTokens[i].split(":")) tedEvalGoldTree.printf(" (seg %s)", segment);
tedEvalGoldSeg.printf("%s\t%s%n", inputTokens[i], goldTokens[i]);
for (String segment : parseTokens[i].split(":")) tedEvalParseTree.printf(" (seg %s)", segment);
tedEvalParseSeg.printf("%s\t%s%n", inputTokens[i], parseTokens[i]);
}
tedEvalGoldTree.printf(")%n");
tedEvalGoldSeg.println();
tedEvalParseTree.printf(")%n");
tedEvalParseSeg.println();
}
}
double accuracy = ((double) correct) / ((double) total);
accuracy *= 100.0;
pwOut.println("EVALUATION RESULTS");
pwOut.printf("#datums:\t%d%n", total);
pwOut.printf("#correct:\t%d%n", correct);
pwOut.printf("accuracy:\t%.2f%n", accuracy);
pwOut.println("==================");
// Output the per label accuracies
pwOut.println("PER LABEL ACCURACIES");
for (String refLabel : labelTotal.keySet()) {
double nTotal = labelTotal.getCount(refLabel);
double nCorrect = labelCorrect.getCount(refLabel);
double acc = (nCorrect / nTotal) * 100.0;
pwOut.printf(" %s\t%.2f%n", refLabel, acc);
}
if (tedEvalParseSeg != null) {
tedEvalGoldTree.close();
tedEvalGoldSeg.close();
tedEvalParseTree.close();
tedEvalParseSeg.close();
}
}
use of java.io.PrintWriter in project CoreNLP by stanfordnlp.
the class StochasticDiffFunctionTester method arrayToFile.
public void arrayToFile(double[] thisArray, String fileName) {
PrintWriter file = null;
NumberFormat nf = new DecimalFormat("0.000E0");
try {
file = new PrintWriter(new FileOutputStream(fileName), true);
} catch (IOException e) {
log.info("Caught IOException outputing List to file: " + e.getMessage());
System.exit(1);
}
for (double element : thisArray) {
file.print(nf.format(element) + " ");
}
file.close();
}
use of java.io.PrintWriter in project CoreNLP by stanfordnlp.
the class StochasticMinimizer method initFiles.
private void initFiles() {
if (outputIterationsToFile) {
String fileName = getName() + ".output";
String infoName = getName() + ".info";
try {
file = new PrintWriter(new FileOutputStream(fileName), true);
infoFile = new PrintWriter(new FileOutputStream(infoName), true);
} catch (IOException e) {
log.info("Caught IOException outputting data to file: " + e.getMessage());
System.exit(1);
}
}
}
use of java.io.PrintWriter in project CoreNLP by stanfordnlp.
the class CharniakScoredParsesReaderWriter method printScoredTrees.
/**
* Print scored parse trees in format used by charniak parser
* @param trees - trees to output
* @param filename - file to output to
*/
public void printScoredTrees(Iterable<List<ScoredObject<Tree>>> trees, String filename) {
try {
PrintWriter pw = IOUtils.getPrintWriter(filename);
int i = 0;
for (List<ScoredObject<Tree>> treeList : trees) {
printScoredTrees(pw, i, treeList);
i++;
}
pw.close();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
use of java.io.PrintWriter in project CoreNLP by stanfordnlp.
the class OpenIEServlet method doGet.
/**
* {@inheritDoc}
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (request.getCharacterEncoding() == null) {
request.setCharacterEncoding("utf-8");
}
response.setContentType("text/json; charset=UTF-8");
PrintWriter out = response.getWriter();
String raw = request.getParameter("q");
if (raw == null || "".equals(raw)) {
out.println("{ok:false, entailments:[], triples=[], msg=\"\"}");
} else {
doGet(out, raw);
}
out.close();
}
Aggregations