001    package org.maltparser.parser;
002    
003    import java.util.HashMap;
004    import java.util.HashSet;
005    import java.util.regex.Pattern;
006    
007    import org.maltparser.core.config.ConfigurationDir;
008    import org.maltparser.core.exception.MaltChainedException;
009    import org.maltparser.core.flow.FlowChartInstance;
010    import org.maltparser.core.flow.item.ChartItem;
011    import org.maltparser.core.flow.spec.ChartItemSpecification;
012    import org.maltparser.core.io.dataformat.DataFormatInstance;
013    import org.maltparser.core.io.dataformat.DataFormatManager;
014    import org.maltparser.core.io.dataformat.DataFormatSpecification.DataStructure;
015    import org.maltparser.core.io.dataformat.DataFormatSpecification.Dependency;
016    import org.maltparser.core.options.OptionManager;
017    import org.maltparser.core.syntaxgraph.DependencyStructure;
018    import org.maltparser.core.syntaxgraph.MappablePhraseStructureGraph;
019    /**
020     * @author Johan Hall
021     *
022     */
023    public class SingleMaltChartItem extends ChartItem {
024            private SingleMalt singleMalt;
025            private String idName;
026            private String targetName;
027            private String sourceName;
028            private String modeName;
029            private String taskName;
030            private DependencyStructure cachedSourceGraph = null;
031            private DependencyStructure cachedTargetGraph = null;
032    
033            
034            
035            public void initialize(FlowChartInstance flowChartinstance, ChartItemSpecification chartItemSpecification) throws MaltChainedException {
036                    super.initialize(flowChartinstance, chartItemSpecification);
037                    
038                    for (String key : chartItemSpecification.getChartItemAttributes().keySet()) {
039                            if (key.equals("target")) {
040                                    targetName = chartItemSpecification.getChartItemAttributes().get(key);
041                            } else if (key.equals("source")) {
042                                    sourceName = chartItemSpecification.getChartItemAttributes().get(key);
043                            }  else if (key.equals("mode")) {
044                                    modeName = chartItemSpecification.getChartItemAttributes().get(key);
045                            }  else if (key.equals("task")) {
046                                    taskName = chartItemSpecification.getChartItemAttributes().get(key);
047                            } else if (key.equals("id")) {
048                                    idName = chartItemSpecification.getChartItemAttributes().get(key);
049                            }
050                    }
051                    if (targetName == null) {
052                            targetName = getChartElement("singlemalt").getAttributes().get("target").getDefaultValue();
053                    } else if (sourceName == null) {
054                            sourceName = getChartElement("singlemalt").getAttributes().get("source").getDefaultValue();
055                    } else if (modeName == null) {
056                            modeName = getChartElement("singlemalt").getAttributes().get("mode").getDefaultValue();
057                    } else if (taskName == null) {
058                            taskName = getChartElement("singlemalt").getAttributes().get("task").getDefaultValue();
059                    } else if (idName == null) {
060                            idName = getChartElement("singlemalt").getAttributes().get("id").getDefaultValue();
061                    }
062                    
063                    singleMalt = (SingleMalt)flowChartinstance.getFlowChartRegistry(org.maltparser.parser.SingleMalt.class, idName);
064                    if (singleMalt == null) {
065                            singleMalt = new SingleMalt();
066                            flowChartinstance.addFlowChartRegistry(org.maltparser.parser.SingleMalt.class, idName, singleMalt);
067                            flowChartinstance.addFlowChartRegistry(org.maltparser.core.config.Configuration.class, idName, singleMalt);
068    
069                    }
070            }
071            
072            
073            public int preprocess(int signal) throws MaltChainedException {
074                    if (taskName.equals("init")) {
075                            if (modeName.equals("learn") || modeName.equals("parse")) {
076                                    OptionManager.instance().overloadOptionValue(getOptionContainerIndex(), "singlemalt", "mode", modeName);
077                                    ConfigurationDir configDir = (ConfigurationDir)flowChartinstance.getFlowChartRegistry(org.maltparser.core.config.ConfigurationDir.class, idName);
078                                    DataFormatManager dataFormatManager = configDir.getDataFormatManager();
079    //                              DataFormatManager dataFormatManager = flowChartinstance.getDataFormatManager();
080    //                              HashMap<String, DataFormatInstance> dataFormatInstances = flowChartinstance.getDataFormatInstances();
081                                    HashMap<String, DataFormatInstance> dataFormatInstances = configDir.getDataFormatInstances();
082                                    if (modeName.equals("learn")) {
083                                            DataFormatInstance dataFormatInstance = null;
084                                            if (dataFormatManager.getInputDataFormatSpec().getDataStructure() == DataStructure.PHRASE) {
085                                                    HashSet<Dependency> deps = dataFormatManager.getInputDataFormatSpec().getDependencies();
086    //                                              String nullValueStategy = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "singlemalt", "null_value").toString();
087    //                                              String rootLabels = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "graph", "root_label").toString();
088                                                    for (Dependency dep : deps) {
089    //                                                      dataFormatInstance = dataFormatManager.getDataFormatSpec(dep.getDependentOn()).createDataFormatInstance(flowChartinstance.getSymbolTables(), nullValueStategy, rootLabels);
090    //                                                      flowChartinstance.getDataFormatInstances().put(flowChartinstance.getDataFormatManager().getOutputDataFormatSpec().getDataFormatName(), dataFormatInstance);
091                                                            dataFormatInstance = dataFormatInstances.get(dataFormatManager.getOutputDataFormatSpec().getDataFormatName());
092                                                    }
093                                                    
094                                                    String decisionSettings = OptionManager.instance().getOptionValue(getOptionContainerIndex(),"guide", "decision_settings").toString().trim();
095                                                    StringBuilder newDecisionSettings = new StringBuilder();
096                                                    if (!Pattern.matches(".*A\\.HEADREL.*", decisionSettings)) {
097                                                            newDecisionSettings.append("+A.HEADREL");
098                                                    }
099                                                    if (!Pattern.matches(".*A\\.PHRASE.*", decisionSettings)) {
100                                                            newDecisionSettings.append("+A.PHRASE");
101                                                    }
102                                                    if (!Pattern.matches(".*A\\.ATTACH.*", decisionSettings)) {
103                                                            newDecisionSettings.append("+A.ATTACH");
104                                                    }
105                                                    if (newDecisionSettings.length() > 0) {
106                                                            OptionManager.instance().overloadOptionValue(getOptionContainerIndex(), "guide", "decision_settings", decisionSettings+newDecisionSettings.toString());
107                                                    }
108                                            } else {
109                                                    dataFormatInstance = dataFormatInstances.get(dataFormatManager.getInputDataFormatSpec().getDataFormatName());
110                                            }
111                                            singleMalt.initialize(getOptionContainerIndex(), dataFormatInstance, configDir, SingleMalt.LEARN);
112                                    } else if (modeName.equals("parse")) {
113                                            singleMalt.initialize(getOptionContainerIndex(), 
114                                                            dataFormatInstances.get(dataFormatManager.getInputDataFormatSpec().getDataFormatName()), configDir, SingleMalt.PARSE);
115                                    } else {
116                                            return ChartItem.TERMINATE;
117                                    }
118                            } else {
119                                    return ChartItem.TERMINATE;
120                            }
121                    }
122                    return signal;
123            }
124            
125            public int process(int signal) throws MaltChainedException {
126                    if (taskName.equals("process")) {
127                            if (cachedSourceGraph == null) {
128                                    cachedSourceGraph = (DependencyStructure)flowChartinstance.getFlowChartRegistry(org.maltparser.core.syntaxgraph.DependencyStructure.class, sourceName);
129                            }
130                            if (cachedTargetGraph == null) {
131                                    cachedTargetGraph = (DependencyStructure)flowChartinstance.getFlowChartRegistry(org.maltparser.core.syntaxgraph.DependencyStructure.class, targetName);
132                            }
133                            if (modeName.equals("learn")) {
134                                    singleMalt.oracleParse(cachedSourceGraph, cachedTargetGraph);
135                            } else if (modeName.equals("parse")) {
136                                    singleMalt.parse(cachedSourceGraph);
137                                    if (cachedSourceGraph instanceof MappablePhraseStructureGraph) {
138                                            ((MappablePhraseStructureGraph)cachedSourceGraph).getMapping().connectUnattachedSpines((MappablePhraseStructureGraph)cachedSourceGraph);
139                                    }
140                                    
141                            }
142                    }
143                    return signal;
144            }
145            
146            public int postprocess(int signal) throws MaltChainedException {
147                    if (taskName.equals("train") && singleMalt.getGuide() != null) {
148                            singleMalt.getGuide().noMoreInstances();
149                    } else if (taskName.equals("train") && singleMalt.getGuide() == null) {
150                            singleMalt.train();
151                    }
152                    return signal;
153            }
154    
155            public void terminate() throws MaltChainedException {
156                    if (flowChartinstance.getFlowChartRegistry(org.maltparser.parser.SingleMalt.class, idName) != null) {
157                            singleMalt.terminate(null);
158                            flowChartinstance.removeFlowChartRegistry(org.maltparser.parser.SingleMalt.class, idName);
159                            flowChartinstance.removeFlowChartRegistry(org.maltparser.core.config.Configuration.class, idName);
160                            singleMalt = null;
161                    } else {
162                            singleMalt = null;
163                    }
164                    cachedSourceGraph = null;
165                    cachedTargetGraph = null;
166            }
167            
168            public SingleMalt getSingleMalt() {
169                    return singleMalt;
170            }
171            
172            public void setSingleMalt(SingleMalt singleMalt) {
173                    this.singleMalt = singleMalt;
174            }
175    
176            public String getTargetName() {
177                    return targetName;
178            }
179    
180            public void setTargetName(String targetName) {
181                    this.targetName = targetName;
182            }
183    
184            public String getSourceName() {
185                    return sourceName;
186            }
187    
188            public void setSourceName(String sourceName) {
189                    this.sourceName = sourceName;
190            }
191    
192            public boolean equals(Object obj) {
193                    if (this == obj)
194                            return true;
195                    if (obj == null)
196                            return false;
197                    if (getClass() != obj.getClass())
198                            return false;
199                    return obj.toString().equals(this.toString());
200            }
201            
202            public int hashCode() {
203                    return 217 + (null == toString() ? 0 : toString().hashCode());
204            }
205            
206            public String toString() {
207                    StringBuilder sb = new StringBuilder();
208                    sb.append("    singlemalt ");
209                    sb.append("id:");sb.append(idName);
210                    sb.append(' ');
211                    sb.append("mode:");sb.append(modeName);
212                    sb.append(' ');
213                    sb.append("task:");sb.append(taskName);
214                    sb.append(' ');
215                    sb.append("source:");sb.append(sourceName);
216                    sb.append(' ');
217                    sb.append("target:");sb.append(targetName);
218                    return sb.toString();
219            }
220    }