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