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.exception.MaltChainedException; 009 import org.maltparser.core.feature.FeatureException; 010 import org.maltparser.core.feature.spec.reader.FeatureSpecReader; 011 import org.maltparser.core.feature.spec.reader.ParReader; 012 013 /** 014 * 015 * 016 * @author Johan Hall 017 */ 018 public class SpecificationModels { 019 private HashMap<URL, FeatureSpecReader> specReaderMap; 020 private HashMap<String, SpecificationModel> specModelMap; 021 private HashMap<Integer, SpecificationModel> specModelIntMap; 022 private LinkedHashMap<URL, ArrayList<SpecificationModel>> specModelKeyMap; 023 private ArrayList<SpecificationModel> currentSpecModelURL; 024 private int counter = 0; 025 026 027 public SpecificationModels() throws MaltChainedException { 028 specReaderMap = new HashMap<URL, FeatureSpecReader>(); 029 specModelMap = new HashMap<String, SpecificationModel>(); 030 specModelIntMap = new HashMap<Integer, SpecificationModel>(); 031 specModelKeyMap = new LinkedHashMap<URL, ArrayList<SpecificationModel>>(); 032 } 033 034 public void add(int index, String featureSpec) throws MaltChainedException { 035 this.add(Integer.toString(index), "MAIN", featureSpec); 036 } 037 038 public void add(String specModelName, String featureSpec) throws MaltChainedException { 039 this.add(specModelName, "MAIN", featureSpec); 040 } 041 042 public void add(int index, String subModelName, String featureSpec) throws MaltChainedException { 043 this.add(Integer.toString(index), subModelName, featureSpec); 044 } 045 046 public void add(String specModelName, String subModelName, String featureSpec) throws MaltChainedException { 047 if (featureSpec == null) { throw new FeatureException("Feature specification is missing."); } 048 if (specModelName == null) {throw new FeatureException("Unknown feature model name."); } 049 if (subModelName == null) {throw new FeatureException("Unknown subfeature model name."); } 050 051 if (!specModelMap.containsKey(specModelName.toUpperCase())) { 052 SpecificationModel specModel = new SpecificationModel(specModelName.toUpperCase()); 053 specModelMap.put(specModelName.toUpperCase(), specModel); 054 currentSpecModelURL.add(specModel); 055 specModelIntMap.put(counter++, specModel); 056 } 057 specModelMap.get(specModelName.toUpperCase()).add(subModelName, featureSpec); 058 } 059 060 public int getNextIndex() { 061 return counter; 062 } 063 064 public void loadParReader(URL specModelURL, String markingStrategy, String coveredRoot) throws MaltChainedException { 065 if (specModelURL == null) { 066 throw new FeatureException("The URL to the feature specification model is missing or not well-formed. "); 067 } 068 FeatureSpecReader specReader = null; 069 String urlSuffix = specModelURL.toString().substring(specModelURL.toString().length()-3); 070 urlSuffix = Character.toUpperCase(urlSuffix.charAt(0)) + urlSuffix.substring(1); 071 try { 072 Class<?> clazz = Class.forName("org.maltparser.core.feature.spec.reader."+urlSuffix+"Reader"); 073 specReader = (FeatureSpecReader)clazz.newInstance(); 074 } catch (InstantiationException e) { 075 throw new FeatureException("Could not initialize the feature specification reader to read the specification file: "+specModelURL.toString(), e); 076 } catch (IllegalAccessException e) { 077 throw new FeatureException("Could not initialize the feature specification reader to read the specification file: "+specModelURL.toString(), e); 078 } catch (ClassNotFoundException e) { 079 throw new FeatureException("Could not find the feature specification reader to read the specification file: "+specModelURL.toString(), e); 080 } 081 specReaderMap.put(specModelURL, specReader); 082 083 if (specReader instanceof ParReader) { 084 if (markingStrategy.equalsIgnoreCase("head") || markingStrategy.equalsIgnoreCase("path") || markingStrategy.equalsIgnoreCase("head+path")) { 085 ((ParReader)specReader).setPplifted(true); 086 } 087 if (markingStrategy.equalsIgnoreCase("path") || markingStrategy.equalsIgnoreCase("head+path")) { 088 ((ParReader)specReader).setPppath(true); 089 } 090 if (!coveredRoot.equalsIgnoreCase("none")) { 091 ((ParReader)specReader).setPpcoveredRoot(true); 092 } 093 } 094 currentSpecModelURL = new ArrayList<SpecificationModel>(); 095 specModelKeyMap.put(specModelURL, currentSpecModelURL); 096 specReader.load(specModelURL, this); 097 } 098 099 public void load(URL specModelURL) throws MaltChainedException { 100 if (specModelURL == null) { 101 throw new FeatureException("The URL to the feature specification model is missing or not well-formed. "); 102 } 103 FeatureSpecReader specReader = null; 104 String urlSuffix = specModelURL.toString().substring(specModelURL.toString().length()-3); 105 urlSuffix = Character.toUpperCase(urlSuffix.charAt(0)) + urlSuffix.substring(1); 106 try { 107 Class<?> clazz = Class.forName("org.maltparser.core.feature.spec.reader."+urlSuffix+"Reader"); 108 specReader = (FeatureSpecReader)clazz.newInstance(); 109 } catch (InstantiationException e) { 110 throw new FeatureException("Could not initialize the feature specification reader to read the specification file: "+specModelURL.toString(), e); 111 } catch (IllegalAccessException e) { 112 throw new FeatureException("Could not initialize the feature specification reader to read the specification file: "+specModelURL.toString(), e); 113 } catch (ClassNotFoundException e) { 114 throw new FeatureException("Could not find the feature specification reader to read the specification file: "+specModelURL.toString(), e); 115 } 116 specReaderMap.put(specModelURL, specReader); 117 118 currentSpecModelURL = new ArrayList<SpecificationModel>(); 119 specModelKeyMap.put(specModelURL, currentSpecModelURL); 120 specReader.load(specModelURL, this); 121 } 122 123 public SpecificationModel getSpecificationModel(URL url, int specModelUrlIndex) { 124 return specModelKeyMap.get(url).get(specModelUrlIndex); 125 } 126 127 public String toString() { 128 StringBuilder sb = new StringBuilder(); 129 130 for (URL url : specModelKeyMap.keySet()) { 131 for (int i = 0; i < specModelKeyMap.get(url).size(); i++) { 132 sb.append(url.toString()); 133 sb.append(':'); 134 sb.append(i); 135 sb.append('\n'); 136 sb.append(specModelKeyMap.get(url).get(i).toString()); 137 } 138 } 139 return sb.toString(); 140 } 141 }