001    package org.maltparser.transform.pseudo;
002    
003    
004    import org.maltparser.core.config.ConfigurationDir;
005    import org.maltparser.core.exception.MaltChainedException;
006    import org.maltparser.core.flow.FlowChartInstance;
007    import org.maltparser.core.flow.item.ChartItem;
008    import org.maltparser.core.flow.spec.ChartItemSpecification;
009    import org.maltparser.core.helper.SystemLogger;
010    import org.maltparser.core.io.dataformat.DataFormatInstance;
011    import org.maltparser.core.options.OptionManager;
012    import org.maltparser.core.syntaxgraph.DependencyStructure;
013    import org.maltparser.core.syntaxgraph.TokenStructure;
014    /**
015    *
016    *
017    * @author Johan Hall
018    */
019    public 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                            
078                            if (!marking_strategy.equalsIgnoreCase("none") || !covered_root.equalsIgnoreCase("none")) { 
079                                    pproj.initialize(marking_strategy, covered_root, lifting_order, SystemLogger.logger(), dataFormatInstance);
080                            }
081                            if (!marking_strategy.equalsIgnoreCase("none") || !covered_root.equalsIgnoreCase("none")) { 
082                                    pprojActive = true;
083                            }
084                    }
085                    return signal;
086            }
087            
088            public int process(int signal) throws MaltChainedException {
089                    if (cachedGraph == null) {
090                            marking_strategy = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "pproj", "marking_strategy").toString().trim();
091                            covered_root = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "pproj", "covered_root").toString().trim();
092                            lifting_order = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "pproj", "lifting_order").toString().trim();
093    
094                            cachedGraph = (TokenStructure)flowChartinstance.getFlowChartRegistry(org.maltparser.core.syntaxgraph.TokenStructure.class, sourceName);
095                            if (!marking_strategy.equalsIgnoreCase("none") || !covered_root.equalsIgnoreCase("none")) { 
096                                    pprojActive = true;
097                            }
098                    }
099                    
100                    if (pprojActive && cachedGraph instanceof DependencyStructure) {
101                            if (taskName.equals("proj")) {
102                                            pproj.projectivize((DependencyStructure)cachedGraph);
103                            } else if (taskName.equals("merge")) {
104                                            pproj.mergeArclabels((DependencyStructure)cachedGraph);
105                            } else if (taskName.equals("deproj")) {
106                                            pproj.deprojectivize((DependencyStructure)cachedGraph);
107                            } else if (taskName.equals("split")) {
108                                            pproj.splitArclabels((DependencyStructure)cachedGraph);
109                            }
110                    }
111                    return signal;
112            }
113            
114            public int postprocess(int signal) throws MaltChainedException {
115                    return signal;
116            }
117    
118            
119            public void terminate() throws MaltChainedException {
120                    pproj = null; 
121                    pprojActive = false;
122                    cachedGraph = null;
123            }
124            
125            public boolean equals(Object obj) {
126                    if (this == obj)
127                            return true;
128                    if (obj == null)
129                            return false;
130                    if (getClass() != obj.getClass())
131                            return false;
132                    return obj.toString().equals(this.toString());
133            }
134            
135            public int hashCode() {
136                    return 217 + (null == toString() ? 0 : toString().hashCode());
137            }
138            
139            public String toString() {
140                    final StringBuilder sb = new StringBuilder();
141                    sb.append("    pseudoproj ");
142                    sb.append("id:");sb.append(idName);
143                    sb.append(' ');
144                    sb.append("task:");sb.append(taskName);
145                    sb.append(' ');
146                    sb.append("source:");sb.append(sourceName);
147                    sb.append(' ');
148                    sb.append("target:");sb.append(targetName);
149                    return sb.toString();
150            }
151    }