/** * Saves the feature divide model settings .fsm file. * * @throws MaltChainedException */ protected void save() throws MaltChainedException { try { final BufferedWriter out = new BufferedWriter( getGuide() .getConfiguration() .getConfigurationDir() .getOutputStreamWriter(getModelName() + ".dsm")); out.write(masterModel.getIndex() + "\t" + masterModel.getFrequency() + "\n"); if (divideModels != null) { for (AtomicModel divideModel : divideModels.values()) { out.write(divideModel.getIndex() + "\t" + divideModel.getFrequency() + "\n"); } } out.close(); } catch (IOException e) { throw new GuideException( "Could not write to the guide model settings file '" + getModelName() + ".dsm" + "', when " + "saving the guide model settings to file. ", e); } }
public boolean predict(SingleDecision decision) throws MaltChainedException { if (getGuide().getGuideMode() == ClassifierGuide.GuideMode.BATCH) { throw new GuideException("Can only predict during parsing. "); } else if (!(divideFeature.getFeatureValue() instanceof SingleFeatureValue)) { throw new GuideException("The divide feature does not have a single value. "); } // divideFeature.update(); if (divideModels != null && divideModels.containsKey( ((SingleFeatureValue) divideFeature.getFeatureValue()).getCode())) { return divideModels .get(((SingleFeatureValue) divideFeature.getFeatureValue()).getCode()) .predict(decision); } else if (masterModel != null && masterModel.getFrequency() > 0) { return masterModel.predict(decision); } else { getGuide() .getConfiguration() .getConfigLogger() .info( "Could not predict the next parser decision because there is " + "no divide or master model that covers the divide value '" + ((SingleFeatureValue) divideFeature.getFeatureValue()).getCode() + "', as default" + " class code '1' is used. "); decision.addDecision(1); // default prediction // classCodeTable.getEmptyKBestList().addKBestItem(1); } return true; }
public void terminate() throws MaltChainedException { if (divideModels != null) { for (AtomicModel divideModel : divideModels.values()) { divideModel.terminate(); } } if (masterModel != null) { masterModel.terminate(); } }
public void finalizeSentence(DependencyStructure dependencyGraph) throws MaltChainedException { // if (getGuide().getGuideMode() == Guide.GuideMode.CLASSIFY) { // throw new GuideException("Can only finish sentence during learning. "); // } if (divideModels != null) { for (AtomicModel divideModel : divideModels.values()) { divideModel.finalizeSentence(dependencyGraph); } } else { throw new GuideException("The feature divide models cannot be found. "); } }
public void noMoreInstances() throws MaltChainedException { // if (getGuide().getGuideMode() == Guide.GuideMode.CLASSIFY) { // throw new GuideException("Can only finish all data during learning. "); // } if (divideModels != null) { divideFeature.updateCardinality(); for (Integer index : divideModels.keySet()) { divideModels.get(index).noMoreInstances(); } final TreeSet<Integer> removeSet = new TreeSet<Integer>(); for (Integer index : divideModels.keySet()) { if (divideModels.get(index).getFrequency() <= divideThreshold) { divideModels .get(index) .moveAllInstances(masterModel, divideFeature, divideFeatureIndexVector); removeSet.add(index); } } for (Integer index : removeSet) { divideModels.remove(index); } masterModel.noMoreInstances(); } else { throw new GuideException("The feature divide models cannot be found. "); } }
private AtomicModel getAtomicModel() throws MaltChainedException { if (getGuide().getGuideMode() == ClassifierGuide.GuideMode.BATCH) { throw new GuideException("Can only predict during parsing. "); } else if (!(divideFeature.getFeatureValue() instanceof SingleFeatureValue)) { throw new GuideException("The divide feature does not have a single value. "); } if (divideModels != null && divideModels.containsKey( ((SingleFeatureValue) divideFeature.getFeatureValue()).getCode())) { return divideModels.get(((SingleFeatureValue) divideFeature.getFeatureValue()).getCode()); } else if (masterModel != null && masterModel.getFrequency() > 0) { return masterModel; } else { getGuide() .getConfiguration() .getConfigLogger() .info( "Could not predict the next parser decision because there is " + "no divide or master model that covers the divide value '" + ((SingleFeatureValue) divideFeature.getFeatureValue()).getCode() + "', as default" + " class code '1' is used. "); } return null; }
public void train() throws MaltChainedException { for (AtomicModel divideModel : divideModels.values()) { divideModel.train(); } masterModel.train(); save(); for (AtomicModel divideModel : divideModels.values()) { divideModel.terminate(); } masterModel.terminate(); }
/** * Moves all instance from this atomic model into the destination atomic model and add the divide * feature. This method is used by the feature divide model to sum up all model below a certain * threshold. * * @param model the destination atomic model * @param divideFeature the divide feature * @param divideFeatureIndexVector the divide feature index vector * @throws MaltChainedException */ public void moveAllInstances( AtomicModel model, FeatureFunction divideFeature, ArrayList<Integer> divideFeatureIndexVector) throws MaltChainedException { if (method == null) { throw new GuideException("The learner cannot be found. "); } else if (model == null) { throw new GuideException("The guide model cannot be found. "); } else if (divideFeature == null) { throw new GuideException("The divide feature cannot be found. "); } else if (divideFeatureIndexVector == null) { throw new GuideException("The divide feature index vector cannot be found. "); } ((Modifiable) divideFeature).setFeatureValue(index); method.moveAllInstances(model.getMethod(), divideFeature, divideFeatureIndexVector); method.terminate(); method = null; }
/** * Loads the feature divide model settings .fsm file. * * @throws MaltChainedException */ protected void load() throws MaltChainedException { try { final BufferedReader in = new BufferedReader( getGuide() .getConfiguration() .getConfigurationDir() .getInputStreamReader(getModelName() + ".dsm")); final Pattern tabPattern = Pattern.compile("\t"); while (true) { String line = in.readLine(); if (line == null) break; String[] cols = tabPattern.split(line); if (cols.length != 2) { throw new GuideException(""); } int code = -1; int freq = 0; try { code = Integer.parseInt(cols[0]); freq = Integer.parseInt(cols[1]); } catch (NumberFormatException e) { throw new GuideException( "Could not convert a string value into an integer value when loading the feature divide model settings (.fsm). ", e); } if (code == -1) { masterModel = new AtomicModel(-1, masterFeatureVector, this); masterModel.setFrequency(freq); } else if (divideModels != null) { divideModels.put(code, new AtomicModel(code, divideFeatureVector, this)); divideModels.get(code).setFrequency(freq); } setFrequency(getFrequency() + freq); } in.close(); } catch (IOException e) { throw new GuideException( "Could not read from the guide model settings file '" + getModelName() + ".dsm" + "', when " + "loading the guide model settings. ", e); } }