public InstanceList [ ] split ( java . util . Random r , int _nfolds ) { List < Integer > articleIds = newArrayList ( instanceArticles . keySet ( ) ) ; Collections . shuffle ( articleIds , r ) ; final int nrSentences = instanceArticles . size ( ) ; final int nrSentencesPerSplit = ( int ) ( nrSentences / ( double ) _nfolds ) ; int runningSplitId = 0 ; int runningSentencesPerSplit = 0 ; InstanceList [ ] splitted = new InstanceList [ _nfolds ] ; for ( int i = 0 ; i < splitted . length ; i ++ ) { splitted [ i ] = new InstanceList ( pipe ) ; } for ( int i = 0 ; i < articleIds . size ( ) ; i ++ ) { // add all sentences from this article for ( Instance sentence : instanceArticles . get ( articleIds . get ( i ) ) ) { // System.out.println(format("adding {} to split {}", // sentence.name, runningSplitId)); splitted [ runningSplitId ] . add ( sentence ) ; runningSentencesPerSplit ++ ; } // move to next split? if ( runningSentencesPerSplit > nrSentencesPerSplit && runningSplitId < ( _nfolds - 1 ) ) { runningSplitId ++ ; runningSentencesPerSplit -= nrSentencesPerSplit ; } } // some stats LOG . debug ( "{} sentences in {} splits" , nrSentences , _nfolds ) ; for ( InstanceList iList : splitted ) { LOG . debug ( "size:: " + iList . size ( ) ) ; } return splitted ; }
Shuffles the elements of this list among several smaller lists .