File size: 6,942 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 | package org.maltparser.parser.guide;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.File;
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.AlgoritmInterface;
import org.maltparser.parser.DependencyParserConfig;
import org.maltparser.parser.guide.decision.BranchedDecisionModel;
import org.maltparser.parser.guide.decision.DecisionModel;
import org.maltparser.parser.guide.decision.OneDecisionModel;
import org.maltparser.parser.guide.decision.SeqDecisionModel;
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;
/**
* The guide is used by a parsing algorithm to predict the next parser action during parsing and to
* add a instance to the training instance set during learning.
@author Johan Hall
*/
public class SingleGuide implements ClassifierGuide {
private final DependencyParserConfig configuration;
private final GuideMode guideMode;
private final FeatureModel featureModel2;
private DecisionModel decisionModel = null;
private String guideName;
public SingleGuide(AlgoritmInterface algorithm, GuideMode guideMode) throws MaltChainedException {
this.configuration = algorithm.getManager();
this.guideMode = guideMode;
String featureModelFileName = getConfiguration().getOptionValue("guide", "features").toString().trim();
// if (getConfiguration().isLoggerInfoEnabled()) {
//
// getConfiguration().logDebugMessage(" Feature model : " + featureModelFileName+"\n");
// if (getGuideMode() == ClassifierGuide.GuideMode.BATCH) {
// getConfiguration().logDebugMessage(" Learner : " + getConfiguration().getOptionValueString("guide", "learner").toString()+"\n");
// } else {
// getConfiguration().logDebugMessage(" Classifier : " + getConfiguration().getOptionValueString("guide", "learner")+"\n");
// }
// }
String dataSplitColumn = getConfiguration().getOptionValue("guide", "data_split_column").toString().trim();
String dataSplitStructure = getConfiguration().getOptionValue("guide", "data_split_structure").toString().trim();
featureModel2 = getConfiguration().getFeatureModelManager().getFeatureModel(findURL(featureModelFileName, getConfiguration()), 0, algorithm.getParserRegistry(), dataSplitColumn, dataSplitStructure);
// if (getGuideMode() == ClassifierGuide.GuideMode.BATCH) {
// getConfiguration().writeInfoToConfigFile("\nFEATURE MODEL\n");
// getConfiguration().writeInfoToConfigFile(featureModel.toString());
// }
}
public void addInstance(FeatureModel featureModel,GuideDecision decision) throws MaltChainedException {
if (decisionModel == null) {
if (decision instanceof SingleDecision) {
initDecisionModel((SingleDecision)decision);
} else if (decision instanceof MultipleDecision && decision.numberOfDecisions() > 0) {
initDecisionModel(((MultipleDecision)decision).getSingleDecision(0));
}
}
decisionModel.addInstance(featureModel,decision);
}
public void finalizeSentence(DependencyStructure dependencyGraph) throws MaltChainedException {
if (decisionModel != null) {
decisionModel.finalizeSentence(dependencyGraph);
}
}
public void noMoreInstances() throws MaltChainedException {
if (decisionModel != null) {
decisionModel.noMoreInstances(featureModel2);
} else {
configuration.logDebugMessage("The guide cannot create any models because there is no decision model. ");
}
}
public void terminate() throws MaltChainedException {
if (decisionModel != null) {
decisionModel.terminate();
decisionModel = null;
}
}
public void predict(FeatureModel featureModel,GuideDecision decision) throws MaltChainedException {
if (decisionModel == null) {
if (decision instanceof SingleDecision) {
initDecisionModel((SingleDecision)decision);
} else if (decision instanceof MultipleDecision && decision.numberOfDecisions() > 0) {
initDecisionModel(((MultipleDecision)decision).getSingleDecision(0));
}
}
decisionModel.predict(featureModel,decision);
}
public FeatureVector predictExtract(FeatureModel featureModel,GuideDecision decision) throws MaltChainedException {
if (decisionModel == null) {
if (decision instanceof SingleDecision) {
initDecisionModel((SingleDecision)decision);
} else if (decision instanceof MultipleDecision && decision.numberOfDecisions() > 0) {
initDecisionModel(((MultipleDecision)decision).getSingleDecision(0));
}
}
return decisionModel.predictExtract(featureModel,decision);
}
public FeatureVector extract(FeatureModel featureModel) throws MaltChainedException {
return decisionModel.extract(featureModel);
}
public boolean predictFromKBestList(FeatureModel featureModel, GuideDecision decision) throws MaltChainedException {
if (decisionModel != null) {
return decisionModel.predictFromKBestList(featureModel,decision);
} else {
throw new GuideException("The decision model cannot be found. ");
}
}
public DecisionModel getDecisionModel() {
return decisionModel;
}
public DependencyParserConfig getConfiguration() {
return configuration;
}
public GuideMode getGuideMode() {
return guideMode;
}
protected void initDecisionModel(SingleDecision decision) throws MaltChainedException {
if (decision.getRelationToNextDecision() == RelationToNextDecision.SEQUANTIAL) {
decisionModel = new SeqDecisionModel(this);
} else if (decision.getRelationToNextDecision() == RelationToNextDecision.BRANCHED) {
decisionModel = new BranchedDecisionModel(this);
} else if (decision.getRelationToNextDecision() == RelationToNextDecision.NONE) {
decisionModel = new OneDecisionModel(this);
}
}
public String getGuideName() {
return guideName;
}
public void setGuideName(String guideName) {
this.guideName = guideName;
}
public static URL findURL(String specModelFileName, DependencyParserConfig config) throws MaltChainedException {
URL url = null;
File specFile = config.getFile(specModelFileName);
if (specFile != null && specFile.exists()) {
try {
url = new URL("file:///"+specFile.getAbsolutePath());
} catch (MalformedURLException e) {
throw new MaltChainedException("Malformed URL: "+specFile, e);
}
} else {
url = config.getConfigFileEntryURL(specModelFileName);
}
return url;
}
public String toString() {
final StringBuilder sb = new StringBuilder();
return sb.toString();
}
}
|