001 package org.maltparser.parser;
002
003 import java.util.Set;
004 import java.util.regex.Pattern;
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.item.ChartItem;
010 import org.maltparser.core.flow.spec.ChartItemSpecification;
011 import org.maltparser.core.io.dataformat.DataFormatInstance;
012 import org.maltparser.core.io.dataformat.DataFormatManager;
013 import org.maltparser.core.io.dataformat.DataFormatSpecification.DataStructure;
014 import org.maltparser.core.io.dataformat.DataFormatSpecification.Dependency;
015 import org.maltparser.core.options.OptionManager;
016 import org.maltparser.core.syntaxgraph.DependencyStructure;
017 import org.maltparser.core.syntaxgraph.MappablePhraseStructureGraph;
018 /**
019 * @author Johan Hall
020 *
021 */
022 public class SingleMaltChartItem extends ChartItem {
023 private SingleMalt singleMalt;
024 private String idName;
025 private String targetName;
026 private String sourceName;
027 private String modeName;
028 private String taskName;
029 private DependencyStructure cachedSourceGraph = null;
030 private DependencyStructure cachedTargetGraph = null;
031
032
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("mode")) {
043 modeName = chartItemSpecification.getChartItemAttributes().get(key);
044 } else if (key.equals("task")) {
045 taskName = chartItemSpecification.getChartItemAttributes().get(key);
046 } else if (key.equals("id")) {
047 idName = chartItemSpecification.getChartItemAttributes().get(key);
048 }
049 }
050 if (targetName == null) {
051 targetName = getChartElement("singlemalt").getAttributes().get("target").getDefaultValue();
052 } else if (sourceName == null) {
053 sourceName = getChartElement("singlemalt").getAttributes().get("source").getDefaultValue();
054 } else if (modeName == null) {
055 modeName = getChartElement("singlemalt").getAttributes().get("mode").getDefaultValue();
056 } else if (taskName == null) {
057 taskName = getChartElement("singlemalt").getAttributes().get("task").getDefaultValue();
058 } else if (idName == null) {
059 idName = getChartElement("singlemalt").getAttributes().get("id").getDefaultValue();
060 }
061
062 singleMalt = (SingleMalt)flowChartinstance.getFlowChartRegistry(org.maltparser.parser.SingleMalt.class, idName);
063 if (singleMalt == null) {
064 singleMalt = new SingleMalt();
065 flowChartinstance.addFlowChartRegistry(org.maltparser.parser.SingleMalt.class, idName, singleMalt);
066 flowChartinstance.addFlowChartRegistry(org.maltparser.core.config.Configuration.class, idName, singleMalt);
067
068 }
069 }
070
071
072 public int preprocess(int signal) throws MaltChainedException {
073 if (taskName.equals("init")) {
074 if (modeName.equals("learn") || modeName.equals("parse")) {
075 OptionManager.instance().overloadOptionValue(getOptionContainerIndex(), "singlemalt", "mode", modeName);
076 ConfigurationDir configDir = (ConfigurationDir)flowChartinstance.getFlowChartRegistry(org.maltparser.core.config.ConfigurationDir.class, idName);
077 DataFormatManager dataFormatManager = configDir.getDataFormatManager();
078
079 if (modeName.equals("learn")) {
080 DataFormatInstance dataFormatInstance = null;
081 if (dataFormatManager.getInputDataFormatSpec().getDataStructure() == DataStructure.PHRASE) {
082 Set<Dependency> deps = dataFormatManager.getInputDataFormatSpec().getDependencies();
083 String nullValueStrategy = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "singlemalt", "null_value").toString();
084
085 for (Dependency dep : dataFormatManager.getInputDataFormatSpec().getDependencies()) {
086 dataFormatInstance = dataFormatManager.getDataFormatSpec(dep.getDependentOn()).createDataFormatInstance(configDir.getSymbolTables(), nullValueStrategy);
087 configDir.addDataFormatInstance(dataFormatManager.getOutputDataFormatSpec().getDataFormatName(), dataFormatInstance);
088 }
089
090 String decisionSettings = OptionManager.instance().getOptionValue(getOptionContainerIndex(),"guide", "decision_settings").toString().trim();
091 StringBuilder newDecisionSettings = new StringBuilder();
092 if (!Pattern.matches(".*A\\.HEADREL.*", decisionSettings)) {
093 newDecisionSettings.append("+A.HEADREL");
094 }
095 if (!Pattern.matches(".*A\\.PHRASE.*", decisionSettings)) {
096 newDecisionSettings.append("+A.PHRASE");
097 }
098 if (!Pattern.matches(".*A\\.ATTACH.*", decisionSettings)) {
099 newDecisionSettings.append("+A.ATTACH");
100 }
101 if (newDecisionSettings.length() > 0) {
102 OptionManager.instance().overloadOptionValue(getOptionContainerIndex(), "guide", "decision_settings", decisionSettings+newDecisionSettings.toString());
103 }
104 } else {
105 dataFormatInstance = configDir.getDataFormatInstance(dataFormatManager.getInputDataFormatSpec().getDataFormatName());
106 }
107 singleMalt.initialize(getOptionContainerIndex(), dataFormatInstance, configDir, SingleMalt.LEARN);
108 } else if (modeName.equals("parse")) {
109 singleMalt.initialize(getOptionContainerIndex(),
110 configDir.getDataFormatInstance(dataFormatManager.getInputDataFormatSpec().getDataFormatName()), configDir, SingleMalt.PARSE);
111 } else {
112 return ChartItem.TERMINATE;
113 }
114 } else {
115 return ChartItem.TERMINATE;
116 }
117 }
118 return signal;
119 }
120
121 public int process(int signal) throws MaltChainedException {
122 if (taskName.equals("process")) {
123 if (cachedSourceGraph == null) {
124 cachedSourceGraph = (DependencyStructure)flowChartinstance.getFlowChartRegistry(org.maltparser.core.syntaxgraph.DependencyStructure.class, sourceName);
125 }
126 if (cachedTargetGraph == null) {
127 cachedTargetGraph = (DependencyStructure)flowChartinstance.getFlowChartRegistry(org.maltparser.core.syntaxgraph.DependencyStructure.class, targetName);
128 }
129 if (modeName.equals("learn")) {
130 singleMalt.oracleParse(cachedSourceGraph, cachedTargetGraph);
131 } else if (modeName.equals("parse")) {
132 singleMalt.parse(cachedSourceGraph);
133 // if (cachedSourceGraph instanceof MappablePhraseStructureGraph) {
134 // System.out.println("MappablePhraseStructureGraph");
135 // ((MappablePhraseStructureGraph)cachedSourceGraph).getMapping().connectUnattachedSpines((MappablePhraseStructureGraph)cachedSourceGraph);
136 // }
137 }
138 }
139 return signal;
140 }
141
142 public int postprocess(int signal) throws MaltChainedException {
143 if (taskName.equals("train") && singleMalt.getGuide() != null) {
144 singleMalt.getGuide().noMoreInstances();
145 } else if (taskName.equals("train") && singleMalt.getGuide() == null) {
146 singleMalt.train();
147 }
148 return signal;
149 }
150
151 public void terminate() throws MaltChainedException {
152 if (flowChartinstance.getFlowChartRegistry(org.maltparser.parser.SingleMalt.class, idName) != null) {
153 singleMalt.terminate(null);
154 flowChartinstance.removeFlowChartRegistry(org.maltparser.parser.SingleMalt.class, idName);
155 flowChartinstance.removeFlowChartRegistry(org.maltparser.core.config.Configuration.class, idName);
156 singleMalt = null;
157 } else {
158 singleMalt = null;
159 }
160 cachedSourceGraph = null;
161 cachedTargetGraph = null;
162 }
163
164 public SingleMalt getSingleMalt() {
165 return singleMalt;
166 }
167
168 public void setSingleMalt(SingleMalt singleMalt) {
169 this.singleMalt = singleMalt;
170 }
171
172 public String getTargetName() {
173 return targetName;
174 }
175
176 public void setTargetName(String targetName) {
177 this.targetName = targetName;
178 }
179
180 public String getSourceName() {
181 return sourceName;
182 }
183
184 public void setSourceName(String sourceName) {
185 this.sourceName = sourceName;
186 }
187
188 public boolean equals(Object obj) {
189 if (this == obj)
190 return true;
191 if (obj == null)
192 return false;
193 if (getClass() != obj.getClass())
194 return false;
195 return obj.toString().equals(this.toString());
196 }
197
198 public int hashCode() {
199 return 217 + (null == toString() ? 0 : toString().hashCode());
200 }
201
202 public String toString() {
203 StringBuilder sb = new StringBuilder();
204 sb.append(" singlemalt ");
205 sb.append("id:");sb.append(idName);
206 sb.append(' ');
207 sb.append("mode:");sb.append(modeName);
208 sb.append(' ');
209 sb.append("task:");sb.append(taskName);
210 sb.append(' ');
211 sb.append("source:");sb.append(sourceName);
212 sb.append(' ');
213 sb.append("target:");sb.append(targetName);
214 return sb.toString();
215 }
216 }