001 package org.maltparser.core.syntaxgraph;
002
003 import java.util.HashMap;
004 import java.util.HashSet;
005
006 import org.maltparser.core.config.ConfigurationDir;
007 import org.maltparser.core.exception.MaltChainedException;
008 import org.maltparser.core.flow.FlowChartInstance;
009 import org.maltparser.core.flow.FlowException;
010 import org.maltparser.core.flow.item.ChartItem;
011 import org.maltparser.core.flow.spec.ChartItemSpecification;
012 import org.maltparser.core.io.dataformat.DataFormatInstance;
013 import org.maltparser.core.io.dataformat.DataFormatManager;
014 import org.maltparser.core.io.dataformat.DataFormatSpecification.DataStructure;
015 import org.maltparser.core.io.dataformat.DataFormatSpecification.Dependency;
016 import org.maltparser.core.options.OptionManager;
017 import org.maltparser.core.symbol.SymbolTableHandler;
018 import org.maltparser.core.syntaxgraph.ds2ps.LosslessMapping;
019 /**
020 *
021 *
022 * @author Johan Hall
023 */
024 public class SyntaxGraphChartItem extends ChartItem {
025 private String idName;
026 private String structureName;
027 private String taskName;
028 private TokenStructure graph;
029
030 public SyntaxGraphChartItem() { super(); }
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("id")) {
037 idName = chartItemSpecification.getChartItemAttributes().get(key);
038 } else if (key.equals("structure")) {
039 structureName = chartItemSpecification.getChartItemAttributes().get(key);
040 } else if (key.equals("task")) {
041 taskName = chartItemSpecification.getChartItemAttributes().get(key);
042 }
043 }
044 if (idName == null) {
045 idName = getChartElement("graph").getAttributes().get("id").getDefaultValue();
046 } else if (structureName == null) {
047 structureName = getChartElement("graph").getAttributes().get("structure").getDefaultValue();
048 } else if (taskName == null) {
049 taskName = getChartElement("graph").getAttributes().get("task").getDefaultValue();
050 }
051 }
052
053 public int preprocess(int signal) throws MaltChainedException {
054 if (taskName.equals("create")) {
055 boolean phrase = false;
056 boolean dependency = false;
057 ConfigurationDir configDir = (ConfigurationDir)flowChartinstance.getFlowChartRegistry(org.maltparser.core.config.ConfigurationDir.class, idName);
058 DataFormatInstance dataFormatInstance = null;
059 DataFormatManager dataFormatManager = configDir.getDataFormatManager();
060 // DataFormatManager dataFormatManager = flowChartinstance.getDataFormatManager();
061 SymbolTableHandler symbolTables = configDir.getSymbolTables();
062 // SymbolTableHandler symbolTables = flowChartinstance.getSymbolTables();
063
064 HashMap<String, DataFormatInstance> dataFormatInstances = configDir.getDataFormatInstances();
065 // HashMap<String, DataFormatInstance> dataFormatInstances = flowChartinstance.getDataFormatInstances();
066 for (String key : dataFormatInstances.keySet()) {
067 if (dataFormatInstances.get(key).getDataFormarSpec().getDataStructure() == DataStructure.PHRASE) {
068 phrase = true;
069 }
070 if (dataFormatInstances.get(key).getDataFormarSpec().getDataStructure() == DataStructure.DEPENDENCY) {
071 dependency = true;
072 dataFormatInstance = dataFormatInstances.get(key);
073 }
074 }
075
076 if (dependency == false && OptionManager.instance().getOptionValue(getOptionContainerIndex(), "config", "flowchart").toString().equals("learn")) {
077 dependency = true;
078 HashSet<Dependency> deps = dataFormatManager.getInputDataFormatSpec().getDependencies();
079 String nullValueStategy = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "singlemalt", "null_value").toString();
080 String rootLabels = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "graph", "root_label").toString();
081 for (Dependency dep : deps) {
082 dataFormatInstance = dataFormatManager.getDataFormatSpec(dep.getDependentOn()).createDataFormatInstance(symbolTables, nullValueStategy, rootLabels);
083 dataFormatInstances.put(dataFormatManager.getOutputDataFormatSpec().getDataFormatName(), dataFormatInstance);
084 }
085 }
086
087 if (dependency == true && phrase == false) {
088 graph = new DependencyGraph(symbolTables);
089 flowChartinstance.addFlowChartRegistry(org.maltparser.core.syntaxgraph.DependencyStructure.class, structureName, graph);
090 } else if (dependency == true && phrase == true) {
091 graph = new MappablePhraseStructureGraph(symbolTables);
092 final DataFormatInstance inFormat = dataFormatInstances.get(dataFormatManager.getInputDataFormatSpec().getDataFormatName());
093 final DataFormatInstance outFormat = dataFormatInstances.get(dataFormatManager.getOutputDataFormatSpec().getDataFormatName());
094
095 if (inFormat != null && outFormat != null) {
096 LosslessMapping mapping = null;
097 if (inFormat.getDataFormarSpec().getDataStructure() == DataStructure.DEPENDENCY) {
098 mapping = new LosslessMapping(inFormat, outFormat);
099 } else {
100 mapping = new LosslessMapping(outFormat, inFormat);
101 }
102 if (inFormat.getDataFormarSpec().getDataStructure() == DataStructure.PHRASE) {
103 mapping.setHeadRules(OptionManager.instance().getOptionValue(getOptionContainerIndex(), "graph", "head_rules").toString());
104 }
105 ((MappablePhraseStructureGraph)graph).setMapping(mapping);
106 } else {
107 throw new FlowException("Couldn't determine the input and output data format. ");
108 }
109 flowChartinstance.addFlowChartRegistry(org.maltparser.core.syntaxgraph.DependencyStructure.class, structureName, graph);
110 flowChartinstance.addFlowChartRegistry(org.maltparser.core.syntaxgraph.PhraseStructure.class, structureName, graph);
111 } else if (dependency == false && phrase == true) {
112 graph = new PhraseStructureGraph(symbolTables);
113 flowChartinstance.addFlowChartRegistry(org.maltparser.core.syntaxgraph.PhraseStructure.class, structureName, graph);
114 } else {
115 graph = new Sentence(symbolTables);
116 }
117
118 if (dataFormatInstance != null) {
119 ((DependencyStructure)graph).setDefaultRootEdgeLabels(
120 OptionManager.instance().getOptionValue(getOptionContainerIndex(), "graph", "root_label").toString(),
121 dataFormatInstance.getDependencyEdgeLabelSymbolTables());
122 }
123 flowChartinstance.addFlowChartRegistry(org.maltparser.core.syntaxgraph.TokenStructure.class, structureName, graph);
124 }
125 return signal;
126 }
127
128 public int process(int signal) throws MaltChainedException {
129 return signal;
130 }
131
132 public int postprocess(int signal) throws MaltChainedException {
133 return signal;
134 }
135
136 public void terminate() throws MaltChainedException {
137 if (graph != null) {
138 graph.clear();
139 graph = null;
140 }
141 }
142
143 public boolean equals(Object obj) {
144 if (this == obj)
145 return true;
146 if (obj == null)
147 return false;
148 if (getClass() != obj.getClass())
149 return false;
150 return obj.toString().equals(this.toString());
151 }
152
153 public int hashCode() {
154 return 217 + (null == toString() ? 0 : toString().hashCode());
155 }
156
157 public String toString() {
158 StringBuilder sb = new StringBuilder();
159 sb.append(" graph ");
160 sb.append("id:");sb.append(idName);
161 sb.append(' ');
162 sb.append("task:");
163 sb.append(taskName);
164 sb.append(' ');
165 sb.append("structure:");
166 sb.append(structureName);
167 return sb.toString();
168 }
169 }