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