001 package org.maltparser.core.feature.spec; 002 003 import java.net.URL; 004 import java.util.ArrayList; 005 import java.util.HashMap; 006 import java.util.LinkedHashMap; 007 008 import org.maltparser.core.config.ConfigurationDir; 009 import org.maltparser.core.exception.MaltChainedException; 010 import org.maltparser.core.feature.FeatureException; 011 import org.maltparser.core.feature.spec.reader.FeatureSpecReader; 012 import org.maltparser.core.feature.spec.reader.ParReader; 013 import org.maltparser.core.helper.Util; 014 015 /** 016 * 017 * 018 * @author Johan Hall 019 */ 020 public class SpecificationModels { 021 private HashMap<URL, FeatureSpecReader> specReaderMap; 022 private HashMap<String, SpecificationModel> specModelMap; 023 private HashMap<Integer, SpecificationModel> specModelIntMap; 024 private LinkedHashMap<URL, ArrayList<SpecificationModel>> specModelKeyMap; 025 private ArrayList<SpecificationModel> currentSpecModelURL; 026 private int counter = 0; 027 private ConfigurationDir configDirectory; 028 // private Configuration configuration; 029 030 public SpecificationModels(ConfigurationDir configDirectory) throws MaltChainedException { 031 // public SpecificationModels(Configuration configuration) throws MaltChainedException { 032 setConfigDirectory(configDirectory); 033 specReaderMap = new HashMap<URL, FeatureSpecReader>(); 034 specModelMap = new HashMap<String, SpecificationModel>(); 035 specModelIntMap = new HashMap<Integer, SpecificationModel>(); 036 specModelKeyMap = new LinkedHashMap<URL, ArrayList<SpecificationModel>>(); 037 // setConfiguration(configuration); 038 } 039 040 public void add(int index, String featureSpec) throws MaltChainedException { 041 this.add(Integer.toString(index), "MAIN", featureSpec); 042 } 043 044 public void add(String specModelName, String featureSpec) throws MaltChainedException { 045 this.add(specModelName, "MAIN", featureSpec); 046 } 047 048 public void add(int index, String subModelName, String featureSpec) throws MaltChainedException { 049 this.add(Integer.toString(index), subModelName, featureSpec); 050 } 051 052 public void add(String specModelName, String subModelName, String featureSpec) throws MaltChainedException { 053 if (featureSpec == null) { throw new FeatureException("Feature specification is missing."); } 054 if (specModelName == null) {throw new FeatureException("Unknown feature model name."); } 055 if (subModelName == null) {throw new FeatureException("Unknown subfeature model name."); } 056 057 if (!specModelMap.containsKey(specModelName.toUpperCase())) { 058 SpecificationModel specModel = new SpecificationModel(specModelName.toUpperCase()); 059 specModelMap.put(specModelName.toUpperCase(), specModel); 060 currentSpecModelURL.add(specModel); 061 specModelIntMap.put(counter++, specModel); 062 } 063 specModelMap.get(specModelName.toUpperCase()).add(subModelName, featureSpec); 064 } 065 066 public int getNextIndex() { 067 return counter; 068 } 069 070 public void loadParReader(String specModelFileName, boolean malt04, String markingStrategy, String coveredRoot) throws MaltChainedException { 071 URL url = Util.findURL(specModelFileName); 072 if (url == null) { 073 int indexSlash = specModelFileName.lastIndexOf('/'); 074 if (indexSlash == -1) { 075 indexSlash = specModelFileName.lastIndexOf('\\'); 076 } 077 if (indexSlash == -1) { 078 url = Util.findURL(configDirectory.getFile(specModelFileName).getAbsolutePath()); 079 } else { 080 url = Util.findURL(configDirectory.getFile(specModelFileName.substring(indexSlash+1)).getAbsolutePath()); 081 } 082 } 083 loadParReader(url, malt04, markingStrategy, coveredRoot); 084 } 085 086 public void loadParReader(URL specModelURL, boolean malt04, String markingStrategy, String coveredRoot) throws MaltChainedException { 087 if (specModelURL == null) { 088 throw new FeatureException("The URL to the feature specification model is missing or not well-formed. "); 089 } 090 FeatureSpecReader specReader = null; 091 String urlSuffix = specModelURL.toString().substring(specModelURL.toString().length()-3); 092 urlSuffix = Character.toUpperCase(urlSuffix.charAt(0)) + urlSuffix.substring(1); 093 try { 094 Class<?> clazz = Class.forName("org.maltparser.core.feature.spec.reader."+urlSuffix+"Reader"); 095 specReader = (FeatureSpecReader)clazz.newInstance(); 096 } catch (InstantiationException e) { 097 throw new FeatureException("Could not initialize the feature specification reader to read the specification file: "+specModelURL.toString(), e); 098 } catch (IllegalAccessException e) { 099 throw new FeatureException("Could not initialize the feature specification reader to read the specification file: "+specModelURL.toString(), e); 100 } catch (ClassNotFoundException e) { 101 throw new FeatureException("Could not find the feature specification reader to read the specification file: "+specModelURL.toString(), e); 102 } 103 specReaderMap.put(specModelURL, specReader); 104 105 if (specReader instanceof ParReader) { 106 if (malt04) { 107 ((ParReader)specReader).setMalt04Emulation(true); 108 } 109 110 if (markingStrategy.equalsIgnoreCase("head") || markingStrategy.equalsIgnoreCase("path") || markingStrategy.equalsIgnoreCase("head+path")) { 111 ((ParReader)specReader).setPplifted(true); 112 } 113 if (markingStrategy.equalsIgnoreCase("path") || markingStrategy.equalsIgnoreCase("head+path")) { 114 ((ParReader)specReader).setPppath(true); 115 } 116 if (!coveredRoot.equalsIgnoreCase("none")) { 117 ((ParReader)specReader).setPpcoveredRoot(true); 118 } 119 } 120 currentSpecModelURL = new ArrayList<SpecificationModel>(); 121 specModelKeyMap.put(specModelURL, currentSpecModelURL); 122 specReader.load(specModelURL, this); 123 } 124 125 public void load(String specModelFileName) throws MaltChainedException { 126 URL url = Util.findURL(specModelFileName); 127 if (url == null) { 128 int indexSlash = specModelFileName.lastIndexOf('/'); 129 if (indexSlash == -1) { 130 indexSlash = specModelFileName.lastIndexOf('\\'); 131 } 132 if (indexSlash == -1) { 133 url = Util.findURL(configDirectory.getFile(specModelFileName).getAbsolutePath()); 134 } else { 135 url = Util.findURL(configDirectory.getFile(specModelFileName.substring(indexSlash+1)).getAbsolutePath()); 136 } 137 } 138 load(url); 139 } 140 141 142 143 public void load(URL specModelURL) throws MaltChainedException { 144 if (specModelURL == null) { 145 throw new FeatureException("The URL to the feature specification model is missing or not well-formed. "); 146 } 147 FeatureSpecReader specReader = null; 148 String urlSuffix = specModelURL.toString().substring(specModelURL.toString().length()-3); 149 urlSuffix = Character.toUpperCase(urlSuffix.charAt(0)) + urlSuffix.substring(1); 150 try { 151 Class<?> clazz = Class.forName("org.maltparser.core.feature.spec.reader."+urlSuffix+"Reader"); 152 specReader = (FeatureSpecReader)clazz.newInstance(); 153 } catch (InstantiationException e) { 154 throw new FeatureException("Could not initialize the feature specification reader to read the specification file: "+specModelURL.toString(), e); 155 } catch (IllegalAccessException e) { 156 throw new FeatureException("Could not initialize the feature specification reader to read the specification file: "+specModelURL.toString(), e); 157 } catch (ClassNotFoundException e) { 158 throw new FeatureException("Could not find the feature specification reader to read the specification file: "+specModelURL.toString(), e); 159 } 160 specReaderMap.put(specModelURL, specReader); 161 162 // if (specReader instanceof ParReader) { 163 // if ((Boolean)configuration.getOptionValue("malt0.4", "behavior").equals(true)) { 164 // ((ParReader)specReader).setMalt04Emulation(true); 165 // } 166 // if (configuration.getOptionValueString("singlemalt", "parsing_algorithm").equals("covnonproj") || 167 // configuration.getOptionValueString("singlemalt", "parsing_algorithm").equals("covproj")) { 168 // ((ParReader)specReader).setCovington(true); 169 // } 170 // String markingStrategy = configuration.getOptionValue("pproj", "marking_strategy").toString().trim(); 171 // String coveredRoot = configuration.getOptionValue("pproj", "covered_root").toString().trim(); 172 // 173 // if (markingStrategy.equalsIgnoreCase("head") || markingStrategy.equalsIgnoreCase("path") || markingStrategy.equalsIgnoreCase("head+path")) { 174 // ((ParReader)specReader).setPplifted(true); 175 // } 176 // if (markingStrategy.equalsIgnoreCase("path") || markingStrategy.equalsIgnoreCase("head+path")) { 177 // ((ParReader)specReader).setPppath(true); 178 // } 179 // if (!coveredRoot.equalsIgnoreCase("none")) { 180 // ((ParReader)specReader).setPpcoveredRoot(true); 181 // } 182 // } 183 currentSpecModelURL = new ArrayList<SpecificationModel>(); 184 specModelKeyMap.put(specModelURL, currentSpecModelURL); 185 specReader.load(specModelURL, this); 186 } 187 188 public SpecificationModel getSpecificationModel(String specModelURL, int specModelUrlIndex) throws MaltChainedException { 189 URL url = Util.findURL(specModelURL); 190 if (url == null) { 191 int indexSlash = specModelURL.lastIndexOf('/'); 192 if (indexSlash == -1) { 193 indexSlash = specModelURL.lastIndexOf('\\'); 194 } 195 if (indexSlash == -1) { 196 url = Util.findURL(configDirectory.getFile(specModelURL).getAbsolutePath()); 197 } else { 198 url = Util.findURL(configDirectory.getFile(specModelURL.substring(indexSlash+1)).getAbsolutePath()); 199 } 200 } 201 return specModelKeyMap.get(url).get(specModelUrlIndex); 202 } 203 204 205 // public Configuration getConfiguration() { 206 // return configuration; 207 // } 208 // 209 // public void setConfiguration(Configuration configuration) { 210 // this.configuration = configuration; 211 // } 212 213 public ConfigurationDir getConfigDirectory() { 214 return configDirectory; 215 } 216 217 public void setConfigDirectory(ConfigurationDir configDirectory) { 218 this.configDirectory = configDirectory; 219 } 220 221 public String toString() { 222 StringBuilder sb = new StringBuilder(); 223 224 for (URL url : specModelKeyMap.keySet()) { 225 for (int i = 0; i < specModelKeyMap.get(url).size(); i++) { 226 sb.append(url.toString()); 227 sb.append(':'); 228 sb.append(i); 229 sb.append('\n'); 230 sb.append(specModelKeyMap.get(url).get(i).toString()); 231 } 232 } 233 return sb.toString(); 234 } 235 }