def folds(documents=[], K=10, **kwargs):
 def chunks(iterable, n=10):
  a = list(iterable)
  i = 0
  j = 0
  for m in xrange(n):
   j = (i + len(a[m::n]))
   (yield a[i:j])
   i = j
 k = kwargs.get('k', K)
 d = list(chunks(documents, max(k, 2)))
 for holdout in xrange(k):
  (yield (list(chain(*(d[:holdout] + d[(holdout + 1):]))), d[holdout]))

def folds ( documents = [ ] , K = 10 , ** kwargs ) : def chunks ( iterable , n = 10 ) : a = list ( iterable ) i = 0 j = 0 for m in xrange ( n ) : j = ( i + len ( a [ m : : n ] ) ) ( yield a [ i : j ] ) i = j k = kwargs . get ( 'k' , K ) d = list ( chunks ( documents , max ( k , 2 ) ) ) for holdout in xrange ( k ) : ( yield ( list ( chain ( * ( d [ : holdout ] + d [ ( holdout + 1 ) : ] ) ) ) , d [ holdout ] ) )
returns an iterator of k folds .