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    *
019    *
020    * @author Johan Hall
021    */
022    public class SingleMaltChartItem extends ChartItem {
023            private SingleMalt singleMalt;
024            
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            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 boolean preprocess() 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 false;
112                                    }
113                            } else {
114                                    return false;
115                            }
116                    }
117                    return true;
118            }
119            
120            public boolean process(boolean continueNextSentence) 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    
129                            if (modeName.equals("learn")) {
130                                    singleMalt.oracleParse(cachedSourceGraph, cachedTargetGraph);
131                            } else if (modeName.equals("parse")) {
132                                    singleMalt.parse(cachedSourceGraph);
133                                    if (cachedSourceGraph instanceof MappablePhraseStructureGraph) {
134                                            ((MappablePhraseStructureGraph)cachedSourceGraph).getMapping().connectUnattachedSpines((MappablePhraseStructureGraph)cachedSourceGraph);
135                                    }
136                                    
137                            }
138                    }
139                    return continueNextSentence;
140            }
141            
142            public boolean postprocess() throws MaltChainedException {
143                    if (taskName.equals("train")) {
144                            singleMalt.getGuide().noMoreInstances();
145                    }
146                    return true;
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    }