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

edu.stanford.nlp.parser.lexparser.RerankingParserQuery Maven / Gradle / Ivy

Go to download

Stanford Parser processes raw text in English, Chinese, German, Arabic, and French, and extracts constituency parse trees.

There is a newer version: 3.9.2
Show newest version
package edu.stanford.nlp.parser.lexparser;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import edu.stanford.nlp.ling.HasWord;
import edu.stanford.nlp.parser.KBestViterbiParser;
import edu.stanford.nlp.parser.common.NoSuchParseException;
import edu.stanford.nlp.parser.common.ParserConstraint;
import edu.stanford.nlp.parser.common.ParserQuery;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.util.Generics;
import edu.stanford.nlp.util.ScoredComparator;
import edu.stanford.nlp.util.ScoredObject;

/**
 * Rerank trees from the ParserQuery based on scores from a Reranker.
 * 
* TODO: should handle Factored parsers as well * * @author John Bauer */ public class RerankingParserQuery implements ParserQuery { private final Options op; private final ParserQuery parserQuery; private final Reranker reranker; private final int rerankerKBest; private List> scoredTrees; /** * Data for this particular query stored by the Reranker will be * stored in this object */ private RerankerQuery rerankerQuery; public RerankingParserQuery(Options op, ParserQuery parserQuery, Reranker reranker) { this.op = op; this.parserQuery = parserQuery; this.reranker = reranker; this.rerankerKBest = op.rerankerKBest; } @Override public boolean saidMemMessage() { return parserQuery.saidMemMessage(); } @Override public void setConstraints(List constraints) { parserQuery.setConstraints(constraints); } @Override public boolean parse(List sentence) { boolean success = parserQuery.parse(sentence); if (!success) { return false; } List> bestKParses = parserQuery.getKBestPCFGParses(rerankerKBest); if (bestKParses.isEmpty()) { return false; } scoredTrees = rerank(sentence, bestKParses); return true; } @Override public boolean parseAndReport(List sentence, PrintWriter pwErr) { boolean success = parserQuery.parseAndReport(sentence, pwErr); if (!success) { return false; } List> bestKParses = parserQuery.getKBestPCFGParses(rerankerKBest); if (bestKParses.isEmpty()) { return false; } scoredTrees = rerank(sentence, bestKParses); return true; } List> rerank(List sentence, List> bestKParses) { this.rerankerQuery = reranker.process(sentence); List> reranked = new ArrayList>(); for (ScoredObject scoredTree : bestKParses) { double score = scoredTree.score(); try { score = op.baseParserWeight * score + rerankerQuery.score(scoredTree.object()); } catch (NoSuchParseException e) { score = Double.NEGATIVE_INFINITY; } reranked.add(new ScoredObject(scoredTree.object(), score)); } Collections.sort(reranked, ScoredComparator.DESCENDING_COMPARATOR); return reranked; } @Override public Tree getBestParse() { if (scoredTrees == null || scoredTrees.isEmpty()) { return null; } return scoredTrees.get(0).object(); } @Override public Tree getBestPCFGParse() { return getBestParse(); } @Override public double getPCFGScore() { if (scoredTrees == null || scoredTrees.isEmpty()) { throw new AssertionError(); } return scoredTrees.get(0).score(); } @Override public Tree getBestDependencyParse(boolean debinarize) { // TODO: barf? return null; } @Override public Tree getBestFactoredParse() { // TODO: barf? return null; } @Override public List> getBestPCFGParses() { if (scoredTrees == null || scoredTrees.isEmpty()) { throw new AssertionError(); } List> equalTrees = Generics.newArrayList(); double score = scoredTrees.get(0).score(); int treePos = 0; while (treePos < scoredTrees.size() && scoredTrees.get(treePos).score() == score) { equalTrees.add(scoredTrees.get(treePos)); } return equalTrees; } @Override public void restoreOriginalWords(Tree tree) { parserQuery.restoreOriginalWords(tree); } @Override public boolean hasFactoredParse() { return false; } @Override public List> getKBestPCFGParses(int kbestPCFG) { List> trees = Generics.newArrayList(); for (int treePos = 0; treePos < scoredTrees.size() && treePos < kbestPCFG; ++treePos) { trees.add(scoredTrees.get(treePos)); } return trees; } @Override public List> getKGoodFactoredParses(int kbest) { // TODO: barf? return null; } @Override public KBestViterbiParser getPCFGParser() { return null; } @Override public KBestViterbiParser getFactoredParser() { return null; } @Override public KBestViterbiParser getDependencyParser() { return null; } /** * Parsing succeeded without any horrible errors or fallback */ @Override public boolean parseSucceeded() { return parserQuery.parseSucceeded(); } /** * The sentence was skipped, probably because it was too long or of length 0 */ @Override public boolean parseSkipped() { return parserQuery.parseSkipped(); } /** * The model had to fall back to a simpler model on the previous parse */ @Override public boolean parseFallback() { return parserQuery.parseFallback(); } /** * The model ran out of memory on the most recent parse */ @Override public boolean parseNoMemory() { return parserQuery.parseNoMemory(); } /** * The model could not parse the most recent sentence for some reason */ @Override public boolean parseUnparsable() { return parserQuery.parseUnparsable(); } @Override public List originalSentence() { return parserQuery.originalSentence(); } public RerankerQuery rerankerQuery() { return rerankerQuery; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy