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