001package org.maltparser.transform.pseudo;
002
003
004import org.maltparser.core.config.ConfigurationDir;
005import org.maltparser.core.exception.MaltChainedException;
006import org.maltparser.core.flow.FlowChartInstance;
007import org.maltparser.core.flow.item.ChartItem;
008import org.maltparser.core.flow.spec.ChartItemSpecification;
009import org.maltparser.core.helper.SystemLogger;
010import org.maltparser.core.io.dataformat.DataFormatInstance;
011import org.maltparser.core.options.OptionManager;
012import org.maltparser.core.syntaxgraph.DependencyStructure;
013import org.maltparser.core.syntaxgraph.TokenStructure;
014/**
015*
016*
017* @author Johan Hall
018*/
019public class PseudoProjChartItem extends ChartItem {
020        private String idName;
021        private String targetName;
022        private String sourceName;
023        private String taskName;
024        
025        private String marking_strategy;
026        private String covered_root;
027        private String lifting_order;
028        
029        private PseudoProjectivity pproj; 
030        private boolean pprojActive = false;
031        private TokenStructure cachedGraph = null;
032        
033        public PseudoProjChartItem() {}
034        
035        public void initialize(FlowChartInstance flowChartinstance, ChartItemSpecification chartItemSpecification) throws MaltChainedException {
036                super.initialize(flowChartinstance, chartItemSpecification);
037                
038                for (String key : chartItemSpecification.getChartItemAttributes().keySet()) {
039                        if (key.equals("target")) {
040                                targetName = chartItemSpecification.getChartItemAttributes().get(key);
041                        } else if (key.equals("source")) {
042                                sourceName = chartItemSpecification.getChartItemAttributes().get(key);
043                        } else if (key.equals("id")) {
044                                idName = chartItemSpecification.getChartItemAttributes().get(key);
045                        }  else if (key.equals("task")) {
046                                taskName = chartItemSpecification.getChartItemAttributes().get(key);
047                        }
048                }
049                
050                if (targetName == null) {
051                        targetName = getChartElement("pseudoproj").getAttributes().get("target").getDefaultValue();
052                } else if (sourceName == null) {
053                        sourceName = getChartElement("pseudoproj").getAttributes().get("source").getDefaultValue();
054                } else if (idName == null) {
055                        idName = getChartElement("pseudoproj").getAttributes().get("id").getDefaultValue();
056                } else if (taskName == null) {
057                        taskName = getChartElement("pseudoproj").getAttributes().get("task").getDefaultValue();
058                }
059        
060                PseudoProjectivity tmppproj = (PseudoProjectivity)flowChartinstance.getFlowChartRegistry(org.maltparser.transform.pseudo.PseudoProjectivity.class, idName);
061                if (tmppproj == null) {
062                        pproj = new PseudoProjectivity();
063                        flowChartinstance.addFlowChartRegistry(org.maltparser.transform.pseudo.PseudoProjectivity.class, idName, pproj);
064                } else {
065                        pproj = tmppproj;
066                }
067        }
068        
069        public int preprocess(int signal) throws MaltChainedException {
070                if (taskName.equals("init")) {
071                        ConfigurationDir configDir = (ConfigurationDir)flowChartinstance.getFlowChartRegistry(org.maltparser.core.config.ConfigurationDir.class, idName);
072//                      SymbolTableHandler symbolTables = configDir.getSymbolTables();
073                        DataFormatInstance dataFormatInstance = configDir.getInputDataFormatInstance();
074                        marking_strategy = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "pproj", "marking_strategy").toString().trim();
075                        covered_root = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "pproj", "covered_root").toString().trim();
076                        lifting_order = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "pproj", "lifting_order").toString().trim();
077                        if (!marking_strategy.equalsIgnoreCase("none") || !covered_root.equalsIgnoreCase("none")) { 
078                                pproj.initialize(marking_strategy, covered_root, lifting_order, SystemLogger.logger(), dataFormatInstance, configDir.getSymbolTables());
079                        }
080                        if (!marking_strategy.equalsIgnoreCase("none") || !covered_root.equalsIgnoreCase("none")) { 
081                                pprojActive = true;
082                        }
083                }
084                return signal;
085        }
086        
087        public int process(int signal) throws MaltChainedException {
088                if (cachedGraph == null) {
089                        marking_strategy = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "pproj", "marking_strategy").toString().trim();
090                        covered_root = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "pproj", "covered_root").toString().trim();
091                        lifting_order = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "pproj", "lifting_order").toString().trim();
092
093                        cachedGraph = (TokenStructure)flowChartinstance.getFlowChartRegistry(org.maltparser.core.syntaxgraph.TokenStructure.class, sourceName);
094                        if (!marking_strategy.equalsIgnoreCase("none") || !covered_root.equalsIgnoreCase("none")) { 
095                                pprojActive = true;
096                        }
097                }
098                
099                if (pprojActive && cachedGraph instanceof DependencyStructure) {
100                        if (taskName.equals("proj")) {
101                                        pproj.projectivize((DependencyStructure)cachedGraph);
102                        } else if (taskName.equals("merge")) {
103                                        pproj.mergeArclabels((DependencyStructure)cachedGraph);
104                        } else if (taskName.equals("deproj")) {
105                                        pproj.deprojectivize((DependencyStructure)cachedGraph);
106//                              marking_strategy = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "pproj", "marking_strategy").toString().trim();
107//                              covered_root = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "pproj", "covered_root").toString().trim();
108//                              ConfigurationDir configDir = (ConfigurationDir)flowChartinstance.getFlowChartRegistry(org.maltparser.core.config.ConfigurationDir.class, idName);
109//                              Deprojectivizer deprojectivizer = new Deprojectivizer(marking_strategy, covered_root, configDir.getInputDataFormatInstance(), configDir.getSymbolTables());
110//                              deprojectivizer.deprojectivize((DependencyStructure)cachedGraph);
111                        } else if (taskName.equals("split")) {
112                                        pproj.splitArclabels((DependencyStructure)cachedGraph);
113                        }
114                }
115                return signal;
116        }
117        
118        public int postprocess(int signal) throws MaltChainedException {
119                return signal;
120        }
121
122        
123        public void terminate() throws MaltChainedException {
124                pproj = null; 
125                pprojActive = false;
126                cachedGraph = null;
127        }
128        
129        public boolean equals(Object obj) {
130                if (this == obj)
131                        return true;
132                if (obj == null)
133                        return false;
134                if (getClass() != obj.getClass())
135                        return false;
136                return obj.toString().equals(this.toString());
137        }
138        
139        public int hashCode() {
140                return 217 + (null == toString() ? 0 : toString().hashCode());
141        }
142        
143        public String toString() {
144                final StringBuilder sb = new StringBuilder();
145                sb.append("    pseudoproj ");
146                sb.append("id:");sb.append(idName);
147                sb.append(' ');
148                sb.append("task:");sb.append(taskName);
149                sb.append(' ');
150                sb.append("source:");sb.append(sourceName);
151                sb.append(' ');
152                sb.append("target:");sb.append(targetName);
153                return sb.toString();
154        }
155}