示例#1
0
  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;
  }
示例#2
0
  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;
  }
示例#3
0
  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. ");
    }
  }
示例#4
0
  public void addInstance(SingleDecision decision) throws MaltChainedException {
    if (getGuide().getGuideMode() == ClassifierGuide.GuideMode.CLASSIFY) {
      throw new GuideException("Can only add instance during learning. ");
    } else if (!(divideFeature.getFeatureValue() instanceof SingleFeatureValue)) {
      throw new GuideException("The divide feature does not have a single value. ");
    }

    divideFeature.update();
    if (divideModels != null) {
      if (!divideModels.containsKey(
          ((SingleFeatureValue) divideFeature.getFeatureValue()).getCode())) {
        divideModels.put(
            ((SingleFeatureValue) divideFeature.getFeatureValue()).getCode(),
            new AtomicModel(
                ((SingleFeatureValue) divideFeature.getFeatureValue()).getCode(),
                divideFeatureVector,
                this));
      }
      divideModels
          .get(((SingleFeatureValue) divideFeature.getFeatureValue()).getCode())
          .addInstance(decision);
    } else {
      throw new GuideException("The feature divide models cannot be found. ");
    }
  }
示例#5
0
  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);
    }
  }