use of edu.illinois.cs.cogcomp.sl.util.IFeatureVector in project cogcomp-nlp by CogComp.
the class CommaSequenceFeatureGenerator method getFeatureVector.
/**
* This function returns a feature vector \Phi(x,y) based on an instance-structure pair.
*
* @return Feature Vector \Phi(x,y), where x is the input instance and y is the output structure
*/
@Override
public IFeatureVector getFeatureVector(IInstance x, IStructure y) {
// lexicon should have been completely built while reading the problem instances itself
assert !lexicon.isAllowNewFeatures();
CommaSequence commaSequence = (CommaSequence) x;
CommaLabelSequence commaLabelSequence = (CommaLabelSequence) y;
FeatureVectorBuffer fv = new FeatureVectorBuffer();
int len = commaSequence.sortedCommas.size();
/*
* for(Comma comma : commaSequence.sortedCommas){ FeatureVector lbjFeatureVector =
* lbjExtractor.classify(comma); for(int i=0; i<lbjFeatureVector.featuresSize(); i++){
* String emittedFeatureString = lbjFeatureVector.getFeature(i).toString();
* lexicon.addFeature(emittedFeatureString);
* fv.addFeature(lexicon.getFeatureId(emittedFeatureString), 1); } }
*
* String startLabel = commaLabelSequence.commaLabels.get(0);
* lexicon.addFeature(startLabel); fv.addFeature(lexicon.getFeatureId(startLabel), 1);
*
* for(int i=1; i<commaLabelSequence.commaLabels.size(); i++){ String previousLabel =
* commaLabelSequence.commaLabels.get(i-1); String currentLabel =
* commaLabelSequence.commaLabels.get(i); String transitionFeatureString = previousLabel +
* "---" + currentLabel; lexicon.addFeature(transitionFeatureString);
* fv.addFeature(lexicon.getFeatureId(transitionFeatureString), 1); }
*/
int[] tags = commaLabelSequence.labelIds;
IFeatureVector[] baseFeatures = commaSequence.baseFeatures;
int numOfEmissionFeatures = lexicon.getNumOfFeature();
int numOfLabels = lexicon.getNumOfLabels();
// add emission features.....
for (int i = 0; i < len; i++) {
fv.addFeature(baseFeatures[i], numOfEmissionFeatures * tags[i]);
}
// add prior feature
int emissionOffset = numOfEmissionFeatures * numOfLabels;
fv.addFeature(emissionOffset + tags[0], 1.0f);
// add transition features
int priorEmissionOffset = emissionOffset + numOfLabels;
// calculate transition features
for (int i = 1; i < len; i++) {
fv.addFeature(priorEmissionOffset + (// TODO can't allow label-id of 0
tags[i - 1] * // product will be 0
numOfLabels + tags[i]), 1.0f);
}
return fv.toFeatureVector();
}
Aggregations