001package org.maltparser.core.syntaxgraph;
002
003import org.maltparser.core.config.ConfigurationDir;
004import org.maltparser.core.exception.MaltChainedException;
005import org.maltparser.core.flow.FlowChartInstance;
006import org.maltparser.core.flow.FlowException;
007import org.maltparser.core.flow.item.ChartItem;
008import org.maltparser.core.flow.spec.ChartItemSpecification;
009import org.maltparser.core.helper.HashSet;
010import org.maltparser.core.io.dataformat.DataFormatInstance;
011import org.maltparser.core.io.dataformat.DataFormatManager;
012import org.maltparser.core.io.dataformat.DataFormatSpecification.DataStructure;
013import org.maltparser.core.io.dataformat.DataFormatSpecification.Dependency;
014import org.maltparser.core.options.OptionManager;
015import org.maltparser.core.symbol.SymbolTableHandler;
016import org.maltparser.core.syntaxgraph.ds2ps.LosslessMapping;
017/**
018*
019*
020* @author Johan Hall
021*/
022public class SyntaxGraphChartItem extends ChartItem {
023        private String idName;
024        private String structureName;
025        private String taskName;
026        private TokenStructure graph;
027        
028        public SyntaxGraphChartItem() { super(); }
029        
030        public void initialize(FlowChartInstance flowChartinstance, ChartItemSpecification chartItemSpecification) throws MaltChainedException {
031                super.initialize(flowChartinstance, chartItemSpecification);
032                
033                for (String key : chartItemSpecification.getChartItemAttributes().keySet()) {
034                        if (key.equals("id")) {
035                                idName = chartItemSpecification.getChartItemAttributes().get(key);
036                        } else if (key.equals("structure")) {
037                                structureName = chartItemSpecification.getChartItemAttributes().get(key);
038                        } else if (key.equals("task")) {
039                                taskName = chartItemSpecification.getChartItemAttributes().get(key);
040                        }
041                }
042                if (idName == null) {
043                        idName = getChartElement("graph").getAttributes().get("id").getDefaultValue();
044                } else if (structureName == null) {
045                        structureName = getChartElement("graph").getAttributes().get("structure").getDefaultValue();
046                } else if (taskName == null) {
047                        taskName = getChartElement("graph").getAttributes().get("task").getDefaultValue();
048                }
049        }
050        
051        public int preprocess(int signal) throws MaltChainedException {
052                if (taskName.equals("create")) {
053                        boolean phrase = false;
054                        boolean dependency = false;
055                        ConfigurationDir configDir = (ConfigurationDir)flowChartinstance.getFlowChartRegistry(org.maltparser.core.config.ConfigurationDir.class, idName);
056                        DataFormatInstance dataFormatInstance = null;
057                        DataFormatManager dataFormatManager = configDir.getDataFormatManager();
058                        SymbolTableHandler symbolTables = configDir.getSymbolTables();
059
060                        
061
062                        for (String key : configDir.getDataFormatInstanceKeys()) {
063                                DataFormatInstance dfi = configDir.getDataFormatInstance(key);
064                                if (dfi.getDataFormarSpec().getDataStructure() == DataStructure.PHRASE) {
065                                        phrase = true;
066                                }
067                                if (dfi.getDataFormarSpec().getDataStructure() == DataStructure.DEPENDENCY) {
068                                        dependency = true;
069                                        dataFormatInstance = dfi;
070                                }
071                        }
072
073                        if (dependency == false && OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "flowchart").toString().equals("learn")) {
074                                dependency = true;
075                                HashSet<Dependency> deps = dataFormatManager.getInputDataFormatSpec().getDependencies();
076                                String nullValueStategy = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "singlemalt", "null_value").toString();
077                                for (Dependency dep : deps) {
078                                        dataFormatInstance = dataFormatManager.getDataFormatSpec(dep.getDependentOn()).createDataFormatInstance(symbolTables, nullValueStategy);
079                                        configDir.addDataFormatInstance(dataFormatManager.getOutputDataFormatSpec().getDataFormatName(), dataFormatInstance);
080                                }
081                        }
082
083                        if (dependency == true && phrase == false) {
084                                graph = new DependencyGraph(symbolTables);
085                                flowChartinstance.addFlowChartRegistry(org.maltparser.core.syntaxgraph.DependencyStructure.class, structureName, graph);
086                        } else if (dependency == true && phrase == true) {
087                                graph = new MappablePhraseStructureGraph(symbolTables);
088                                final DataFormatInstance inFormat = configDir.getDataFormatInstance(dataFormatManager.getInputDataFormatSpec().getDataFormatName()); 
089                                final DataFormatInstance outFormat = configDir.getDataFormatInstance(dataFormatManager.getOutputDataFormatSpec().getDataFormatName());
090
091                                if (inFormat != null && outFormat != null) {
092                                        LosslessMapping mapping = null;
093                                        if (inFormat.getDataFormarSpec().getDataStructure() == DataStructure.DEPENDENCY) {
094                                                mapping = new LosslessMapping(inFormat, outFormat, symbolTables);
095                                        } else {
096                                                mapping = new LosslessMapping(outFormat, inFormat, symbolTables);
097                                        }
098                                        if (inFormat.getDataFormarSpec().getDataStructure() == DataStructure.PHRASE) {
099                                                mapping.setHeadRules(OptionManager.instance().getOptionValue(getOptionContainerIndex(), "graph", "head_rules").toString());
100                                        }
101                                        ((MappablePhraseStructureGraph)graph).setMapping(mapping);
102                                } else {
103                                        throw new FlowException("Couldn't determine the input and output data format. ");
104                                }
105                                flowChartinstance.addFlowChartRegistry(org.maltparser.core.syntaxgraph.DependencyStructure.class, structureName, graph);
106                                flowChartinstance.addFlowChartRegistry(org.maltparser.core.syntaxgraph.PhraseStructure.class, structureName, graph);
107                        } else if (dependency == false && phrase == true) {
108                                graph = new PhraseStructureGraph(symbolTables);
109                                flowChartinstance.addFlowChartRegistry(org.maltparser.core.syntaxgraph.PhraseStructure.class, structureName, graph);
110                        } else {
111                                graph = new Sentence(symbolTables);
112                        }
113                        
114                        if (dataFormatInstance != null) {
115                                ((DependencyStructure)graph).setDefaultRootEdgeLabels(
116                                                OptionManager.instance().getOptionValue(getOptionContainerIndex(), "graph", "root_label").toString(), 
117                                                dataFormatInstance.getDependencyEdgeLabelSymbolTables(symbolTables));
118                        }
119                        flowChartinstance.addFlowChartRegistry(org.maltparser.core.syntaxgraph.TokenStructure.class, structureName, graph);
120                }
121                return signal;
122        }
123        
124        public int process(int signal) throws MaltChainedException {
125                return signal;
126        }
127        
128        public int postprocess(int signal) throws MaltChainedException {
129                return signal;
130        }
131        
132        public void terminate() throws MaltChainedException {
133                if (graph != null) {
134                        graph.clear();
135                        graph = null;
136                }
137        }
138        
139        public boolean equals(Object obj) {
140                if (this == obj)
141                        return true;
142                if (obj == null)
143                        return false;
144                if (getClass() != obj.getClass())
145                        return false;
146                return obj.toString().equals(this.toString());
147        }
148        
149        public int hashCode() {
150                return 217 + (null == toString() ? 0 : toString().hashCode());
151        }
152        
153        public String toString() {
154                StringBuilder sb = new StringBuilder();
155                sb.append("    graph ");
156                sb.append("id:");sb.append(idName);
157                sb.append(' ');
158                sb.append("task:");
159                sb.append(taskName);
160                sb.append(' ');
161                sb.append("structure:");
162                sb.append(structureName);
163                return sb.toString();
164        }
165}