/** * Invokes the train() of the learning method * * @throws MaltChainedException */ public void train() throws MaltChainedException { try { method.train(featureVector); method.terminate(); method = null; } catch (NullPointerException e) { throw new GuideException("The learner cannot be found. ", e); } }
public void finalizeSentence(DependencyStructure dependencyGraph) throws MaltChainedException { try { method.finalizeSentence(dependencyGraph); } catch (NullPointerException e) { throw new GuideException("The learner cannot be found. ", e); } }
public void noMoreInstances() throws MaltChainedException { try { method.noMoreInstances(); } catch (NullPointerException e) { throw new GuideException("The learner cannot be found. ", e); } }
public void addInstance(SingleDecision decision) throws MaltChainedException { try { method.addInstance(decision, featureVector); } catch (NullPointerException e) { throw new GuideException("The learner cannot be found. ", e); } }
/** * Constructs an atomic model. * * @param index the index of the atomic model (-1..n), where -1 is special value (used by a single * model or the master divide model) and n is number of divide models. * @param features the feature vector used by the atomic model. * @param parent the parent guide model. * @throws MaltChainedException */ public AtomicModel(int index, FeatureVector features, Model parent) throws MaltChainedException { setParent(parent); setIndex(index); if (index == -1) { setModelName(parent.getModelName() + "."); } else { setModelName(parent.getModelName() + "." + new Formatter().format("%03d", index) + "."); } setFeatures(features); setFrequency(0); initMethod(); if (getGuide().getGuideMode() == ClassifierGuide.GuideMode.BATCH && index == -1 && getGuide().getConfiguration().getConfigurationDir().getInfoFileWriter() != null) { try { getGuide() .getConfiguration() .getConfigurationDir() .getInfoFileWriter() .write(method.toString()); getGuide().getConfiguration().getConfigurationDir().getInfoFileWriter().flush(); } catch (IOException e) { throw new GuideException("Could not write learner settings to the information file. ", e); } } }
/** * 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; }
public void terminate() throws MaltChainedException { if (method != null) { method.terminate(); method = null; } featureVector = null; parent = null; }
public boolean predict(SingleDecision decision) throws MaltChainedException { try { if (getGuide().getGuideMode() == ClassifierGuide.GuideMode.BATCH) { throw new GuideException("Cannot predict during batch training. "); } return method.predict(featureVector, decision); } catch (NullPointerException e) { throw new GuideException("The learner cannot be found. ", e); } }
/* (non-Javadoc) * @see java.lang.Object#toString() */ public String toString() { final StringBuilder sb = new StringBuilder(); sb.append(method.toString()); return sb.toString(); }
public void moveAllInstances( LearningMethod method, FeatureFunction divideFeature, ArrayList<Integer> divideFeatureIndexVector) throws MaltChainedException { if (method == null) { throw new LibException("The learning method cannot be found. "); } else if (divideFeature == null) { throw new LibException("The divide feature cannot be found. "); } try { final BufferedReader in = new BufferedReader(getInstanceInputStreamReader(".ins")); final BufferedWriter out = method.getInstanceWriter(); final StringBuilder sb = new StringBuilder(6); int l = in.read(); char c; int j = 0; while (true) { if (l == -1) { sb.setLength(0); break; } c = (char) l; l = in.read(); if (c == '\t') { if (divideFeatureIndexVector.contains(j - 1)) { out.write( Integer.toString( ((SingleFeatureValue) divideFeature.getFeatureValue()).getIndexCode())); out.write('\t'); } out.write(sb.toString()); j++; out.write('\t'); sb.setLength(0); } else if (c == '\n') { out.write(sb.toString()); if (divideFeatureIndexVector.contains(j - 1)) { out.write('\t'); out.write( Integer.toString( ((SingleFeatureValue) divideFeature.getFeatureValue()).getIndexCode())); } out.write('\n'); sb.setLength(0); method.increaseNumberOfInstances(); this.decreaseNumberOfInstances(); j = 0; } else { sb.append(c); } } in.close(); getFile(".ins").delete(); out.flush(); } catch (SecurityException e) { throw new LibException("The learner cannot remove the instance file. ", e); } catch (NullPointerException e) { throw new LibException("The instance file cannot be found. ", e); } catch (FileNotFoundException e) { throw new LibException("The instance file cannot be found. ", e); } catch (IOException e) { throw new LibException("The learner read from the instance file. ", e); } }