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