Advertisement
Guest User

Untitled

a guest
Nov 24th, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.49 KB | None | 0 0
  1. import random
  2.  
  3. """
  4. db is a defaultdict(list) like this: {(word1, word2): [possible words]}
  5. """
  6.  
  7.  
  8. CHAIN_LENGTH = 2
  9. STOP_WORD = '\x02'
  10. MAX_WORDS = 50
  11.  
  12.  
  13. def generate_message(db):
  14.     key = random.choice(list(db.keys()))
  15.     gen_words = []
  16.     for i in range(MAX_WORDS):
  17.         gen_words.append(key[0])
  18.         next_word = random.choice(db[key])
  19.         if not next_word or next_word is STOP_WORD:
  20.             break
  21.         key = key[1:] + (next_word,)
  22.  
  23.     return ' '.join(gen_words)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement