/**
 * Generates a string from a list of words.
 */ import { WordsetItem } from './generate' ; function generateFromCorpus ( { words , wordCount , difficulty , } ) { let result = [ ] ; let lastWord = undefined ; let i = 0 ; while ( i < wordCount ) { let word = pick ( words , difficulty ) ; if ( typeof word === 'function' ) word = word ( ) ; const subwords = word . split ( /\s+/ ) ; if ( subwords [ 0 ] !== lastWord ) { result . push ( word ) ; lastWord = subwords [ subwords . length - 1 ] ; i += subwords . length ; } } return result . join ( ' ' ) ; } example function pick < T > ( words , difficulty ) { const rand = Math . random ( ) ** difficulty ; const index = Math . round ( ( words . length - 1 ) * rand ) ; return words [ index ] ; } export { generateFromCorpus } ;