Advertisement
Peaser

Phrasemaker

Apr 12th, 2015
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. import dictionary, random
  2.  
  3. def phrase(minwords, maxwords, maxwordlen=None, maxphraselen=None, EXACT_PHRASE_LEN=None):
  4.     """
  5.    If "maxwordlen" and "maxphraselen" are NoneType (Default), then there are no
  6.    limits for max word/phrase length respectively.
  7.  
  8.    If "EXACT_PHRASE_LEN" is set, "maxphraselen" will be ignored regardless if it is set.
  9.  
  10.    >>> phrase(3, 5, 10, EXACT_PHRASE_LEN=20)
  11.    'lymphaemialenadrowel'
  12.  
  13.    >>> phrase(3, 5, 10, 22)
  14.    'eatsformicatekoa'
  15.  
  16.    """
  17.     maxwordlen   = float("inf") if maxwordlen   == None else maxwordlen
  18.     maxphraselen = float("inf") if maxphraselen == None else maxphraselen
  19.     words = [i for i in dictionary.allwords if len(i) <= maxwordlen]
  20.     while 1:
  21.         final = ''.join([random.choice(words) for i in range(random.randrange(minwords, maxwords+1))])
  22.         if EXACT_PHRASE_LEN:
  23.             if len(final) == EXACT_PHRASE_LEN:
  24.                 break
  25.         else:
  26.             if len(final) <= maxphraselen:
  27.                 break
  28.     final = final.lower()
  29.     return final
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement