![JAR search and dependency download from the Maven repository](/logo.png)
edu.berkeley.nlp.crf.ScoreCalculator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of berkeleyparser Show documentation
Show all versions of berkeleyparser Show documentation
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.classify.IndexLinearizer;
import edu.berkeley.nlp.util.ArrayUtil;
import edu.berkeley.nlp.util.Counter;
public class ScoreCalculator implements Serializable {
/**
*
*/
private static final long serialVersionUID = 6864706229279071608L;
private final Encoding encoding;
private final FeatureExtractor vertexExtractor;
private final FeatureExtractor edgeExtractor;
private final IndexLinearizer il;
public ScoreCalculator(Encoding encoding, FeatureExtractor vertexExtractor,
FeatureExtractor edgeExtractor) {
this.encoding = encoding;
this.vertexExtractor = vertexExtractor;
this.edgeExtractor = edgeExtractor;
this.il = new IndexLinearizer(encoding.getNumFeatures(), encoding.getNumLabels());
}
public double[][] getScoreMatrix(InstanceSequence sequence, int index, double[] w) {
double[][] M = getLinearScoreMatrix(sequence, index, w);
for (int i=0; i sequence, int index, double[] w) {
return ArrayUtil.exp(getLinearVertexScores(sequence, index, w));
}
public double[][] getLinearScoreMatrix(InstanceSequence sequence, int index, double[] w) {
int numLabels = encoding.getNumLabels();
double[][] M = new double[numLabels][numLabels];
Counter vertexFeatures = vertexExtractor.extractFeatures(sequence.getVertexInstance(index));
for (int vc = 0; vc edgeFeatures = edgeExtractor.extractFeatures(sequence.getEdgeInstance(index, previousLabel));
double edgeScore = dotProduct(edgeFeatures, vc, w);
M[vp][vc] = vertexScore + edgeScore;
}
}
return M;
}
public double[] getLinearVertexScores(InstanceSequence sequence, int index, double[] w) {
int numLabels = encoding.getNumLabels();
double[] s = new double[numLabels];
Counter vertexFeatures = vertexExtractor.extractFeatures(sequence.getVertexInstance(index));
for (int vc = 0; vc features, int labelIndex, double[] w) {
double val = 0.0;
for (F feature : features.keySet()) {
if (encoding.hasFeature(feature)) {
int featureIndex = encoding.getFeatureIndex(feature);
int linearIndex = il.getLinearIndex(featureIndex, labelIndex);
val += features.getCount(feature) * w[linearIndex];
}
}
return val;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy