Exemple #1
0
 /**
  * Provides the fundamental data structures which encode the maxent model information. This method
  * will usually only be needed by GISModelWriters. The following values are held in the Object
  * array which is returned by this method:
  * <li>index 0: opennlp.maxent.Context[] containing the model parameters
  * <li>index 1: java.util.Map containing the mapping of model predicates to unique integers
  * <li>index 2: java.lang.String[] containing the names of the outcomes, stored in the index of
  *     the array which represents their unique ids in the model.
  * <li>index 3: java.lang.Integer containing the value of the models correction constant
  * <li>index 4: java.lang.Double containing the value of the models correction parameter
  *
  * @return An Object[] with the values as described above.
  */
 public final Object[] getDataStructures() {
   Object[] data = new Object[5];
   data[0] = evalParams.getParams();
   data[1] = pmap;
   data[2] = outcomeNames;
   data[3] = (int) evalParams.getCorrectionConstant();
   data[4] = evalParams.getCorrectionParam();
   return data;
 }
Exemple #2
0
  /**
   * 预测
   *
   * @param context 环境
   * @param prior 先验概率
   * @param model 特征函数
   * @return
   */
  public static double[] eval(int[] context, double[] prior, EvalParameters model) {
    Context[] params = model.getParams();
    int numfeats[] = new int[model.getNumOutcomes()];
    int[] activeOutcomes;
    double[] activeParameters;
    double value = 1;
    for (int ci = 0; ci < context.length; ci++) {
      if (context[ci] >= 0) {
        Context predParams = params[context[ci]];
        activeOutcomes = predParams.getOutcomes();
        activeParameters = predParams.getParameters();
        for (int ai = 0; ai < activeOutcomes.length; ai++) {
          int oid = activeOutcomes[ai];
          numfeats[oid]++;
          prior[oid] += activeParameters[ai] * value;
        }
      }
    }

    double normal = 0.0;
    for (int oid = 0; oid < model.getNumOutcomes(); oid++) {
      if (model.getCorrectionParam() != 0) {
        prior[oid] =
            Math.exp(
                prior[oid] * model.getConstantInverse()
                    + ((1.0 - ((double) numfeats[oid] / model.getCorrectionConstant()))
                        * model.getCorrectionParam()));
      } else {
        prior[oid] = Math.exp(prior[oid] * model.getConstantInverse());
      }
      normal += prior[oid];
    }

    for (int oid = 0; oid < model.getNumOutcomes(); oid++) {
      prior[oid] /= normal;
    }
    return prior;
  }
Exemple #3
0
 public int getNumOutcomes() {
   return (evalParams.getNumOutcomes());
 }
Exemple #4
0
 /**
  * 预测分布
  *
  * @param context 环境
  * @return 概率数组
  */
 public final double[] eval(String[] context) {
   return (eval(context, new double[evalParams.getNumOutcomes()]));
 }