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

edu.berkeley.nlp.crf.Inference Maven / Gradle / Ivy

Go to download

The Berkeley parser analyzes the grammatical structure of natural language using probabilistic context-free grammars (PCFGs).

The newest version!
package edu.berkeley.nlp.crf;

import java.io.Serializable;

import edu.berkeley.nlp.classify.Encoding;
import edu.berkeley.nlp.classify.FeatureExtractor;
import edu.berkeley.nlp.math.DoubleArrays;
import edu.berkeley.nlp.math.DoubleMatrices;
import edu.berkeley.nlp.util.ArrayUtil;
import edu.berkeley.nlp.util.Pair;
import edu.berkeley.nlp.util.PriorityQueue;

public class Inference implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1948395432745606240L;
	
	private final Encoding encoding;
	private final ScoreCalculator scoreCalculator;
	
	public Inference(Encoding encoding,
			FeatureExtractor vertexExtractor, FeatureExtractor edgeExtractor) {
		this.encoding = encoding;
		this.scoreCalculator = new ScoreCalculator(encoding, vertexExtractor, edgeExtractor);
	}
	
	public double[][] getAlphas(InstanceSequence sequence, double[] w) {
		int n = sequence.getSequenceLength();
		double[][] alpha = new double[n][];
		alpha[0] = scoreCalculator.getVertexScores(sequence, 0, w);
		for (int i=1; i sequence, double[] w) {
		int n = sequence.getSequenceLength();
		double[][] beta = new double[n][];
		beta[n-1] = DoubleArrays.constantArray(1.0, encoding.getNumLabels());
		for (int i=n-2; i>=0; i--) {
			double[][] scoreMatrix = scoreCalculator.getScoreMatrix(sequence, i+1, w);
			beta[i] = DoubleMatrices.product(scoreMatrix, beta[i+1]);
		}
		return beta;
	}
	
	public Pair getKBestChartAndBacktrace(InstanceSequence sequence, double[] w, int k) {
		int n = sequence.getSequenceLength();
		int numLabels = encoding.getNumLabels();
		int[][][][] bestLabels = new int[n][numLabels][][];
		double[][][] bestScores = new double[n][numLabels][];
		double[] startScores = scoreCalculator.getLinearVertexScores(sequence, 0, w);
		for (int l=0; l> pq = new PriorityQueue>();
				for (int pl=0; pl backtrace = pq.next();
					bestLabels[i][l][c][0] = backtrace.getFirst();
					bestLabels[i][l][c][1] = backtrace.getSecond();
				}
			}
		}
		return Pair.makePair(bestLabels, bestScores);
	}
	
	public double[][] getVertexPosteriors(double[][] alpha, double[][] beta) {
		double[][] p = new double[alpha.length][encoding.getNumLabels()];
		for (int i=0; i sequence, double[] w, double[][] alpha, double[][] beta) {
		int numLabels = encoding.getNumLabels();
		int n = sequence.getSequenceLength();
		double[][][] p = new double[n][numLabels][numLabels];
		for (int i=1; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy