001    package org.maltparser.core.syntaxgraph;
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.options.OptionManager;
008    import org.maltparser.core.symbol.SymbolTable;
009    import org.maltparser.core.syntaxgraph.edge.Edge;
010    import org.maltparser.core.syntaxgraph.node.DependencyNode;
011    /**
012    *
013    *
014    * @author Johan Hall
015    */
016    public class CopyChartItem extends ChartItem {
017            private String idName;
018            private String targetName;
019            private String sourceName;
020            private String taskName;
021            private boolean usePartialTree;
022            
023            private TokenStructure cachedSource = null;
024            private TokenStructure cachedTarget = null;
025            
026            public CopyChartItem() {}
027            
028            public void initialize(FlowChartInstance flowChartinstance, ChartItemSpecification chartItemSpecification) throws MaltChainedException {
029                    super.initialize(flowChartinstance, chartItemSpecification);
030                    for (String key : chartItemSpecification.getChartItemAttributes().keySet()) {
031                            if (key.equals("id")) {
032                                    idName = chartItemSpecification.getChartItemAttributes().get(key);
033                            } else if (key.equals("target")) {
034                                    targetName = chartItemSpecification.getChartItemAttributes().get(key);
035                            } else if (key.equals("source")) {
036                                    sourceName = chartItemSpecification.getChartItemAttributes().get(key);
037                            }  else if (key.equals("task")) {
038                                    taskName = chartItemSpecification.getChartItemAttributes().get(key);
039                            }
040                    }
041                    if (idName == null) {
042                            idName = getChartElement("copy").getAttributes().get("id").getDefaultValue();
043                    } else if (targetName == null) {
044                            targetName = getChartElement("copy").getAttributes().get("target").getDefaultValue();
045                    } else if (sourceName == null) {
046                            sourceName = getChartElement("copy").getAttributes().get("source").getDefaultValue();
047                    } else if (taskName == null) {
048                            taskName = getChartElement("copy").getAttributes().get("task").getDefaultValue();
049                    }
050                    usePartialTree = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "singlemalt", "use_partial_tree").toString().equals("true");
051            }
052            
053            public int preprocess(int signal) throws MaltChainedException {
054                    return signal;
055            }
056            
057            public int process(int signal) throws MaltChainedException {
058                    if (taskName.equals("terminals")) {
059                            if (cachedSource == null) {
060                                    cachedSource = (TokenStructure)flowChartinstance.getFlowChartRegistry(org.maltparser.core.syntaxgraph.TokenStructure.class, sourceName);
061                            }
062                            if (cachedTarget == null) {
063                                    cachedTarget = (TokenStructure)flowChartinstance.getFlowChartRegistry(org.maltparser.core.syntaxgraph.TokenStructure.class, targetName);
064                            }
065                            copyTerminalStructure(cachedSource, cachedTarget);
066    //                      SystemLogger.logger().info("usePartialTree:" + usePartialTree);
067                            if (usePartialTree && cachedSource instanceof DependencyStructure && cachedTarget instanceof DependencyStructure) {
068                                    copyPartialDependencyStructure((DependencyStructure)cachedSource, (DependencyStructure)cachedTarget);
069                            }
070                    }
071                    return signal;
072            }
073            
074            public int postprocess(int signal) throws MaltChainedException {
075                    return signal;
076            }
077    
078            
079            public void terminate() throws MaltChainedException {
080                    cachedSource = null;
081                    cachedTarget = null;
082            }
083            
084            public void copyTerminalStructure(TokenStructure sourceGraph, TokenStructure targetGraph) throws MaltChainedException {
085                    targetGraph.clear();
086                    for (int index : sourceGraph.getTokenIndices()) {
087                            DependencyNode gnode = sourceGraph.getTokenNode(index);
088                            DependencyNode pnode = targetGraph.addTokenNode(gnode.getIndex());
089                            for (SymbolTable table : gnode.getLabelTypes()) {
090                                    pnode.addLabel(table, gnode.getLabelSymbol(table));
091                            }
092                    }
093            }
094            
095            public void copyPartialDependencyStructure(DependencyStructure sourceGraph, DependencyStructure targetGraph) throws MaltChainedException {
096                    SymbolTable partHead = cachedSource.getSymbolTables().getSymbolTable("PARTHEAD");
097                    SymbolTable partDeprel = cachedSource.getSymbolTables().getSymbolTable("PARTDEPREL");
098                    if (partHead == null || partDeprel == null) {
099                            return;
100                    }
101                    SymbolTable deprel = cachedTarget.getSymbolTables().getSymbolTable("DEPREL");
102                    for (int index : sourceGraph.getTokenIndices()) {
103                            DependencyNode snode = sourceGraph.getTokenNode(index);
104                            DependencyNode tnode = targetGraph.getTokenNode(index);
105                            if (snode != null && tnode != null) {
106                                    int spartheadindex = Integer.parseInt(snode.getLabelSymbol(partHead));
107                                    String spartdeprel = snode.getLabelSymbol(partDeprel);
108                                    if (spartheadindex > 0) {
109                                            Edge tedge = targetGraph.addDependencyEdge(spartheadindex, snode.getIndex());
110                                            tedge.addLabel(deprel, spartdeprel);
111                                    }
112                            }
113                    }
114            }
115            
116            public boolean equals(Object obj) {
117                    if (this == obj)
118                            return true;
119                    if (obj == null)
120                            return false;
121                    if (getClass() != obj.getClass())
122                            return false;
123                    return obj.toString().equals(this.toString());
124            }
125            
126            public int hashCode() {
127                    return 217 + (null == toString() ? 0 : toString().hashCode());
128            }
129            
130            public String toString() {
131                    final StringBuilder sb = new StringBuilder();
132                    sb.append("    copy ");
133                    sb.append("id:");sb.append(idName);
134                    sb.append(' ');
135                    sb.append("task:");
136                    sb.append(taskName);
137                    sb.append(' ');
138                    sb.append("source:");
139                    sb.append(sourceName);
140                    sb.append(' ');
141                    sb.append("target:");
142                    sb.append(targetName);
143                    return sb.toString();
144            }
145    }