A probability table is also a generator. Start at the boundary symbol, draw the next symbol from that row's distribution, move to it, and repeat until the model draws the boundary again, which is how it says "this word is finished". The only primitive you need is a way to draw an index from a discrete distribution given one uniform random number. The clean way is inverse-transform sampling: lay the probabilities end to end along the interval from 0 to 1 as a cumulative sum, and the symbol whose segment contains the random number is the draw.
Implement sample_word(probs, itos, rng, max_len), which generates one word.
probs is a row-stochastic (V, V) array and itos[0] is the boundary .. Start in row 0.u = rng.random_sample() and picks the first index j whose cumulative probability is strictly greater than u. Clamp to V - 1 in case rounding leaves the last cumulative value just below u.max_len characters have been generated.rng is a np.random.RandomState.np.cumsum(probs[state]) gives the segment edges. np.searchsorted(cum, u, side='right') returns the first edge strictly greater than u.
Pinning one draw per step is what makes the output reproducible from a seed, so do not call rng anywhere else.
No results yet
Run your code to check it against the examples, or submit to run every test.