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

edu.stanford.nlp.parser.lexparser.ExactGrammarCompactor 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.lexparser; 
import edu.stanford.nlp.util.logging.Redwood;

import edu.stanford.nlp.trees.PennTreebankLanguagePack;
import edu.stanford.nlp.fsm.TransducerGraph;
import edu.stanford.nlp.fsm.QuasiDeterminizer;
import edu.stanford.nlp.fsm.AutomatonMinimizer;
import edu.stanford.nlp.fsm.FastExactAutomatonMinimizer;

import java.util.List;

/**
 * @author Teg Grenager ([email protected])
 */
public class ExactGrammarCompactor extends GrammarCompactor  {

  /** A logger for this class */
  private static Redwood.RedwoodChannels log = Redwood.channels(ExactGrammarCompactor.class);

  TransducerGraph.GraphProcessor quasiDeterminizer = new QuasiDeterminizer();
  AutomatonMinimizer minimizer = new FastExactAutomatonMinimizer();
  TransducerGraph.NodeProcessor ntsp = new TransducerGraph.SetToStringNodeProcessor(new PennTreebankLanguagePack());
  TransducerGraph.NodeProcessor otsp = new TransducerGraph.ObjectToSetNodeProcessor();
  TransducerGraph.ArcProcessor isp = new TransducerGraph.InputSplittingProcessor();
  TransducerGraph.ArcProcessor ocp = new TransducerGraph.OutputCombiningProcessor();
  private boolean saveGraphs; // = false;

  public ExactGrammarCompactor(Options op, 
                               boolean saveGraphs, boolean verbose) {
    super(op);
    this.saveGraphs = saveGraphs;
    this.verbose = verbose;
    outputType = NORMALIZED_LOG_PROBABILITIES;
  }

  @Override
  protected TransducerGraph doCompaction(TransducerGraph graph, List l1, List l3) {
    TransducerGraph result = graph;
    if (saveGraphs) {
      writeFile(result, "unminimized", (String) result.getEndNodes().iterator().next());
    }
    result = quasiDeterminizer.processGraph(result);
    result = new TransducerGraph(result, ocp); // combine outputs into inputs
    result = minimizer.minimizeFA(result); // minimize the thing
    //result = new  TransducerGraph(graph, otsp); // for debugging
    result = new TransducerGraph(result, ntsp);  // pull out strings from sets returned by minimizer
    result = new TransducerGraph(result, isp); // split outputs from inputs
    if (saveGraphs) {
      writeFile(result, "exactminimized", (String) result.getEndNodes().iterator().next());
    }
    // for debugging do comparison of the paths accepted by graph and result
    //log.info(TransducerGraph.testGraphPaths(graph, result, 100));
    return result;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy