File size: 10,408 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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | 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.helper.HashMap;
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
**/
public class BranchedDecisionModel implements DecisionModel {
private final ClassifierGuide guide;
private final String modelName;
// private final FeatureModel featureModel;
private InstanceModel instanceModel;
private final int decisionIndex;
private final DecisionModel parentDecisionModel;
private final HashMap<Integer,DecisionModel> children;
private final String branchedDecisionSymbols;
public BranchedDecisionModel(ClassifierGuide _guide) throws MaltChainedException {
this.guide = _guide;
this.branchedDecisionSymbols = "";
// this.featureModel = _featureModel;
this.decisionIndex = 0;
this.modelName = "bdm0";
this.parentDecisionModel = null;
this.children = new HashMap<Integer,DecisionModel>();
}
public BranchedDecisionModel(ClassifierGuide _guide, DecisionModel _parentDecisionModel, String _branchedDecisionSymbol) throws MaltChainedException {
this.guide = _guide;
this.parentDecisionModel =_parentDecisionModel;
this.decisionIndex = parentDecisionModel.getDecisionIndex() + 1;
if (_branchedDecisionSymbol != null && _branchedDecisionSymbol.length() > 0) {
this.branchedDecisionSymbols = _branchedDecisionSymbol;
this.modelName = "bdm"+decisionIndex+branchedDecisionSymbols;
} else {
this.branchedDecisionSymbols = "";
this.modelName = "bdm"+decisionIndex;
}
// this.featureModel = parentDecisionModel.getFeatureModel();
this.children = new HashMap<Integer,DecisionModel>();
}
private void initInstanceModel(FeatureModel featureModel, String subModelName) throws MaltChainedException {
if (featureModel.hasDivideFeatureFunction()) {
instanceModel = new FeatureDivideModel(this);
} else {
instanceModel = new AtomicModel(-1, this);
}
}
// public void updateFeatureModel() throws MaltChainedException {
// featureModel.update();
// }
public void finalizeSentence(DependencyStructure dependencyGraph) throws MaltChainedException {
if (instanceModel != null) {
instanceModel.finalizeSentence(dependencyGraph);
}
if (children != null) {
for (DecisionModel child : children.values()) {
child.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 (children != null) {
for (DecisionModel child : children.values()) {
child.noMoreInstances(featureModel);
}
}
}
public void terminate() throws MaltChainedException {
if (instanceModel != null) {
instanceModel.terminate();
instanceModel = null;
}
if (children != null) {
for (DecisionModel child : children.values()) {
child.terminate();
}
}
}
public void addInstance(FeatureModel featureModel, GuideDecision decision) throws MaltChainedException {
if (decision instanceof SingleDecision) {
throw new GuideException("A branched decision model expect more than one decisions. ");
}
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 (decisionIndex+1 < decision.numberOfDecisions()) {
if (singleDecision.continueWithNextDecision()) {
DecisionModel child = children.get(singleDecision.getDecisionCode());
if (child == null) {
child = initChildDecisionModel(((MultipleDecision)decision).getSingleDecision(decisionIndex+1),
branchedDecisionSymbols+(branchedDecisionSymbols.length() == 0?"":"_")+singleDecision.getDecisionSymbol());
children.put(singleDecision.getDecisionCode(), child);
}
child.addInstance(featureModel,decision);
}
}
}
public boolean predict(FeatureModel featureModel, GuideDecision decision) throws MaltChainedException {
featureModel.update();
final SingleDecision singleDecision = ((MultipleDecision)decision).getSingleDecision(decisionIndex);
if (instanceModel == null) {
initInstanceModel(featureModel, singleDecision.getTableContainer().getTableContainerName());
}
instanceModel.predict(featureModel.getFeatureVector(branchedDecisionSymbols, singleDecision.getTableContainer().getTableContainerName()), singleDecision);
if (decisionIndex+1 < decision.numberOfDecisions()) {
if (singleDecision.continueWithNextDecision()) {
DecisionModel child = children.get(singleDecision.getDecisionCode());
if (child == null) {
child = initChildDecisionModel(((MultipleDecision)decision).getSingleDecision(decisionIndex+1),
branchedDecisionSymbols+(branchedDecisionSymbols.length() == 0?"":"_")+singleDecision.getDecisionSymbol());
children.put(singleDecision.getDecisionCode(), child);
}
child.predict(featureModel, decision);
}
}
return true;
}
public FeatureVector predictExtract(FeatureModel featureModel, GuideDecision decision) throws MaltChainedException {
if (decision instanceof SingleDecision) {
throw new GuideException("A branched decision model expect more than one decisions. ");
}
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 (decisionIndex+1 < decision.numberOfDecisions()) {
if (singleDecision.continueWithNextDecision()) {
DecisionModel child = children.get(singleDecision.getDecisionCode());
if (child == null) {
child = initChildDecisionModel(((MultipleDecision)decision).getSingleDecision(decisionIndex+1),
branchedDecisionSymbols+(branchedDecisionSymbols.length() == 0?"":"_")+singleDecision.getDecisionSymbol());
children.put(singleDecision.getDecisionCode(), child);
}
child.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 branched decision model expect more than one decisions. ");
}
boolean success = false;
final SingleDecision singleDecision = ((MultipleDecision)decision).getSingleDecision(decisionIndex);
if (decisionIndex+1 < decision.numberOfDecisions()) {
if (singleDecision.continueWithNextDecision()) {
DecisionModel child = children.get(singleDecision.getDecisionCode());
if (child != null) {
success = child.predictFromKBestList(featureModel, decision);
}
}
}
if (!success) {
success = singleDecision.updateFromKBestList();
if (decisionIndex+1 < decision.numberOfDecisions()) {
if (singleDecision.continueWithNextDecision()) {
DecisionModel child = children.get(singleDecision.getDecisionCode());
if (child == null) {
child = initChildDecisionModel(((MultipleDecision)decision).getSingleDecision(decisionIndex+1),
branchedDecisionSymbols+(branchedDecisionSymbols.length() == 0?"":"_")+singleDecision.getDecisionSymbol());
children.put(singleDecision.getDecisionCode(), child);
}
child.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 getParentDecisionModel() {
return parentDecisionModel;
}
private DecisionModel initChildDecisionModel(SingleDecision decision, String branchedDecisionSymbol) throws MaltChainedException {
if (decision.getRelationToNextDecision() == RelationToNextDecision.SEQUANTIAL) {
return new SeqDecisionModel(guide, this, branchedDecisionSymbol);
} else if (decision.getRelationToNextDecision() == RelationToNextDecision.BRANCHED) {
return new BranchedDecisionModel(guide, this, branchedDecisionSymbol);
} else if (decision.getRelationToNextDecision() == RelationToNextDecision.NONE) {
return new OneDecisionModel(guide, this, branchedDecisionSymbol);
}
throw new GuideException("Could not find an appropriate decision model for the relation to the next decision");
}
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append(modelName + ", ");
for (DecisionModel model : children.values()) {
sb.append(model.toString() + ", ");
}
return sb.toString();
}
}
|