Exemplo n.º 1
0
  /**
   * A local sort at a particular node along the deep sorting operation. This is a recursive
   * function.
   *
   * @param currentFeatureIdx The currently tested feature.
   * @param currentNode The current node, holding the currently processed zone in the array of
   *     feature vectors.
   */
  private void sortNode(int currentFeatureIdx, MaryNode currentNode) {
    /* If we have reached a leaf, do a final sort according to the unit index and return: */
    if (currentFeatureIdx == featureSequence.length) {
      Arrays.sort(featureVectors, currentNode.from, currentNode.to, cui);
      numberOfLeaves++;
      /*System.out.print( "LEAF ! (" + (currentNode.to-currentNode.from) + " units)" );
      for ( int i = currentNode.from; i < currentNode.to; i++ ) {
          System.out.print( " (" + featureVectors[i].getUnitIndex() + " 0)" );
      }
      System.out.println( "" );*/
      return;
    }
    /* Else: */
    int currentFeature = featureSequence[currentFeatureIdx];
    FeatureVector.FeatureType featureType = featureVectors[0].getFeatureType(currentFeature);
    /* Register the feature currently used for the splitting */
    currentNode.setFeatureIndex(currentFeature);
    /* Perform the sorting according to the currently considered feature: */
    /* 1) position the comparator onto the right feature */
    c.setFeatureIdx(currentFeature, featureType);
    /* 2) do the sorting */
    Arrays.sort(featureVectors, currentNode.from, currentNode.to, c);

    /* Then, seek for the zones where the feature value is the same,
     * and launch the next sort level on these. */
    int nVal = featureDefinition.getNumberOfValues(currentFeature);
    currentNode.split(nVal);
    int nextFrom = currentNode.from;
    int nextTo = currentNode.from;
    for (int i = 0; i < nVal; i++) {
      nextFrom = nextTo;
      // System.out.print( "Next node begins at " + nextFrom );
      while ((nextTo < currentNode.to)
          && (featureVectors[nextTo].getFeatureAsInt(currentFeature) == i)) {
        // System.out.print( " " + featureVectors[nextTo].getFeatureAsInt( currentFeature ) );
        nextTo++;
      }
      // System.out.println( " and ends at " + nextTo + " for a total of " + (nextTo-nextFrom) + "
      // units." );
      if ((nextTo - nextFrom) != 0) {
        MaryNode nod = new MaryNode(nextFrom, nextTo);
        currentNode.setChild(i, nod);
        // System.out.print("(" + i + " isByteOf " + currentFeature + ")" );
        sortNode(currentFeatureIdx + 1, nod);
      } else currentNode.setChild(i, null);
    }
  }