1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
package com.wcohen.ss.abbvGapsHmm;
import java.util.ArrayList;
import java.util.List;
import com.wcohen.ss.abbvGapsHmm.AbbvGapsHMM.*;
/**
* @author Dana Movshovitz-Attias
*/
public class AbbvGapsHmmBackwardsViterbiEvaluator
extends
AbbvGapsHmmBackwardsEvaluator {
protected Matrix3D _alpha;
protected Matrix3D _beta;
protected Matrix3D _emissions;
protected Matrix3D _states;
protected Matrix3D _prevSF;
protected Matrix3D _prevLF;
protected Matrix3D _prevSF_stringPos;
protected Matrix3D _prevLF_stringPos;
protected Double _bestProb;
protected Double _currProb;
public AbbvGapsHmmBackwardsViterbiEvaluator(AbbvGapsHMM abbvGapsHMM) {
super(abbvGapsHMM);
}
public AbbreviationAlignmentContainer<AbbvGapsHMM.Emissions, AbbvGapsHMM.States> backwardViterbiEvaluate(Acronym acronym, List<Double> transitionParams, List<Double> emissionParams){
_transitionParams = transitionParams;
_emissionParams = emissionParams;
super.evaluate(acronym);
return findMostProbablePathBackwards(acronym);
}
/* (non-Javadoc)
* @see Structures.AbbvGapsHMMEvaluator#initEvalMat()
*/
@Override
protected void initEvalMat() {
_emissions = new Matrix3D(_evalMat.dimension1(), _evalMat.dimension2(), _evalMat.dimension3());
_states = new Matrix3D(_evalMat.dimension1(), _evalMat.dimension2(), _evalMat.dimension3());
_prevSF = new Matrix3D(_evalMat.dimension1(), _evalMat.dimension2(), _evalMat.dimension3());
_prevLF = new Matrix3D(_evalMat.dimension1(), _evalMat.dimension2(), _evalMat.dimension3());
_prevSF_stringPos = new Matrix3D(_evalMat.dimension1(), _evalMat.dimension2(), _evalMat.dimension3());
_prevLF_stringPos = new Matrix3D(_evalMat.dimension1(), _evalMat.dimension2(), _evalMat.dimension3());
super.initEvalMat();
}
protected int getLegalStringPos(int strPos, String str){
int legalStrPos = strPos;
if(strPos > str.length() || strPos == -1){
legalStrPos = str.length();
}
return legalStrPos;
}
public AbbreviationAlignmentContainer<AbbvGapsHMM.Emissions, AbbvGapsHMM.States> findMostProbablePathBackwards(Acronym acronym){
int sPos = _sParam.getRangeEnd();
int lPos = _lParam.getRangeEnd();
int state = States.S.ordinal();
int stringPosS = -1;
int stringPosL = -1;
if(_evalMat.at(0, 0, States.S.ordinal()) == 0){
return null;
}
List<Emissions> emmisionsPath = new ArrayList<AbbvGapsHMM.Emissions>();
List<States> statesPath = new ArrayList<AbbvGapsHMM.States>();
List<String> lAlign = new ArrayList<String>();
List<String> sAlign = new ArrayList<String>();
Emissions[] emissionValues = Emissions.values();
States[] stateValues = States.values();
Emissions currEmission;
Integer currSF;
Integer currLF;
Integer currState;
Integer currSF_stringPos;
Integer currLF_stringPos;
while(sPos < _sParam.getEvalMatrixSize()-1 || lPos < _lParam.getEvalMatrixSize()-1){
currEmission = emissionValues[ (int) Math.round(_emissions.at(sPos, lPos, state)) ];
currSF = (int) Math.round(_prevSF.at(sPos, lPos, state));
currLF = (int) Math.round(_prevLF.at(sPos, lPos, state));
currState = (int) Math.round(_states.at(sPos, lPos, state));
currSF_stringPos = (int) Math.round(_prevSF_stringPos.at(sPos, lPos, state));
currLF_stringPos = (int) Math.round(_prevLF_stringPos.at(sPos, lPos, state));
if(stringPosS != -1 && stringPosL != -1){
stringPosS = getLegalStringPos(stringPosS, acronym._shortForm);
stringPosL = getLegalStringPos(stringPosL, acronym._longForm);
lAlign.add(acronym._longForm.substring(stringPosL, currLF_stringPos));
sAlign.add(acronym._shortForm.substring(stringPosS, currSF_stringPos));
}
emmisionsPath.add(currEmission);
statesPath.add(stateValues[currState]);
sPos = currSF;
lPos = currLF;
state = currState;
stringPosS = currSF_stringPos;
stringPosL = currLF_stringPos;
}
return new AbbreviationAlignmentContainer<AbbvGapsHMM.Emissions, AbbvGapsHMM.States>(sAlign, lAlign, emmisionsPath, statesPath, _evalMat.at(0, 0, States.S.ordinal()));
}
protected void updateLegalOutgoingEdges(
int currS, int currL, States currState,
int prevS, int prevL, States prevState,
Transitions transition, Emissions emission
){
Double prevProb = _evalMat.at(currS, currL, prevState.ordinal());
Double currProb = _evalMat.at(prevS, prevL, currState.ordinal())*_emissionParams.get(emission.ordinal())*_transitionParams.get(transition.ordinal());
if(prevProb.compareTo(currProb) < 0){
// Save best probability
_evalMat.set(currS, currL, prevState.ordinal(), currProb);
// Save emission with best probability
_emissions.set(currS, currL, prevState.ordinal(), emission.ordinal());
// Save previous location that produced best probability
_states.set(currS, currL, prevState.ordinal(), currState.ordinal());
_prevSF.set(currS, currL, prevState.ordinal(), prevS);
_prevLF.set(currS, currL, prevState.ordinal(), prevL);
_prevSF_stringPos.set(currS, currL, prevState.ordinal(), _sParam.getEvalStringPos());
_prevLF_stringPos.set(currS, currL, prevState.ordinal(), _lParam.getEvalStringPos());
}
}
}
|