void doInsideScores(){
  grammar.logarithmMode();
  lexicon.logarithmMode();
  for (int diff=1; diff <= length; diff++) {
    for (int start=0; start < (length - diff + 1); start++) {
      int end=start + diff;
      for (int pparentState=0; pparentState < numStates; pparentState++) {
        BinaryRule[] parentRules=grammar.splitRulesWithP(pparentState);
        for (int i=0; i < parentRules.length; i++) {
          BinaryRule r=parentRules[i];
          int leftState=r.leftChildState;
          int parentState=r.parentState;
          int narrowR=narrowRExtent[start][leftState];
          boolean iPossibleL=(narrowR < end);
          if (!iPossibleL) {
            continue;
          }
          int narrowL=narrowLExtent[end][r.rightChildState];
          boolean iPossibleR=(narrowL >= narrowR);
          if (!iPossibleR) {
            continue;
          }
          int min1=narrowR;
          int min2=wideLExtent[end][r.rightChildState];
          int min=(min1 > min2 ? min1 : min2);
          if (min > narrowL) {
            continue;
          }
          int max1=wideRExtent[start][leftState];
          int max2=narrowL;
          int max=(max1 < max2 ? max1 : max2);
          if (min > max) {
            continue;
          }
          double pS=r.getScore(0,0,0);
          double oldIScore=iScore[start][end][parentState];
          double bestIScore=oldIScore;
          boolean foundBetter;
          for (int split=min; split <= max; split++) {
            double lS=iScore[start][split][leftState];
            if (Double.isInfinite(lS)) {
              continue;
            }
            double rS=iScore[split][end][r.rightChildState];
            if (Double.isInfinite(rS)) {
              continue;
            }
            touchedRules++;
            double tot=pS + lS + rS;
            if (tot > bestIScore) {
              bestIScore=tot;
            }
          }
          foundBetter=bestIScore > oldIScore;
          if (foundBetter) {
            iScore[start][end][parentState]=bestIScore;
            if (Double.isInfinite(oldIScore)) {
              if (start > narrowLExtent[end][parentState]) {
                narrowLExtent[end][parentState]=start;
                wideLExtent[end][parentState]=start;
              }
 else {
                if (start < wideLExtent[end][parentState]) {
                  wideLExtent[end][parentState]=start;
                }
              }
              if (end < narrowRExtent[start][parentState]) {
                narrowRExtent[start][parentState]=end;
                wideRExtent[start][parentState]=end;
              }
 else {
                if (end > wideRExtent[start][parentState]) {
                  wideRExtent[start][parentState]=end;
                }
              }
            }
          }
        }
      }
      for (int pState=0; pState < numStates; pState++) {
        UnaryRule[] unaries=grammar.getClosedViterbiUnaryRulesByParent(pState);
        double cur=iScore[start][end][pState];
        double best=cur;
        for (int r=0; r < unaries.length; r++) {
          UnaryRule ur=unaries[r];
          int cState=ur.childState;
          if (pState == cState)           continue;
          double pS=ur.getScore(0,0);
          double iS=iScore[start][end][cState];
          if (Double.isInfinite(iS)) {
            continue;
          }
          double tot=iS + pS;
          touchedRules++;
          if (tot > best) {
            best=tot;
          }
        }
        if (best > cur) {
          iScore[start][end][pState]=best;
          if (cur == Double.NEGATIVE_INFINITY) {
            if (start > narrowLExtent[end][pState]) {
              narrowLExtent[end][pState]=start;
              wideLExtent[end][pState]=start;
            }
 else {
              if (start < wideLExtent[end][pState]) {
                wideLExtent[end][pState]=start;
              }
            }
            if (end < narrowRExtent[start][pState]) {
              narrowRExtent[start][pState]=end;
              wideRExtent[start][pState]=end;
            }
 else {
              if (end > wideRExtent[start][pState]) {
                wideRExtent[start][pState]=end;
              }
            }
          }
        }
      }
    }
  }
}

