File size: 9,429 Bytes
6f3ebfa | 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | package org.maltparser.parser.guide.decision;
import org.maltparser.core.exception.MaltChainedException;
import org.maltparser.core.feature.FeatureModel;
import org.maltparser.core.feature.FeatureVector;
import org.maltparser.core.syntaxgraph.DependencyStructure;
import org.maltparser.parser.guide.ClassifierGuide;
import org.maltparser.parser.guide.GuideException;
import org.maltparser.parser.guide.instance.AtomicModel;
import org.maltparser.parser.guide.instance.FeatureDivideModel;
import org.maltparser.parser.guide.instance.InstanceModel;
import org.maltparser.parser.history.action.GuideDecision;
import org.maltparser.parser.history.action.MultipleDecision;
import org.maltparser.parser.history.action.SingleDecision;
import org.maltparser.parser.history.container.TableContainer.RelationToNextDecision;
/**
*
* @author Johan Hall
* @since 1.1
**/
public class SeqDecisionModel implements DecisionModel {
private final ClassifierGuide guide;
private final String modelName;
// private final FeatureModel featureModel;
private InstanceModel instanceModel;
private final int decisionIndex;
private final DecisionModel prevDecisionModel;
private DecisionModel nextDecisionModel;
private final String branchedDecisionSymbols;
public SeqDecisionModel(ClassifierGuide _guide) throws MaltChainedException {
this.guide = _guide;
this.branchedDecisionSymbols = "";
// this.featureModel = _featureModel;
this.decisionIndex = 0;
this.modelName = "sdm"+decisionIndex;
this.prevDecisionModel = null;
}
public SeqDecisionModel(ClassifierGuide _guide, DecisionModel _prevDecisionModel, String _branchedDecisionSymbol) throws MaltChainedException {
this.guide = _guide;
this.decisionIndex = _prevDecisionModel.getDecisionIndex() + 1;
if (_branchedDecisionSymbol != null && _branchedDecisionSymbol.length() > 0) {
this.branchedDecisionSymbols = _branchedDecisionSymbol;
this.modelName = "sdm"+decisionIndex+branchedDecisionSymbols;
} else {
this.branchedDecisionSymbols = "";
this.modelName = "sdm"+decisionIndex;
}
// this.featureModel = _prevDecisionModel.getFeatureModel();
this.prevDecisionModel = _prevDecisionModel;
}
// public void updateFeatureModel() throws MaltChainedException {
// featureModel.update();
// }
private void initInstanceModel(FeatureModel featureModel, String subModelName) throws MaltChainedException {
if (featureModel.hasDivideFeatureFunction()) {
instanceModel = new FeatureDivideModel(this);
} else {
instanceModel = new AtomicModel(-1, this);
}
}
public void finalizeSentence(DependencyStructure dependencyGraph) throws MaltChainedException {
if (instanceModel != null) {
instanceModel.finalizeSentence(dependencyGraph);
}
if (nextDecisionModel != null) {
nextDecisionModel.finalizeSentence(dependencyGraph);
}
}
public void noMoreInstances(FeatureModel featureModel) throws MaltChainedException {
if (guide.getGuideMode() == ClassifierGuide.GuideMode.CLASSIFY) {
throw new GuideException("The decision model could not create it's model. ");
}
if (instanceModel != null) {
instanceModel.noMoreInstances(featureModel);
instanceModel.train();
}
if (nextDecisionModel != null) {
nextDecisionModel.noMoreInstances(featureModel);
}
}
public void terminate() throws MaltChainedException {
if (instanceModel != null) {
instanceModel.terminate();
instanceModel = null;
}
if (nextDecisionModel != null) {
nextDecisionModel.terminate();
nextDecisionModel = null;
}
}
public void addInstance(FeatureModel featureModel, GuideDecision decision) throws MaltChainedException {
if (decision instanceof SingleDecision) {
throw new GuideException("A sequantial decision model expect a sequence of decisions, not a single decision. ");
}
featureModel.update();
final SingleDecision singleDecision = ((MultipleDecision)decision).getSingleDecision(decisionIndex);
if (instanceModel == null) {
initInstanceModel(featureModel, singleDecision.getTableContainer().getTableContainerName());
}
instanceModel.addInstance(featureModel.getFeatureVector(branchedDecisionSymbols, singleDecision.getTableContainer().getTableContainerName()), singleDecision);
if (singleDecision.continueWithNextDecision() && decisionIndex+1 < decision.numberOfDecisions()) {
if (nextDecisionModel == null) {
initNextDecisionModel(((MultipleDecision)decision).getSingleDecision(decisionIndex+1), branchedDecisionSymbols);
}
nextDecisionModel.addInstance(featureModel, decision);
}
}
public boolean predict(FeatureModel featureModel, GuideDecision decision) throws MaltChainedException {
if (decision instanceof SingleDecision) {
throw new GuideException("A sequantial decision model expect a sequence of decisions, not a single decision. ");
}
featureModel.update();
final SingleDecision singleDecision = ((MultipleDecision)decision).getSingleDecision(decisionIndex);
if (instanceModel == null) {
initInstanceModel(featureModel, singleDecision.getTableContainer().getTableContainerName());
}
boolean success = instanceModel.predict(featureModel.getFeatureVector(branchedDecisionSymbols, singleDecision.getTableContainer().getTableContainerName()), singleDecision);
if (singleDecision.continueWithNextDecision() && decisionIndex+1 < decision.numberOfDecisions()) {
if (nextDecisionModel == null) {
initNextDecisionModel(((MultipleDecision)decision).getSingleDecision(decisionIndex+1), branchedDecisionSymbols);
}
success = nextDecisionModel.predict(featureModel, decision) && success;
}
return success;
}
public FeatureVector predictExtract(FeatureModel featureModel, GuideDecision decision) throws MaltChainedException {
if (decision instanceof SingleDecision) {
throw new GuideException("A sequantial decision model expect a sequence of decisions, not a single decision. ");
}
featureModel.update();
final SingleDecision singleDecision = ((MultipleDecision)decision).getSingleDecision(decisionIndex);
if (instanceModel == null) {
initInstanceModel(featureModel, singleDecision.getTableContainer().getTableContainerName());
}
FeatureVector fv = instanceModel.predictExtract(featureModel.getFeatureVector(branchedDecisionSymbols, singleDecision.getTableContainer().getTableContainerName()), singleDecision);
if (singleDecision.continueWithNextDecision() && decisionIndex+1 < decision.numberOfDecisions()) {
if (nextDecisionModel == null) {
initNextDecisionModel(((MultipleDecision)decision).getSingleDecision(decisionIndex+1), branchedDecisionSymbols);
}
nextDecisionModel.predictExtract(featureModel, decision);
}
return fv;
}
public FeatureVector extract(FeatureModel featureModel) throws MaltChainedException {
featureModel.update();
return null ; //instanceModel.extract(); // TODO handle many feature vectors
}
public boolean predictFromKBestList(FeatureModel featureModel, GuideDecision decision) throws MaltChainedException {
if (decision instanceof SingleDecision) {
throw new GuideException("A sequantial decision model expect a sequence of decisions, not a single decision. ");
}
boolean success = false;
final SingleDecision singleDecision = ((MultipleDecision)decision).getSingleDecision(decisionIndex);
// TODO develop different strategies for resolving which kBestlist that should be used
if (nextDecisionModel != null && singleDecision.continueWithNextDecision()) {
success = nextDecisionModel.predictFromKBestList(featureModel, decision);
}
if (!success) {
success = singleDecision.updateFromKBestList();
if (success && singleDecision.continueWithNextDecision() && decisionIndex+1 < decision.numberOfDecisions()) {
if (nextDecisionModel == null) {
initNextDecisionModel(((MultipleDecision)decision).getSingleDecision(decisionIndex+1), branchedDecisionSymbols);
}
nextDecisionModel.predict(featureModel, decision);
}
}
return success;
}
public ClassifierGuide getGuide() {
return guide;
}
public String getModelName() {
return modelName;
}
// public FeatureModel getFeatureModel() {
// return featureModel;
// }
public int getDecisionIndex() {
return decisionIndex;
}
public DecisionModel getPrevDecisionModel() {
return prevDecisionModel;
}
public DecisionModel getNextDecisionModel() {
return nextDecisionModel;
}
private void initNextDecisionModel(SingleDecision decision, String branchedDecisionSymbol) throws MaltChainedException {
if (decision.getRelationToNextDecision() == RelationToNextDecision.SEQUANTIAL) {
this.nextDecisionModel = new SeqDecisionModel(guide, this, branchedDecisionSymbol);
} else if (decision.getRelationToNextDecision() == RelationToNextDecision.BRANCHED) {
this.nextDecisionModel = new BranchedDecisionModel(guide, this, branchedDecisionSymbol);
} else if (decision.getRelationToNextDecision() == RelationToNextDecision.NONE) {
this.nextDecisionModel = new OneDecisionModel(guide, this, branchedDecisionSymbol);
}
}
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append(modelName + ", ");
sb.append(nextDecisionModel.toString());
return sb.toString();
}
}
|