private void buildSegmentationRecursive(int method, Segments smnt, int b, int e)
    {
        int bestid, bestval1, bestval2, temp;

        bestid=-1;bestval1=-1;bestval2=-1;
        for(int i=0;i<super.size();i++){
            if(b<=beg(i) && e>=end(i)){
                switch(method){
                case SEGMENTATION_LONGEST_WEIGHTED:
                    if(len(i)>bestval1 ||
                       (len(i)==bestval1 && conn(i)>bestval2) ){
                        bestid=i;
                        bestval1=len(i);
                        bestval2=conn(i);
                    }
                    break;
                case SEGMENTATION_LONGEST_LEFTMOST:
                    if(len(i)>bestval1 ||
                       (len(i)==bestval1 && beg(i)<bestval2) ){
                        bestid=i;
                        bestval1=len(i);
                        bestval2=beg(i);
                    }
                    break;
                case SEGMENTATION_LONGEST_RIGHTMOST:
                    if(len(i)>bestval1 ||
                       (len(i)==bestval1 && end(i)>bestval2) ){
                        bestid=i;
                        bestval1=len(i);
                        bestval2=end(i);
                    }
                    break;
                case SEGMENTATION_WEIGHTED_LONGEST:
                    temp = (len(i)>1)?conn(i):0;
                    if(temp>bestval1 ||
                       (temp==bestval1 && len(i)>bestval2) ){
                        bestid=i;
                        bestval1=temp;
                        bestval2=len(i);
                    }
                    break;
                case SEGMENTATION_WEIGHTED_LEFTMOST:
                    temp = (len(i)>1)? conn(i) :0;
                    if(temp>bestval1 ||
                       (temp==bestval1 && beg(i)<bestval2) ){
                        bestid=i;
                        bestval1=temp;
                        bestval2=beg(i);
                    }
                    break;
                case SEGMENTATION_WEIGHTED_RIGHTMOST:
                    temp = len(i)>1?conn(i):0;
                    if(temp>bestval1 ||
                       (temp==bestval1 && end(i)>bestval2) ){
                        bestid=i;
                        bestval1=temp;
                        bestval2=end(i);
                    }
                    break;
                default: // dummy defult pick first possible
                    if(bestid<0){
                        bestid=i;
                    }
                    break;
                }
            }
        }
        if(bestid<0) {
            return; // this should never happen, as all one-word segments are created
        }

        if(b<beg(bestid)){
            buildSegmentationRecursive(method,smnt,b,beg(bestid));
        }

        // add segment
        smnt.add(super.get(bestid));

        // check right side
        if(e>end(bestid)){
            buildSegmentationRecursive(method,smnt,end(bestid),e);
        }
    }
Contains the segmentation() method.

@author  Peter Boros
fsa/src/main/java/com/yahoo/fsa/segmenter/Segments.java