void doInsideScores ( ) { grammar . logarithmMode ( ) ; lexicon . logarithmMode ( ) ; for ( int diff = 1 ; diff <= length ; diff ++ ) { for ( int start = 0 ; start < ( length - diff + 1 ) ; start ++ ) { int end = start + diff ; for ( int pparentState = 0 ; pparentState < numStates ; pparentState ++ ) { BinaryRule [ ] parentRules = grammar . splitRulesWithP ( pparentState ) ; for ( int i = 0 ; i < parentRules . length ; i ++ ) { BinaryRule r = parentRules [ i ] ; int leftState = r . leftChildState ; int parentState = r . parentState ; int narrowR = narrowRExtent [ start ] [ leftState ] ; boolean iPossibleL = ( narrowR < end ) ; if ( ! iPossibleL ) { continue ; } int narrowL = narrowLExtent [ end ] [ r . rightChildState ] ; boolean iPossibleR = ( narrowL >= narrowR ) ; if ( ! iPossibleR ) { continue ; } int min1 = narrowR ; int min2 = wideLExtent [ end ] [ r . rightChildState ] ; int min = ( min1 > min2 ? min1 : min2 ) ; if ( min > narrowL ) { continue ; } int max1 = wideRExtent [ start ] [ leftState ] ; int max2 = narrowL ; int max = ( max1 < max2 ? max1 : max2 ) ; if ( min > max ) { continue ; } double pS = r . getScore ( 0 , 0 , 0 ) ; double oldIScore = iScore [ start ] [ end ] [ parentState ] ; double bestIScore = oldIScore ; boolean foundBetter ; for ( int split = min ; split <= max ; split ++ ) { double lS = iScore [ start ] [ split ] [ leftState ] ; if ( Double . isInfinite ( lS ) ) { continue ; } double rS = iScore [ split ] [ end ] [ r . rightChildState ] ; if ( Double . isInfinite ( rS ) ) { continue ; } touchedRules ++ ; double tot = pS + lS + rS ; if ( tot > bestIScore ) { bestIScore = tot ; } } foundBetter = bestIScore > oldIScore ; if ( foundBetter ) { iScore [ start ] [ end ] [ parentState ] = bestIScore ; if ( Double . isInfinite ( oldIScore ) ) { if ( start > narrowLExtent [ end ] [ parentState ] ) { narrowLExtent [ end ] [ parentState ] = start ; wideLExtent [ end ] [ parentState ] = start ; } else { if ( start < wideLExtent [ end ] [ parentState ] ) { wideLExtent [ end ] [ parentState ] = start ; } } if ( end < narrowRExtent [ start ] [ parentState ] ) { narrowRExtent [ start ] [ parentState ] = end ; wideRExtent [ start ] [ parentState ] = end ; } else { if ( end > wideRExtent [ start ] [ parentState ] ) { wideRExtent [ start ] [ parentState ] = end ; } } } } } } for ( int pState = 0 ; pState < numStates ; pState ++ ) { UnaryRule [ ] unaries = grammar . getClosedViterbiUnaryRulesByParent ( pState ) ; double cur = iScore [ start ] [ end ] [ pState ] ; double best = cur ; for ( int r = 0 ; r < unaries . length ; r ++ ) { UnaryRule ur = unaries [ r ] ; int cState = ur . childState ; if ( pState == cState ) continue ; double pS = ur . getScore ( 0 , 0 ) ; double iS = iScore [ start ] [ end ] [ cState ] ; if ( Double . isInfinite ( iS ) ) { continue ; } double tot = iS + pS ; touchedRules ++ ; if ( tot > best ) { best = tot ; } } if ( best > cur ) { iScore [ start ] [ end ] [ pState ] = best ; if ( cur == Double . NEGATIVE_INFINITY ) { if ( start > narrowLExtent [ end ] [ pState ] ) { narrowLExtent [ end ] [ pState ] = start ; wideLExtent [ end ] [ pState ] = start ; } else { if ( start < wideLExtent [ end ] [ pState ] ) { wideLExtent [ end ] [ pState ] = start ; } } if ( end < narrowRExtent [ start ] [ pState ] ) { narrowRExtent [ start ] [ pState ] = end ; wideRExtent [ start ] [ pState ] = end ; } else { if ( end > wideRExtent [ start ] [ pState ] ) { wideRExtent [ start ] [ pState ] = end ; } } } } } } } }
fills in the iscore array of each category over each span of length 2 or more . note : this places the grammar and lexicon into logarithm mode !
