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