Ejemplo n.º 1
0
  /**
   * If this tree contains max-marginals, recover the best parse subtree for a given symbol with the
   * specified span.
   */
  public CfgParseTree getBestParseTreeWithSpan(Object root, int spanStart, int spanEnd) {
    Preconditions.checkState(!sumProduct);

    Assignment rootAssignment = parentVar.outcomeArrayToAssignment(root);
    int rootNonterminalNum = parentVar.assignmentToIntArray(rootAssignment)[0];
    double prob =
        insideChart[spanStart][spanEnd][rootNonterminalNum]
            * outsideChart[spanStart][spanEnd][rootNonterminalNum];

    if (prob == 0.0) {
      return null;
    }

    int splitInd = splitBackpointers[spanStart][spanEnd][rootNonterminalNum];
    if (splitInd < 0) {
      long terminalKey = backpointers[spanStart][spanEnd][rootNonterminalNum];

      int positiveSplitInd = (-1 * splitInd) - 1;
      int terminalSpanStart = positiveSplitInd / numTerminals;
      int terminalSpanEnd = positiveSplitInd % numTerminals;

      // This is a really sucky way to transform the keys back to objects.
      VariableNumMap vars = parentVar.union(ruleTypeVar);
      int[] dimKey = TableFactor.zero(vars).getWeights().keyNumToDimKey(terminalKey);
      Assignment a = vars.intArrayToAssignment(dimKey);
      Object ruleType = a.getValue(ruleTypeVar.getOnlyVariableNum());

      List<Object> terminalList = Lists.newArrayList();
      terminalList.addAll(terminals.subList(terminalSpanStart, terminalSpanEnd + 1));
      return new CfgParseTree(root, ruleType, terminalList, prob, spanStart, spanEnd);
    } else {
      long binaryRuleKey = backpointers[spanStart][spanEnd][rootNonterminalNum];
      int[] binaryRuleComponents =
          binaryRuleDistribution.coerceToDiscrete().getWeights().keyNumToDimKey(binaryRuleKey);

      Assignment best = binaryRuleDistribution.getVars().intArrayToAssignment(binaryRuleComponents);
      Object leftRoot = best.getValue(leftVar.getOnlyVariableNum());
      Object rightRoot = best.getValue(rightVar.getOnlyVariableNum());
      Object ruleType = best.getValue(ruleTypeVar.getOnlyVariableNum());

      Preconditions.checkArgument(
          spanStart + splitInd != spanEnd,
          "CFG parse decoding error: %s %s %s",
          spanStart,
          spanEnd,
          splitInd);
      CfgParseTree leftTree = getBestParseTreeWithSpan(leftRoot, spanStart, spanStart + splitInd);
      CfgParseTree rightTree =
          getBestParseTreeWithSpan(rightRoot, spanStart + splitInd + 1, spanEnd);

      Preconditions.checkState(leftTree != null);
      Preconditions.checkState(rightTree != null);

      return new CfgParseTree(root, ruleType, leftTree, rightTree, prob);
    }
  }
Ejemplo n.º 2
0
  /**
   * Update an entry of the inside chart with a new production. Depending on the type of the chart,
   * this performs either a sum or max over productions of the same type in the same entry.
   */
  public void updateInsideEntry(
      int spanStart, int spanEnd, int splitInd, double[] values, Factor binaryRuleProbabilities) {
    Preconditions.checkArgument(binaryRuleProbabilities.getVars().size() == 4);

    if (sumProduct) {
      updateEntrySumProduct(
          insideChart[spanStart][spanEnd],
          values,
          binaryRuleProbabilities.coerceToDiscrete().getWeights(),
          parentVar.getOnlyVariableNum());
    } else {
      Tensor weights = binaryRuleProbabilities.coerceToDiscrete().getWeights();

      updateInsideEntryMaxProduct(spanStart, spanEnd, values, weights, splitInd);
    }
  }
Ejemplo n.º 3
0
  /**
   * Update an entry of the inside chart with a new production. Depending on the type of the chart,
   * this performs either a sum or max over productions of the same type in the same entry.
   */
  public void updateInsideEntryTerminal(
      int spanStart, int spanEnd, int terminalSpanStart, int terminalSpanEnd, Factor factor) {
    Preconditions.checkArgument(factor.getVars().size() == 2);
    // The first entry initializes the chart at this span.
    if (sumProduct) {
      updateEntrySumProduct(
          insideChart[spanStart][spanEnd],
          factor.coerceToDiscrete().getWeights().getValues(),
          factor.coerceToDiscrete().getWeights(),
          parentVar.getOnlyVariableNum());
    } else {
      Tensor weights = factor.coerceToDiscrete().getWeights();

      // Negative split indexes are used to represent terminal rules.
      int splitInd = -1 * (terminalSpanStart * numTerminals + terminalSpanEnd + 1);
      updateInsideEntryMaxProduct(spanStart, spanEnd, weights.getValues(), weights, splitInd);
    }
  }
Ejemplo n.º 4
0
  /** Compute the expected *unnormalized* probability of every rule. */
  public Factor getBinaryRuleExpectations() {
    Tensor binaryRuleWeights = binaryRuleDistribution.coerceToDiscrete().getWeights();
    SparseTensor tensor = SparseTensor.copyRemovingZeros(binaryRuleWeights, binaryRuleExpectations);

    return new TableFactor(binaryRuleDistribution.getVars(), tensor);
  }