001package org.maltparser.core.lw.parser; 002 003import java.io.File; 004 005import java.io.IOException; 006import java.io.InputStream; 007import java.io.InputStreamReader; 008import java.io.OutputStreamWriter; 009import java.lang.reflect.InvocationTargetException; 010import java.net.URL; 011 012import org.maltparser.core.config.ConfigurationException; 013import org.maltparser.core.exception.MaltChainedException; 014import org.maltparser.core.feature.FeatureModelManager; 015import org.maltparser.core.io.dataformat.DataFormatInstance; 016import org.maltparser.core.options.OptionManager; 017import org.maltparser.core.propagation.PropagationManager; 018import org.maltparser.core.symbol.SymbolTableHandler; 019import org.maltparser.core.syntaxgraph.DependencyStructure; 020import org.maltparser.parser.AbstractParserFactory; 021import org.maltparser.parser.DependencyParserConfig; 022 023/** 024 * A lightweight version of org.maltparser.parser.SingleMalt. This class can only perform parsing and is used by 025 * the concurrent MaltParser model. 026 * 027 * @author Johan Hall 028 * 029 */ 030public final class LWSingleMalt implements DependencyParserConfig { 031 public final static Class<?>[] paramTypes = { org.maltparser.parser.DependencyParserConfig.class }; 032 private final McoModel mcoModel; 033 private final int optionContainerIndex; 034 private final DataFormatInstance dataFormatInstance; 035 private final PropagationManager propagationManager; 036 private final FeatureModelManager featureModelManager; 037 private final AbstractParserFactory parserFactory; 038 private final String decisionSettings; 039 private final int kBestSize; 040 private final String classitem_separator; 041 private final URL featureModelURL; 042 private final String dataSplitColumn; 043 private final String dataSplitStructure; 044 private final boolean excludeNullValues; 045 private final LWDecisionModel decisionModel; 046 047 public LWSingleMalt(int containerIndex, DataFormatInstance dataFormatInstance, McoModel _mcoModel, PropagationManager _propagationManager, FeatureModelManager _featureModelManager) throws MaltChainedException { 048 this.optionContainerIndex = containerIndex; 049 this.mcoModel = _mcoModel; 050 this.dataFormatInstance = dataFormatInstance; 051 this.propagationManager = _propagationManager; 052 this.featureModelManager = _featureModelManager; 053 this.parserFactory = makeParserFactory(); 054 this.decisionSettings = getOptionValue("guide", "decision_settings").toString().trim(); 055 this.kBestSize = ((Integer)getOptionValue("guide", "kbest")).intValue(); 056 this.classitem_separator = getOptionValue("guide", "classitem_separator").toString().trim(); 057 this.featureModelURL = getConfigFileEntryURL(getOptionValue("guide", "features").toString().trim()); 058 this.dataSplitColumn = getOptionValue("guide", "data_split_column").toString().trim(); 059 this.dataSplitStructure = getOptionValue("guide", "data_split_structure").toString().trim(); 060 this.excludeNullValues = getOptionValue("singlemalt", "null_value").toString().equalsIgnoreCase("none"); 061 this.decisionModel = new LWDecisionModel(mcoModel, excludeNullValues, getOptionValueString("guide","learner")); 062 } 063 064 private AbstractParserFactory makeParserFactory() throws MaltChainedException { 065 Class<?> clazz = (Class<?>)getOptionValue("singlemalt", "parsing_algorithm"); 066 try { 067 Object[] arguments = { this }; 068 return (AbstractParserFactory)clazz.getConstructor(paramTypes).newInstance(arguments); 069 } catch (NoSuchMethodException e) { 070 throw new ConfigurationException("The parser factory '"+clazz.getName()+"' cannot be initialized. ", e); 071 } catch (InstantiationException e) { 072 throw new ConfigurationException("The parser factory '"+clazz.getName()+"' cannot be initialized. ", e); 073 } catch (IllegalAccessException e) { 074 throw new ConfigurationException("The parser factory '"+clazz.getName()+"' cannot be initialized. ", e); 075 } catch (InvocationTargetException e) { 076 throw new ConfigurationException("The parser factory '"+clazz.getName()+"' cannot be initialized. ", e); 077 } 078 } 079 public FeatureModelManager getFeatureModelManager() { 080 return featureModelManager; 081 } 082 083 public AbstractParserFactory getParserFactory() { 084 return parserFactory; 085 } 086 087 public void parse(DependencyStructure graph) throws MaltChainedException { 088 if (graph.hasTokens()) { 089 LWDeterministicParser parser = new LWDeterministicParser(this, graph.getSymbolTables()); 090 parser.parse(graph); 091 } 092 } 093 094 public void oracleParse(DependencyStructure goldGraph, DependencyStructure oracleGraph) throws MaltChainedException {} 095 096 public void terminate(Object[] arguments) throws MaltChainedException {} 097 098 public boolean isLoggerInfoEnabled() { 099 return false; 100 } 101 public boolean isLoggerDebugEnabled() { 102 return false; 103 } 104 public void logErrorMessage(String message) {} 105 public void logInfoMessage(String message) {} 106 public void logInfoMessage(char character) {} 107 public void logDebugMessage(String message) {} 108 public void writeInfoToConfigFile(String message) throws MaltChainedException {} 109 110 public OutputStreamWriter getOutputStreamWriter(String fileName) throws MaltChainedException { 111 return null; 112 } 113 114 public OutputStreamWriter getAppendOutputStreamWriter(String fileName) throws MaltChainedException { 115 return null; 116 } 117 118 public InputStreamReader getInputStreamReader(String fileName) throws MaltChainedException { 119 try { 120 return mcoModel.getInputStreamReader(fileName, "UTF-8"); 121 } catch (IOException e) { 122 throw new ConfigurationException("Couldn't read file "+fileName+" from mco-file ", e); 123 } 124 } 125 126 public InputStream getInputStreamFromConfigFileEntry(String fileName) throws MaltChainedException { 127 try { 128 return mcoModel.getInputStream(fileName); 129 } catch (IOException e) { 130 throw new ConfigurationException("Couldn't read file "+fileName+" from mco-file ", e); 131 } 132 } 133 134 public URL getConfigFileEntryURL(String fileName) throws MaltChainedException { 135 try { 136 return mcoModel.getMcoEntryURL(fileName); 137 } catch (IOException e) { 138 throw new ConfigurationException("Couldn't read file "+fileName+" from mco-file ", e); 139 } 140 } 141 142 public Object getConfigFileEntryObject(String fileName) throws MaltChainedException { 143 return mcoModel.getMcoEntryObject(fileName); 144 } 145 146 public String getConfigFileEntryString(String fileName) throws MaltChainedException { 147 return mcoModel.getMcoEntryString(fileName); 148 } 149 150 public File getFile(String fileName) throws MaltChainedException { 151 return new File(System.getProperty("user.dir")+File.separator+fileName); 152 } 153 154 public Object getOptionValue(String optiongroup, String optionname) throws MaltChainedException { 155 return OptionManager.instance().getOptionValue(optionContainerIndex, optiongroup, optionname); 156 } 157 158 public String getOptionValueString(String optiongroup, String optionname) throws MaltChainedException { 159 return OptionManager.instance().getOptionValueString(optionContainerIndex, optiongroup, optionname); 160 } 161 162 public SymbolTableHandler getSymbolTables() { 163 return null; 164 } 165 166 public DataFormatInstance getDataFormatInstance() { 167 return dataFormatInstance; 168 } 169 170 public PropagationManager getPropagationManager() { 171 return propagationManager; 172 } 173 174 public String getDecisionSettings() { 175 return decisionSettings; 176 } 177 178 public int getkBestSize() { 179 return kBestSize; 180 } 181 182 public String getClassitem_separator() { 183 return classitem_separator; 184 } 185 186 public URL getFeatureModelURL() { 187 return featureModelURL; 188 } 189 190 public String getDataSplitColumn() { 191 return dataSplitColumn; 192 } 193 194 public String getDataSplitStructure() { 195 return dataSplitStructure; 196 } 197 198 public boolean isExcludeNullValues() { 199 return excludeNullValues; 200 } 201 202 public LWDecisionModel getDecisionModel() { 203 return decisionModel; 204 } 205}