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

edu.stanford.nlp.parser.shiftreduce.TreeRecorder Maven / Gradle / Ivy

Go to download

Stanford CoreNLP provides a set of natural language analysis tools which can take raw English language text input and give the base forms of words, their parts of speech, whether they are names of companies, people, etc., normalize dates, times, and numeric quantities, mark up the structure of sentences in terms of phrases and word dependencies, and indicate which noun phrases refer to the same entities. It provides the foundational building blocks for higher level text understanding applications.

There is a newer version: 4.5.7
Show newest version
package edu.stanford.nlp.parser.shiftreduce;

import edu.stanford.nlp.io.RuntimeIOException;
import edu.stanford.nlp.parser.common.ParserQuery;
import edu.stanford.nlp.parser.metrics.ParserQueryEval;
import edu.stanford.nlp.trees.Tree;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Outputs either binarized or debinarized trees to a given filename.
 * Useful for seeing the intermediate results of the ShiftReduceParser
 *
 * @author John Bauer
 */
public class TreeRecorder implements ParserQueryEval {
  public enum Mode {
    BINARIZED, DEBINARIZED
  };

  private final Mode mode;

  private final BufferedWriter out;

  public TreeRecorder(Mode mode, String filename) {
    this.mode = mode;
    try {
      out = new BufferedWriter(new FileWriter(filename));
    } catch (IOException e) {
      throw new RuntimeIOException(e);
    }
  }

  public void evaluate(ParserQuery query, Tree gold, PrintWriter pw) {
    if (!(query instanceof ShiftReduceParserQuery)) {
      throw new IllegalArgumentException("This evaluator only works for the ShiftReduceParser");
    }
    
    ShiftReduceParserQuery srquery = (ShiftReduceParserQuery) query;
    try {
      switch (mode) {
      case BINARIZED:
        out.write(srquery.getBestBinarizedParse().toString());
        break;
      case DEBINARIZED:
        out.write(srquery.debinarized.toString());
        break;
      default:
        throw new IllegalArgumentException("Unknown mode " + mode);
      }
      out.newLine();
    } catch (IOException e) {
      throw new RuntimeIOException(e);
    }
  }

  public void display(boolean verbose, PrintWriter pw) {
    try {
      out.close();
    } catch (IOException e) {
      throw new RuntimeIOException(e);
    }
  }
  
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy