001    package org.maltparser.parser.guide.decision;
002    
003    import org.maltparser.core.exception.MaltChainedException;
004    import org.maltparser.core.feature.FeatureModel;
005    import org.maltparser.core.feature.FeatureVector;
006    import org.maltparser.core.syntaxgraph.DependencyStructure;
007    import org.maltparser.parser.DependencyParserConfig;
008    import org.maltparser.parser.guide.ClassifierGuide;
009    import org.maltparser.parser.guide.GuideException;
010    import org.maltparser.parser.guide.instance.AtomicModel;
011    import org.maltparser.parser.guide.instance.DecisionTreeModel;
012    import org.maltparser.parser.guide.instance.FeatureDivideModel;
013    import org.maltparser.parser.guide.instance.InstanceModel;
014    import org.maltparser.parser.history.action.GuideDecision;
015    import org.maltparser.parser.history.action.MultipleDecision;
016    import org.maltparser.parser.history.action.SingleDecision;
017    /**
018    *
019    * @author Johan Hall
020    * @since 1.1
021    **/
022    public class OneDecisionModel implements DecisionModel {
023            private ClassifierGuide guide;
024            private String modelName;
025            private FeatureModel featureModel;
026            private InstanceModel instanceModel;
027            private int decisionIndex;
028            private DecisionModel prevDecisionModel;
029            private String branchedDecisionSymbols;
030            private int nIteration;
031    
032            
033            public OneDecisionModel(ClassifierGuide guide, FeatureModel featureModel) throws MaltChainedException {
034                    this.branchedDecisionSymbols = "";
035                    setGuide(guide);
036                    setFeatureModel(featureModel);
037                    setDecisionIndex(0);
038                    if (guide.getGuideName() == null || guide.getGuideName().equals("")) {
039                            setModelName("odm"+decisionIndex);
040                    } else {
041                            setModelName(guide.getGuideName()+".odm"+decisionIndex);
042                    }
043                    
044                    setPrevDecisionModel(null);
045            }
046            
047            public OneDecisionModel(ClassifierGuide guide, DecisionModel prevDecisionModel, String branchedDecisionSymbol) throws MaltChainedException {
048                    if (branchedDecisionSymbol != null && branchedDecisionSymbol.length() > 0) {
049                            this.branchedDecisionSymbols = branchedDecisionSymbol;
050                    } else {
051                            this.branchedDecisionSymbols = "";
052                    }
053                    setGuide(guide);
054                    setFeatureModel(prevDecisionModel.getFeatureModel());
055                    setDecisionIndex(prevDecisionModel.getDecisionIndex() + 1);
056                    setPrevDecisionModel(prevDecisionModel);
057                    if (branchedDecisionSymbols != null && branchedDecisionSymbols.length() > 0) {
058                            setModelName("odm"+decisionIndex+branchedDecisionSymbols);
059                    } else {
060                            setModelName("odm"+decisionIndex);
061                    }
062            }
063            
064            public void updateFeatureModel() throws MaltChainedException {
065                    featureModel.update();
066            }
067            
068            public void updateCardinality() throws MaltChainedException {
069                    featureModel.updateCardinality();
070            }
071            
072    
073            public void finalizeSentence(DependencyStructure dependencyGraph) throws MaltChainedException {
074                    if (instanceModel != null) {
075                            instanceModel.finalizeSentence(dependencyGraph);
076                    }
077            }
078            
079            public void noMoreInstances() throws MaltChainedException {
080                    if (guide.getGuideMode() == ClassifierGuide.GuideMode.CLASSIFY) {
081                            throw new GuideException("The decision model could not create it's model. ");
082                    }
083                    featureModel.updateCardinality();
084                    if (instanceModel != null) {
085                            instanceModel.noMoreInstances();
086                            instanceModel.train();
087                    }
088            }
089    
090            public void terminate() throws MaltChainedException {
091                    if (instanceModel != null) {
092                            instanceModel.terminate();
093                            instanceModel = null;
094                    }
095            }
096            
097            public void addInstance(GuideDecision decision) throws MaltChainedException {
098                    updateFeatureModel();
099                    final SingleDecision singleDecision = (decision instanceof SingleDecision)?(SingleDecision)decision:((MultipleDecision)decision).getSingleDecision(decisionIndex);
100                    
101                    if (instanceModel == null) {
102                            initInstanceModel(singleDecision.getTableContainer().getTableContainerName());
103                    }
104                    instanceModel.addInstance(singleDecision);
105            }
106            
107            public boolean predict(GuideDecision decision) throws MaltChainedException {
108                    updateFeatureModel();
109                    final SingleDecision singleDecision = (decision instanceof SingleDecision)?(SingleDecision)decision:((MultipleDecision)decision).getSingleDecision(decisionIndex);
110    
111                    if (instanceModel == null) {
112                            initInstanceModel(singleDecision.getTableContainer().getTableContainerName());
113                    }
114                    return instanceModel.predict(singleDecision);
115            }
116            
117            public FeatureVector predictExtract(GuideDecision decision) throws MaltChainedException {
118                    updateFeatureModel();
119                    final SingleDecision singleDecision = (decision instanceof SingleDecision)?(SingleDecision)decision:((MultipleDecision)decision).getSingleDecision(decisionIndex);
120    
121                    if (instanceModel == null) {
122                            initInstanceModel(singleDecision.getTableContainer().getTableContainerName());
123                    }
124                    return instanceModel.predictExtract(singleDecision);
125            }
126            
127            public FeatureVector extract() throws MaltChainedException {
128                    updateFeatureModel();
129                    return instanceModel.extract();
130            }
131            
132            public boolean predictFromKBestList(GuideDecision decision) throws MaltChainedException {
133                    if (decision instanceof SingleDecision) {
134                            return ((SingleDecision)decision).updateFromKBestList();
135                    } else {
136                            return ((MultipleDecision)decision).getSingleDecision(decisionIndex).updateFromKBestList();
137                    }
138            }
139            
140            public ClassifierGuide getGuide() {
141                    return guide;
142            }
143    
144            public String getModelName() {
145                    return modelName;
146            }
147            
148            public FeatureModel getFeatureModel() {
149                    return featureModel;
150            }
151    
152            public int getDecisionIndex() {
153                    return decisionIndex;
154            }
155    
156            public DecisionModel getPrevDecisionModel() {
157                    return prevDecisionModel;
158            }
159            
160            private void setPrevDecisionModel(DecisionModel prevDecisionModel) {
161                    this.prevDecisionModel = prevDecisionModel;
162            }
163    
164            private void setFeatureModel(FeatureModel featureModel) {
165                    this.featureModel = featureModel;
166            }
167            
168            private void setDecisionIndex(int decisionIndex) {
169                    this.decisionIndex = decisionIndex;
170            }
171    
172            private void setModelName(String modelName) {
173                    this.modelName = modelName;
174            }
175            
176            private void setGuide(ClassifierGuide guide) {
177                    this.guide = guide;
178            }
179            
180            private final void initInstanceModel(String subModelName) throws MaltChainedException {
181                    FeatureVector fv = featureModel.getFeatureVector(branchedDecisionSymbols+"."+subModelName);
182                    if (fv == null) {
183                            fv = featureModel.getFeatureVector(subModelName);
184                    }
185                    if (fv == null) {
186                            fv = featureModel.getMainFeatureVector();
187                    }
188                    DependencyParserConfig c = guide.getConfiguration();
189                    
190    //              if (c.getOptionValue("guide", "tree_automatic_split_order").toString().equals("yes") ||
191    //                              (c.getOptionValue("guide", "tree_split_columns")!=null &&
192    //                      c.getOptionValue("guide", "tree_split_columns").toString().length() > 0) ||
193    //                      (c.getOptionValue("guide", "tree_split_structures")!=null &&
194    //                      c.getOptionValue("guide", "tree_split_structures").toString().length() > 0)) {
195    //                      instanceModel = new DecisionTreeModel(fv, this); 
196    //              }else 
197                    if (c.getOptionValue("guide", "data_split_column").toString().length() == 0) {
198                            instanceModel = new AtomicModel(-1, fv, this);
199                    } else {
200                            instanceModel = new FeatureDivideModel(fv, this);
201                    }
202            }
203            
204            public String toString() {              
205                    return modelName;
206            }
207    }