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 addInstance(SingleDecision decision, FeatureVector featureVector) throws MaltChainedException { if (featureVector == null) { throw new LibException("The feature vector cannot be found"); } else if (decision == null) { throw new LibException("The decision cannot be found"); } try { sb.append(decision.getDecisionCode() + "\t"); final int n = featureVector.size(); for (int i = 0; i < n; i++) { FeatureValue featureValue = featureVector.getFeatureValue(i); if (featureValue == null || (excludeNullValues == true && featureValue.isNullValue())) { sb.append("-1"); } else { if (featureValue instanceof SingleFeatureValue) { SingleFeatureValue singleFeatureValue = (SingleFeatureValue) featureValue; if (singleFeatureValue.getValue() == 1) { sb.append(singleFeatureValue.getIndexCode()); } else if (singleFeatureValue.getValue() == 0) { sb.append("-1"); } else { sb.append(singleFeatureValue.getIndexCode()); sb.append(":"); sb.append(singleFeatureValue.getValue()); } } else if (featureValue instanceof MultipleFeatureValue) { Set<Integer> values = ((MultipleFeatureValue) featureValue).getCodes(); int j = 0; for (Integer value : values) { sb.append(value.toString()); if (j != values.size() - 1) { sb.append("|"); } j++; } } else { throw new LibException( "Don't recognize the type of feature value: " + featureValue.getClass()); } } sb.append('\t'); } sb.append('\n'); instanceOutput.write(sb.toString()); instanceOutput.flush(); increaseNumberOfInstances(); sb.setLength(0); } catch (IOException e) { throw new LibException("The learner cannot write to the instance file. ", e); } }
/** * Updates the corresponding single decision with the next value in the k-best list. * * @return true if decision has been updated, otherwise false * @throws MaltChainedException */ public boolean updateActionWithNextKBest() throws MaltChainedException { if (addCandidateIndex != 0 && topCandidateIndex < addCandidateIndex && topCandidateIndex < kBestList.size()) { int actionCode = kBestList.get(topCandidateIndex).getActionCode(); if (decision instanceof SingleDecision) { ((SingleDecision) decision).addDecision(actionCode); } topCandidateIndex++; return true; } return false; }
/** * Adds a candidate to the k-best list * * @param actionCode the integer representation of candidate action * @throws MaltChainedException */ public void add(int actionCode) throws MaltChainedException { if (k != -1 && addCandidateIndex >= k) { return; } if (addCandidateIndex >= kBestList.size()) { kBestList.add(new Candidate()); } kBestList.get(addCandidateIndex).setActionCode(actionCode); if (addCandidateIndex == 0) { if (decision instanceof SingleDecision) { ((SingleDecision) decision).addDecision(actionCode); } topCandidateIndex++; } addCandidateIndex++; }
public boolean predict(FeatureVector featureVector, SingleDecision decision) throws MaltChainedException { if (featureVector == null) { throw new LibException( "The learner cannot predict the next class, because the feature vector cannot be found. "); } final FeatureList featureList = new FeatureList(); final int size = featureVector.size(); for (int i = 1; i <= size; i++) { final FeatureValue featureValue = featureVector.getFeatureValue(i - 1); if (featureValue != null && !(excludeNullValues == true && featureValue.isNullValue())) { if (featureValue instanceof SingleFeatureValue) { SingleFeatureValue singleFeatureValue = (SingleFeatureValue) featureValue; int index = featureMap.getIndex(i, singleFeatureValue.getIndexCode()); if (index != -1 && singleFeatureValue.getValue() != 0) { featureList.add(index, singleFeatureValue.getValue()); } } else if (featureValue instanceof MultipleFeatureValue) { for (Integer value : ((MultipleFeatureValue) featureValue).getCodes()) { int v = featureMap.getIndex(i, value); if (v != -1) { featureList.add(v, 1); } } } } } try { decision.getKBestList().addList(model.predict(featureList.toArray())); // decision.getKBestList().addList(prediction(featureList)); } catch (OutOfMemoryError e) { throw new LibException("Out of memory. Please increase the Java heap size (-Xmx<size>). ", e); } return true; }