001package org.maltparser.parser;
002
003import java.util.Set;
004import java.util.regex.Pattern;
005
006import org.maltparser.core.config.ConfigurationDir;
007import org.maltparser.core.exception.MaltChainedException;
008import org.maltparser.core.flow.FlowChartInstance;
009import org.maltparser.core.flow.item.ChartItem;
010import org.maltparser.core.flow.spec.ChartItemSpecification;
011import org.maltparser.core.io.dataformat.DataFormatInstance;
012import org.maltparser.core.io.dataformat.DataFormatManager;
013import org.maltparser.core.io.dataformat.DataFormatSpecification.DataStructure;
014import org.maltparser.core.io.dataformat.DataFormatSpecification.Dependency;
015import org.maltparser.core.options.OptionManager;
016import org.maltparser.core.syntaxgraph.DependencyStructure;
017/**
018 * @author Johan Hall
019 *
020 */
021public 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                                DataFormatManager dataFormatManager = configDir.getDataFormatManager();
077                                if (modeName.equals("learn")) {
078                                        DataFormatInstance dataFormatInstance = null;
079                                        if (dataFormatManager.getInputDataFormatSpec().getDataStructure() == DataStructure.PHRASE) {
080                                                Set<Dependency> deps = dataFormatManager.getInputDataFormatSpec().getDependencies();
081                                                String nullValueStrategy = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "singlemalt", "null_value").toString();
082
083                                                for (Dependency dep : dataFormatManager.getInputDataFormatSpec().getDependencies()) {
084                                                        dataFormatInstance = dataFormatManager.getDataFormatSpec(dep.getDependentOn()).createDataFormatInstance(configDir.getSymbolTables(), nullValueStrategy);
085                                                        configDir.addDataFormatInstance(dataFormatManager.getOutputDataFormatSpec().getDataFormatName(), dataFormatInstance);
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 = configDir.getDataFormatInstance(dataFormatManager.getInputDataFormatSpec().getDataFormatName());
104                                        }
105                                        singleMalt.initialize(getOptionContainerIndex(), dataFormatInstance, configDir.getSymbolTables(), configDir, SingleMalt.LEARN);
106                                } else if (modeName.equals("parse")) {
107                                        singleMalt.initialize(getOptionContainerIndex(),
108                                                        configDir.getDataFormatInstance(dataFormatManager.getInputDataFormatSpec().getDataFormatName()), configDir.getSymbolTables(), configDir, SingleMalt.PARSE);
109                                } else {
110                                        return ChartItem.TERMINATE;
111                                }
112                        } else {
113                                return ChartItem.TERMINATE;
114                        }
115                }
116                return signal;
117        }
118        
119        public int process(int signal) throws MaltChainedException {
120                if (taskName.equals("process")) {
121                        if (cachedSourceGraph == null) {
122                                cachedSourceGraph = (DependencyStructure)flowChartinstance.getFlowChartRegistry(org.maltparser.core.syntaxgraph.DependencyStructure.class, sourceName);
123                        }
124                        if (cachedTargetGraph == null) {
125                                cachedTargetGraph = (DependencyStructure)flowChartinstance.getFlowChartRegistry(org.maltparser.core.syntaxgraph.DependencyStructure.class, targetName);
126                        }
127                        if (modeName.equals("learn")) {
128                                singleMalt.oracleParse(cachedSourceGraph, cachedTargetGraph);
129                        } else if (modeName.equals("parse")) {
130                                singleMalt.parse(cachedSourceGraph);
131//                              if (cachedSourceGraph instanceof MappablePhraseStructureGraph) {
132//                                      System.out.println("MappablePhraseStructureGraph");
133//                                      ((MappablePhraseStructureGraph)cachedSourceGraph).getMapping().connectUnattachedSpines((MappablePhraseStructureGraph)cachedSourceGraph);
134//                              }       
135                        }
136                }
137                return signal;
138        }
139        
140        public int postprocess(int signal) throws MaltChainedException {
141                if (taskName.equals("train") && singleMalt.getGuide() != null) {
142                        singleMalt.getGuide().noMoreInstances();
143                } else if (taskName.equals("train") && singleMalt.getGuide() == null) {
144                        singleMalt.train();
145                }
146                return signal;
147        }
148
149        public void terminate() throws MaltChainedException {
150                if (flowChartinstance.getFlowChartRegistry(org.maltparser.parser.SingleMalt.class, idName) != null) {
151                        singleMalt.terminate(null);
152                        flowChartinstance.removeFlowChartRegistry(org.maltparser.parser.SingleMalt.class, idName);
153                        flowChartinstance.removeFlowChartRegistry(org.maltparser.core.config.Configuration.class, idName);
154                        singleMalt = null;
155                } else {
156                        singleMalt = null;
157                }
158                cachedSourceGraph = null;
159                cachedTargetGraph = null;
160        }
161        
162        public SingleMalt getSingleMalt() {
163                return singleMalt;
164        }
165        
166        public void setSingleMalt(SingleMalt singleMalt) {
167                this.singleMalt = singleMalt;
168        }
169
170        public String getTargetName() {
171                return targetName;
172        }
173
174        public void setTargetName(String targetName) {
175                this.targetName = targetName;
176        }
177
178        public String getSourceName() {
179                return sourceName;
180        }
181
182        public void setSourceName(String sourceName) {
183                this.sourceName = sourceName;
184        }
185
186        public boolean equals(Object obj) {
187                if (this == obj)
188                        return true;
189                if (obj == null)
190                        return false;
191                if (getClass() != obj.getClass())
192                        return false;
193                return obj.toString().equals(this.toString());
194        }
195        
196        public int hashCode() {
197                return 217 + (null == toString() ? 0 : toString().hashCode());
198        }
199        
200        public String toString() {
201                StringBuilder sb = new StringBuilder();
202                sb.append("    singlemalt ");
203                sb.append("id:");sb.append(idName);
204                sb.append(' ');
205                sb.append("mode:");sb.append(modeName);
206                sb.append(' ');
207                sb.append("task:");sb.append(taskName);
208                sb.append(' ');
209                sb.append("source:");sb.append(sourceName);
210                sb.append(' ');
211                sb.append("target:");sb.append(targetName);
212                return sb.toString();
213        }
214}