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