Skip to main content
language tags, new example
Source Link
AShelly
  • 4.8k
  • 1
  • 24
  • 39

This entry selects words from whole system dictionary. It takes advantage of the fact that you can make most nouns into verbs and vice-versa,. It uses a few heuristics to classify words and avoid obvious impossibilities.

$ man python | python words.py The disabled comma-separated source is using those wizards at exit. $ cat COPYING | python words.py #GPL My user accord actions a gnu of software. $ cat pg2591.txt | python words.py #Grimm's Fairy Tales Some bargain receives my threepence. Any wrong worms your world. $ cat words.py | python words.py #self reflection Your filter_possesive not_nouned those prepositions. 
$ man python | python words.py The disabled comma-separated source is using those wizards at exit. $ cat COPYING | python words.py #GPL My user accord actions a gnu of software. $ cat pg2591.txt | python words.py #Grimm's Fairy Tales Some bargain receives my threepence. Any wrong worms your world. $ cat words.py | python words.py #self reflection Your filter_possesive not_nouned those prepositions. $ ls /usr/bin | python words.py #directory lists Their dropbox funziped an arch. 
import random import string import sys import re #words = open("/usr/share/dict/words").readlines() words = re.sub("[]:;.,:?!<>{}()|=\"`[]",' ',sys.stdin.read(),flags=re.M).split() words = list(set(words)) articles=('','a ','the ','some ','this ','that ','my ','any ','your ','their ', 'all ','more ' 'an ') #an must be last pl_articles=('','some ','those ','many ','the ','these ') prepositions = ('of','by','to','for','from','in','with','on','which','when','at', 'into','as','if','near') conjunctions = ('and','or','but') verbs = ('is','are','was', 'be','do','came','been','had','have') pronouns_s = ('he','she','it','we','you') pronouns_o = ('him','her','them') possesive=False modifiers=0 use_prep = None MAX_MODIFIERS=2 def is_modifier(w): return ("'" in w or w[-2:] in ('ry','ed','er','ic','al') or w[-3:] in ('ing','est','ble','ous') or w[-4:] in ('less','ical','mmon') ) def is_verb(w): return (w in verbs or w[-2:] in ('ed',) or w[-3:] in ('ing','ize') ) def is_article(w): return w+' ' in articles or w+' ' in pl_articles def is_conjunction(w): return w in conjunctions def filter_possesive(w,always=False): global possesive #allow only one result = True if "'" in w else False if result: if always: return False if not possesive: possesive = True return False return result def is_preposition(w): global use_prep if w in prepositions: use_prep = w return True return False def is_adverb(w): return w[-2:]=='ly' def is_gerund(w): return w[-3:]=='ing' def is_plural(w): return w[-1]=='s' def not_verb(w): return (w in ('you','they','our','yes') or w[-4:] in ('ness','such') or w in pronouns_o or w in pronouns_s ) def not_noun(w): return (w in verbs) def getword(): while True: w=words[random.randrange(len(words))].rstrip() if w[0] in string.ascii_uppercase: continue if is_article(w) or is_preposition(w): continue if filter_possesive(w): continue #print w return w def get_article(): return articles[random.randrange(len(articles)-1)] #print '--s--' substr='' conjunction = False while True: w=getword() if is_modifier(w): if modifiers < MAX_MODIFIERS: substr+=w+' ' modifiers+=1 else: continue elif is_adverb(w) or is_plural(w) or not_noun(w): continue else: if is_conjunction(w): conjunction = w continue substr= substr+w+' ' if conjunction: substr+=conjunction+' ' conjunction = False continue if w in pronouns_s: substr = w+' ' art='' else: art = get_article() if art is 'a ' and substr[0] in 'aeiou': art='an ' substr= string.capwords(art+substr,'.') break #print '--v--' verbstr='' while True: w=getword() if not_verb(w) or filter_possesive(w,True): continue elif is_adverb(w): verbstr+=w+' ' elif is_gerund(w): verbstr+='is '+w+' ' break elif is_verb(w): verbstr= verbstr+w+' ' break elif is_modifier(w) or is_conjunction(w): continue else: if not is_plural(w): w=w+'ed' if w[-1]!='e' else w+'d' verbstr= verbstr+w+' ' break #print '--o--' obstr='' conjunction = False while True: w=getword() if is_modifier(w): if modifiers<MAX_MODIFIERS: obstr+=w+' ' modifiers+=1 else: continue elif is_adverb(w) or not_noun(w) or w in pronouns_s: continue else: if is_conjunction(w): conjunction = w continue obstr = obstr+w if conjunction: obstr+=' '+conjunction+' ' conjunction = False continue if is_plural(w): art = pl_articles[random.randrange(len(pl_articles))] else: art = articles[random.randrange(len(articles)-1)] if art is 'a ' and obstr[0] in 'aeiou': art='an ' if w in pronouns_o: obstr=w else: obstr= art+obstr break #print '--p--' while use_prep: w=getword() if (is_modifier(w) or is_preposition(w) or is_gerund(w) or not_noun(w) or is_conjunction(w)): continue obstr+=' '+use_prep+' '+w use_prep=None print substr+verbstr+obstr+'.' 
import random import string import sys import re #words = open("/usr/share/dict/words").readlines() words = re.sub("[]:;.,:?!<>{}()|=\"`[]",' ',sys.stdin.read(),flags=re.M).split() words = list(set(words)) articles=('','a ','the ','some ','this ','that ','my ','any ','your ','their ', 'all ','more ' 'an ') #an must be last pl_articles=('','some ','those ','many ','the ','these ') prepositions = ('of','by','to','for','from','in','with','on','which','when','at', 'into','as','if','near') conjunctions = ('and','or','but') verbs = ('is','are','was', 'be','do','came','been','had','have') pronouns_s = ('he','she','it','we','you') pronouns_o = ('him','her','them') possesive=False modifiers=0 use_prep = None MAX_MODIFIERS=2 def is_modifier(w): return ("'" in w or w[-2:] in ('ry','ed','er','ic','al') or w[-3:] in ('ing','est','ble','ous') or w[-4:] in ('less','ical','mmon') ) def is_verb(w): return (w in verbs or w[-2:] in ('ed',) or w[-3:] in ('ing','ize') ) def is_article(w): return w+' ' in articles or w+' ' in pl_articles def is_conjunction(w): return w in conjunctions def filter_possesive(w,always=False): global possesive #allow only one result = True if "'" in w else False if result: if always: return False if not possesive: possesive = True return False return result def is_preposition(w): global use_prep if w in prepositions: use_prep = w return True return False def is_adverb(w): return w[-2:]=='ly' def is_gerund(w): return w[-3:]=='ing' def is_plural(w): return w[-1]=='s' def not_verb(w): return (w in ('you','they','our','yes') or w[-4:] in ('ness','such') or w in pronouns_o or w in pronouns_s ) def not_noun(w): return (w in verbs) def getword(): while True: w=words[random.randrange(len(words))].rstrip() if w[0] in string.ascii_uppercase: continue if is_article(w) or is_preposition(w): continue if filter_possesive(w): continue #print w return w def get_article(): return articles[random.randrange(len(articles)-1)] #print '--s--' substr='' conjunction = False while True: w=getword() if is_modifier(w): if modifiers < MAX_MODIFIERS: substr+=w+' ' modifiers+=1 else: continue elif is_adverb(w) or is_plural(w) or not_noun(w): continue else: if is_conjunction(w): conjunction = w continue substr= substr+w+' ' if conjunction: substr+=conjunction+' ' conjunction = False continue if w in pronouns_s: substr = w+' ' art='' else: art = get_article() if art is 'a ' and substr[0] in 'aeiou': art='an ' substr= string.capwords(art+substr,'.') break #print '--v--' verbstr='' while True: w=getword() if not_verb(w) or filter_possesive(w,True): continue elif is_adverb(w): verbstr+=w+' ' elif is_gerund(w): verbstr+='is '+w+' ' break elif is_verb(w): verbstr= verbstr+w+' ' break elif is_modifier(w) or is_conjunction(w): continue else: if not is_plural(w): w=w+'ed' if w[-1]!='e' else w+'d' verbstr= verbstr+w+' ' break #print '--o--' obstr='' conjunction = False while True: w=getword() if is_modifier(w): if modifiers<MAX_MODIFIERS: obstr+=w+' ' modifiers+=1 else: continue elif is_adverb(w) or not_noun(w) or w in pronouns_s: continue else: if is_conjunction(w): conjunction = w continue obstr = obstr+w if conjunction: obstr+=' '+conjunction+' ' conjunction = False continue if is_plural(w): art = pl_articles[random.randrange(len(pl_articles))] else: art = articles[random.randrange(len(articles)-1)] if art is 'a ' and obstr[0] in 'aeiou': art='an ' if w in pronouns_o: obstr=w else: obstr= art+obstr break #print '--p--' while use_prep: w=getword() if (is_modifier(w) or is_preposition(w) or is_gerund(w) or not_noun(w) or is_conjunction(w)): continue obstr+=' '+use_prep+' '+w use_prep=None print substr+verbstr+obstr+'.' 

This entry selects words from whole system dictionary. It takes advantage of the fact that you can make most nouns into verbs and vice-versa, It uses a few heuristics to avoid obvious impossibilities.

$ man python | python words.py The disabled comma-separated source is using those wizards at exit. $ cat COPYING | python words.py #GPL My user accord actions a gnu of software. $ cat pg2591.txt | python words.py #Grimm's Fairy Tales Some bargain receives my threepence. Any wrong worms your world. $ cat words.py | python words.py #self reflection Your filter_possesive not_nouned those prepositions. 
import random import string import sys import re #words = open("/usr/share/dict/words").readlines() words = re.sub("[]:;.,:?!<>{}()|=\"`[]",' ',sys.stdin.read(),flags=re.M).split() words = list(set(words)) articles=('','a ','the ','some ','this ','that ','my ','any ','your ','their ', 'all ','more ' 'an ') #an must be last pl_articles=('','some ','those ','many ','the ','these ') prepositions = ('of','by','to','for','from','in','with','on','which','when','at', 'into','as','if','near') conjunctions = ('and','or','but') verbs = ('is','are','was', 'be','do','came','been','had','have') pronouns_s = ('he','she','it','we','you') pronouns_o = ('him','her','them') possesive=False modifiers=0 use_prep = None MAX_MODIFIERS=2 def is_modifier(w): return ("'" in w or w[-2:] in ('ry','ed','er','ic','al') or w[-3:] in ('ing','est','ble','ous') or w[-4:] in ('less','ical','mmon') ) def is_verb(w): return (w in verbs or w[-2:] in ('ed',) or w[-3:] in ('ing','ize') ) def is_article(w): return w+' ' in articles or w+' ' in pl_articles def is_conjunction(w): return w in conjunctions def filter_possesive(w,always=False): global possesive #allow only one result = True if "'" in w else False if result: if always: return False if not possesive: possesive = True return False return result def is_preposition(w): global use_prep if w in prepositions: use_prep = w return True return False def is_adverb(w): return w[-2:]=='ly' def is_gerund(w): return w[-3:]=='ing' def is_plural(w): return w[-1]=='s' def not_verb(w): return (w in ('you','they','our','yes') or w[-4:] in ('ness','such') or w in pronouns_o or w in pronouns_s ) def not_noun(w): return (w in verbs) def getword(): while True: w=words[random.randrange(len(words))].rstrip() if w[0] in string.ascii_uppercase: continue if is_article(w) or is_preposition(w): continue if filter_possesive(w): continue #print w return w def get_article(): return articles[random.randrange(len(articles)-1)] #print '--s--' substr='' conjunction = False while True: w=getword() if is_modifier(w): if modifiers < MAX_MODIFIERS: substr+=w+' ' modifiers+=1 else: continue elif is_adverb(w) or is_plural(w) or not_noun(w): continue else: if is_conjunction(w): conjunction = w continue substr= substr+w+' ' if conjunction: substr+=conjunction+' ' conjunction = False continue if w in pronouns_s: substr = w+' ' art='' else: art = get_article() if art is 'a ' and substr[0] in 'aeiou': art='an ' substr= string.capwords(art+substr,'.') break #print '--v--' verbstr='' while True: w=getword() if not_verb(w) or filter_possesive(w,True): continue elif is_adverb(w): verbstr+=w+' ' elif is_gerund(w): verbstr+='is '+w+' ' break elif is_verb(w): verbstr= verbstr+w+' ' break elif is_modifier(w) or is_conjunction(w): continue else: if not is_plural(w): w=w+'ed' if w[-1]!='e' else w+'d' verbstr= verbstr+w+' ' break #print '--o--' obstr='' conjunction = False while True: w=getword() if is_modifier(w): if modifiers<MAX_MODIFIERS: obstr+=w+' ' modifiers+=1 else: continue elif is_adverb(w) or not_noun(w) or w in pronouns_s: continue else: if is_conjunction(w): conjunction = w continue obstr = obstr+w if conjunction: obstr+=' '+conjunction+' ' conjunction = False continue if is_plural(w): art = pl_articles[random.randrange(len(pl_articles))] else: art = articles[random.randrange(len(articles)-1)] if art is 'a ' and obstr[0] in 'aeiou': art='an ' if w in pronouns_o: obstr=w else: obstr= art+obstr break #print '--p--' while use_prep: w=getword() if (is_modifier(w) or is_preposition(w) or is_gerund(w) or not_noun(w) or is_conjunction(w)): continue obstr+=' '+use_prep+' '+w use_prep=None print substr+verbstr+obstr+'.' 

This entry selects words from whole system dictionary. It takes advantage of the fact that you can make most nouns into verbs and vice-versa. It uses a few heuristics to classify words and avoid obvious impossibilities.

$ man python | python words.py The disabled comma-separated source is using those wizards at exit. $ cat COPYING | python words.py #GPL My user accord actions a gnu of software. $ cat pg2591.txt | python words.py #Grimm's Fairy Tales Some bargain receives my threepence. Any wrong worms your world. $ cat words.py | python words.py #self reflection Your filter_possesive not_nouned those prepositions. $ ls /usr/bin | python words.py #directory lists Their dropbox funziped an arch. 
import random import string import sys import re #words = open("/usr/share/dict/words").readlines() words = re.sub("[]:;.,:?!<>{}()|=\"`[]",' ',sys.stdin.read(),flags=re.M).split() words = list(set(words)) articles=('','a ','the ','some ','this ','that ','my ','any ','your ','their ', 'all ','more ' 'an ') #an must be last pl_articles=('','some ','those ','many ','the ','these ') prepositions = ('of','by','to','for','from','in','with','on','which','when','at', 'into','as','if','near') conjunctions = ('and','or','but') verbs = ('is','are','was', 'be','do','came','been','had','have') pronouns_s = ('he','she','it','we','you') pronouns_o = ('him','her','them') possesive=False modifiers=0 use_prep = None MAX_MODIFIERS=2 def is_modifier(w): return ("'" in w or w[-2:] in ('ry','ed','er','ic','al') or w[-3:] in ('ing','est','ble','ous') or w[-4:] in ('less','ical','mmon') ) def is_verb(w): return (w in verbs or w[-2:] in ('ed',) or w[-3:] in ('ing','ize') ) def is_article(w): return w+' ' in articles or w+' ' in pl_articles def is_conjunction(w): return w in conjunctions def filter_possesive(w,always=False): global possesive #allow only one result = True if "'" in w else False if result: if always: return False if not possesive: possesive = True return False return result def is_preposition(w): global use_prep if w in prepositions: use_prep = w return True return False def is_adverb(w): return w[-2:]=='ly' def is_gerund(w): return w[-3:]=='ing' def is_plural(w): return w[-1]=='s' def not_verb(w): return (w in ('you','they','our','yes') or w[-4:] in ('ness','such') or w in pronouns_o or w in pronouns_s ) def not_noun(w): return (w in verbs) def getword(): while True: w=words[random.randrange(len(words))].rstrip() if w[0] in string.ascii_uppercase: continue if is_article(w) or is_preposition(w): continue if filter_possesive(w): continue #print w return w def get_article(): return articles[random.randrange(len(articles)-1)] #print '--s--' substr='' conjunction = False while True: w=getword() if is_modifier(w): if modifiers < MAX_MODIFIERS: substr+=w+' ' modifiers+=1 else: continue elif is_adverb(w) or is_plural(w) or not_noun(w): continue else: if is_conjunction(w): conjunction = w continue substr= substr+w+' ' if conjunction: substr+=conjunction+' ' conjunction = False continue if w in pronouns_s: substr = w+' ' art='' else: art = get_article() if art is 'a ' and substr[0] in 'aeiou': art='an ' substr= string.capwords(art+substr,'.') break #print '--v--' verbstr='' while True: w=getword() if not_verb(w) or filter_possesive(w,True): continue elif is_adverb(w): verbstr+=w+' ' elif is_gerund(w): verbstr+='is '+w+' ' break elif is_verb(w): verbstr= verbstr+w+' ' break elif is_modifier(w) or is_conjunction(w): continue else: if not is_plural(w): w=w+'ed' if w[-1]!='e' else w+'d' verbstr= verbstr+w+' ' break #print '--o--' obstr='' conjunction = False while True: w=getword() if is_modifier(w): if modifiers<MAX_MODIFIERS: obstr+=w+' ' modifiers+=1 else: continue elif is_adverb(w) or not_noun(w) or w in pronouns_s: continue else: if is_conjunction(w): conjunction = w continue obstr = obstr+w if conjunction: obstr+=' '+conjunction+' ' conjunction = False continue if is_plural(w): art = pl_articles[random.randrange(len(pl_articles))] else: art = articles[random.randrange(len(articles)-1)] if art is 'a ' and obstr[0] in 'aeiou': art='an ' if w in pronouns_o: obstr=w else: obstr= art+obstr break #print '--p--' while use_prep: w=getword() if (is_modifier(w) or is_preposition(w) or is_gerund(w) or not_noun(w) or is_conjunction(w)): continue obstr+=' '+use_prep+' '+w use_prep=None print substr+verbstr+obstr+'.' 
added 3095 characters in body
Source Link
AShelly
  • 4.8k
  • 1
  • 24
  • 39

CodeVersion 3 has been modified to take any text file as input:

$ man python | python words.py The disabled comma-separated source is using those wizards at exit. $ cat COPYING | python words.py #GPL My user accord actions a gnu of software. $ cat pg2591.txt | python words.py #Grimm's Fairy Tales Some bargain receives my threepence. Any wrong worms your world. $ cat words.py | python words.py #self reflection Your filter_possesive not_nouned those prepositions. 

Code (version 3):

import random import string import sys wordsimport re #words = open("/usr/share/dict/words").readlines() words = re.sub("[]:;.,:?!<>{}()|=\"`[]",' ',sys.stdin.read(),flags=re.M).split() words = list(set(words)) articles=('','a ','the ','some ','this ','that ','my ','any ','your ','their ', 'all ','more ' 'an ') #an must be last pl_articles=('','some ','those ','many ','the ','these ') prepositions = ('of','by','to','for','from','in','with','on','which','when','at', 'into','as','if','near') conjunctions = ('and','or','but') verbs = ('is','are','was', 'be','do','came','been','had','have') pronouns_s = ('he','she','it','we','you') pronouns_o = ('him','her','them') possesive=False modifiers=0 use_prep = None  MAX_MODIFIERS=2 def getwordis_modifier(w): globalreturn possesive("'" in w or while True w[-2:] in ('ry','ed','er','ic','al') or w=words[random.randrangew[-3:] in (len'ing','est','ble','ous') or w[-4:] in (words'less','ical','mmon') ) def is_verb(w): return (w in verbs or w[-2:] in ('ed',) or w[-3:].rstrip in ('ing','ize') ) def is_article(w): return w+' ' in ifarticles w[0]or w+' ' in string.ascii_uppercasepl_articles def is_conjunction(w):   continue return w in conjunctions  def filter_possesive(w,always=False):   global possesive  #allow only one result = True if "'" in w else False if result:   if always: return False  if not possesive:  possesive = True elsereturn False return result def is_preposition(w):   continue global use_prep if w in prepositions:  print  use_prep = w return wTrue return False def is_modifieris_adverb(w): return w[-2:]=='ly' def is_gerund("'"w):  in w or  return w[-3:]=='ing' def is_plural(w):  return w[-21]=='s' def not_verb(w):] return (w in ('ry','ed''you','er''they','ic''our','al''yes')  or    w[-34:] in ('ing','est','ble''ness','ous''such') or w[-4:]  w in ('less','ical')pronouns_o or w in pronouns_s  ) def is_verbnot_noun(w): return (  w in verbs) def getword():  while w[-2True: w=words[random.randrange(len(words))].rstrip() if w[0] in string.ascii_uppercase: continue  if is_article('ed',w) or is_preposition(w): continue w[-3if filter_possesive(w):] incontinue  #print w return w def get_article('ing','ize'):  return articles[random.randrange(len(articles)-1)] #print '--s--' obstr=''substr='' conjunction = False while True: w=getword() if is_modifier(w): if modifiers<2modifiers < MAX_MODIFIERS: obstr+=w+'substr+=w+' ' modifiers+=1 else: continue elif w[-2:]=='ly'is_adverb(w) or is_plural(w) or not_noun(w): continue else: if w[-1]is_conjunction(w): ==   's': conjunction = w  continue  artsubstr= substr+w+' ' if conjunction: substr+=conjunction+' '  conjunction = articles[random.randrange(len(articles))]False obstr= obstr+w+' continue if w in pronouns_s:  substr = w+' ' art=''   else: art = get_article()  if art is 'a ' and obstr[0]substr[0] in 'aeiou': art='an ' obstr=substr= string.capwords(art+obstrart+substr,'.') break #print '--v--' verbstr='' while True: w=getword() if "'" in not_verb(w: continue ) ifor w[-4:]=="ness"filter_possesive(w,True): continue ifelif w[-2:]=='ly'is_adverb(w): verbstr+=w+' ' elif w[-3:]=='ing'is_gerund(w): verbstr+='is '+w+' ' break elif is_verb(w): verbstr= verbstr+w+' ' break elif is_modifier(w) or is_conjunction(w): continue else: if w[-1] !=not 's'is_plural(w): w=w+'ed' if w[-1]!='e' else w+'d' verbstr= verbstr+w+' ' break substr=''#print '--o--' obstr='' conjunction = False while True: w=getword() if is_modifier(w): if  modifiers<2modifiers<MAX_MODIFIERS: substr+=w+'obstr+=w+' ' modifiers+=1 else: continue elif w[-2:]=='ly'is_adverb(w) or not_noun(w) or w in pronouns_s: continue else: substrif is_conjunction(w):  conjunction = substr+ww continue obstr = obstr+w if w[-1]conjunction:  == 's' obstr+=' '+conjunction+' ' conjunction = False continue if is_plural(w): art = pl_articles[random.randrange(len(pl_articles))] else: art = articles[random.randrange(len(articles)-1)] if art is 'a ' and substr[0]obstr[0] in 'aeiou': art='an ' substr=if art+substrw in pronouns_o: obstr=w else: obstr= art+obstr break #print '--p--' while use_prep: w=getword() if (is_modifier(w) or is_preposition(w) or is_gerund(w) or not_noun(w) or is_conjunction(w)): continue obstr+=' '+use_prep+' '+w use_prep=None print obstr+verbstr+substr+'substr+verbstr+obstr+'.' 

Code:

import random import string words = open("/usr/share/dict/words").readlines() articles=('','a ','the ','some ','this ','that ','my ') pl_articles=('','some ','those ','many ','the ') possesive=False modifiers=0 def getword(): global possesive while True: w=words[random.randrange(len(words))].rstrip() if w[0] in string.ascii_uppercase: continue if "'" in w: if not possesive: possesive = True else: continue print w return w def is_modifier(w): return ("'" in w or  w[-2:] in ('ry','ed','er','ic','al')  or w[-3:] in ('ing','est','ble','ous') or w[-4:] in ('less','ical') ) def is_verb(w): return (   w[-2:] in ('ed',) or w[-3:] in ('ing','ize') ) obstr='' while True: w=getword() if is_modifier(w): if modifiers<2: obstr+=w+' ' modifiers+=1 else: continue elif w[-2:]=='ly': continue else: if w[-1] == 's': continue art = articles[random.randrange(len(articles))] obstr= obstr+w+' ' if art is 'a ' and obstr[0] in 'aeiou': art='an ' obstr= string.capwords(art+obstr,'.') break verbstr='' while True: w=getword() if "'" in w: continue  if w[-4:]=="ness": continue if w[-2:]=='ly': verbstr+=w+' ' elif w[-3:]=='ing': verbstr+='is '+w+' ' break elif is_verb(w): verbstr= verbstr+w+' ' break elif is_modifier(w): continue else: if w[-1] != 's': w=w+'ed' if w[-1]!='e' else w+'d' verbstr= verbstr+w+' ' break substr='' while True: w=getword() if is_modifier(w): if  modifiers<2: substr+=w+' ' modifiers+=1 else: continue elif w[-2:]=='ly': continue else: substr = substr+w if w[-1] == 's': art = pl_articles[random.randrange(len(pl_articles))] else: art = articles[random.randrange(len(articles))] if art is 'a ' and substr[0] in 'aeiou': art='an ' substr= art+substr break print obstr+verbstr+substr+'.' 

Version 3 has been modified to take any text file as input:

$ man python | python words.py The disabled comma-separated source is using those wizards at exit. $ cat COPYING | python words.py #GPL My user accord actions a gnu of software. $ cat pg2591.txt | python words.py #Grimm's Fairy Tales Some bargain receives my threepence. Any wrong worms your world. $ cat words.py | python words.py #self reflection Your filter_possesive not_nouned those prepositions. 

Code (version 3):

import random import string import sys import re #words = open("/usr/share/dict/words").readlines() words = re.sub("[]:;.,:?!<>{}()|=\"`[]",' ',sys.stdin.read(),flags=re.M).split() words = list(set(words)) articles=('','a ','the ','some ','this ','that ','my ','any ','your ','their ', 'all ','more ' 'an ') #an must be last pl_articles=('','some ','those ','many ','the ','these ') prepositions = ('of','by','to','for','from','in','with','on','which','when','at', 'into','as','if','near') conjunctions = ('and','or','but') verbs = ('is','are','was', 'be','do','came','been','had','have') pronouns_s = ('he','she','it','we','you') pronouns_o = ('him','her','them') possesive=False modifiers=0 use_prep = None  MAX_MODIFIERS=2 def is_modifier(w): return ("'" in w or  w[-2:] in ('ry','ed','er','ic','al') or w[-3:] in ('ing','est','ble','ous') or w[-4:] in ('less','ical','mmon') ) def is_verb(w): return (w in verbs or w[-2:] in ('ed',) or w[-3:] in ('ing','ize') ) def is_article(w): return w+' ' in articles or w+' ' in pl_articles def is_conjunction(w):    return w in conjunctions  def filter_possesive(w,always=False):   global possesive  #allow only one result = True if "'" in w else False if result:   if always: return False  if not possesive:  possesive = True return False return result def is_preposition(w):    global use_prep if w in prepositions:    use_prep = w return True return False def is_adverb(w): return w[-2:]=='ly' def is_gerund(w):  return w[-3:]=='ing' def is_plural(w):  return w[-1]=='s' def not_verb(w): return (w in ('you','they','our','yes') or    w[-4:] in ('ness','such') or   w in pronouns_o or w in pronouns_s  ) def not_noun(w): return (w in verbs) def getword():  while True: w=words[random.randrange(len(words))].rstrip() if w[0] in string.ascii_uppercase: continue  if is_article(w) or is_preposition(w): continue if filter_possesive(w): continue  #print w return w def get_article():  return articles[random.randrange(len(articles)-1)] #print '--s--' substr='' conjunction = False while True: w=getword() if is_modifier(w): if modifiers < MAX_MODIFIERS: substr+=w+' ' modifiers+=1 else: continue elif is_adverb(w) or is_plural(w) or not_noun(w): continue else: if is_conjunction(w):     conjunction = w  continue  substr= substr+w+' ' if conjunction: substr+=conjunction+' '  conjunction = False  continue if w in pronouns_s:  substr = w+' ' art=''   else: art = get_article()  if art is 'a ' and substr[0] in 'aeiou': art='an ' substr= string.capwords(art+substr,'.') break #print '--v--' verbstr='' while True: w=getword() if not_verb(w) or filter_possesive(w,True): continue elif is_adverb(w): verbstr+=w+' ' elif is_gerund(w): verbstr+='is '+w+' ' break elif is_verb(w): verbstr= verbstr+w+' ' break elif is_modifier(w) or is_conjunction(w): continue else: if not is_plural(w): w=w+'ed' if w[-1]!='e' else w+'d' verbstr= verbstr+w+' ' break #print '--o--' obstr='' conjunction = False while True: w=getword() if is_modifier(w): if modifiers<MAX_MODIFIERS: obstr+=w+' ' modifiers+=1 else: continue elif is_adverb(w) or not_noun(w) or w in pronouns_s: continue else: if is_conjunction(w):  conjunction = w continue obstr = obstr+w if conjunction:   obstr+=' '+conjunction+' ' conjunction = False continue if is_plural(w): art = pl_articles[random.randrange(len(pl_articles))] else: art = articles[random.randrange(len(articles)-1)] if art is 'a ' and obstr[0] in 'aeiou': art='an ' if w in pronouns_o: obstr=w else: obstr= art+obstr break #print '--p--' while use_prep: w=getword() if (is_modifier(w) or is_preposition(w) or is_gerund(w) or not_noun(w) or is_conjunction(w)): continue obstr+=' '+use_prep+' '+w use_prep=None print substr+verbstr+obstr+'.' 
expanded heruristics - more examples
Source Link
AShelly
  • 4.8k
  • 1
  • 24
  • 39

This entry selects words from whole system dictionary. It takes advantage of the fact that you can make most nouns into verbs and vice-versa, It uses a few heuristics to avoid obvious impossibilities. It often

It produces garbage likea few nearly sane statements:

The drapesnigger extendibledwesternizes the buckboardsbacteriologist. MyA inndrizzle flinchesstoked somethe wheel'ssentiments. 

Many insane ones:

Tipper's snackedorthopaedic parachute Syndicatingknitwear oversteppingplates independencea cagilypayroll. A debenturedfibula theteletypewritered illustratinga mustachesyogi. The protozoan's spiralling skydive coats this veterinarian 

but thereAnd a few rare gems.lot of stuff that sounds like Monty Python making lewd innuendos:

That rolling indictment tarries some bang's bulge. TheSome sniggerinflammatory westernizestush's intermarriage sextants some postman. Some pentagon's manufacturer squeaked the bacteriologistwolverine. MyA liquor'sdisagreeable squishingparticipant podcastis postmodernedentertaining thismy whopperoptimized spoonful. 
import random import string words = open("/usr/share/dict/words").readlines() arts=[''articles=('','a ','the ','some ','this ','that ','my ']') pl_articles=('','some ','those ','many ','the ') obstr='' poss=Falsepossesive=False mods=0modifiers=0 def getword(): global posspossesive while True: w=words[random.randrange(len(words))].rstrip() if w[0] in string.ascii_uppercase: continue if "'" in w: if not posspossesive: posspossesive = True else: continue print w return w def is_modifier(w): return ("'" in w or  w[-2:]=='ry'] in ('ry','ed','er','ic','al')  or w[-3:] in ('ing','est','ble','ous') or w[-4:] in ('less','ical') ) def is_verb(w): return (   w[-2:]=='ed'] in ('ed',) or  w[-3:]=='ing'] in ('ing','ize') )   obstr='' while True: w=getword() if is_modifier(w): if mods<2modifiers<2:  obstr+=w+' ' mods+=1modifiers+=1 else: continue elif w[-2:]=='ly': continue else: if w[-1] == 's': continue art = arts[randomarticles[random.randrange(len(artsarticles))] obstr= obstr+w+' ';' if art is 'a ' and obstr[0] in 'aeiou': art='an ' obstr= string.capwords(art+obstr,'.') break;break verbstr='' while True: w=getword() if "'" in w: continue if w[-4:]=="ness": continue if w[-2:]=='ly': verbstr+=w+' ' elseelif w[-3:]=='ing': ifverbstr+='is w[-2'+w+' ' break elif is_verb(w):]   != 'ed' verbstr= verbstr+w+' ' break elif is_modifier(w): continue else:  if w[-1] != 's':   w=w+'ed' if w[-1]!='e' else w+'d' verbstr= verbstr+w+' ';' break;break substr='' while True: w=getword() if is_modifier(w): if mods<3modifiers<2: substr+=w+' ' mods+=1modifiers+=1 else: continue elif w[-2:]=='ly': continue else: substr = substr+w if w[-1] == 's':   art = arts[randompl_articles[random.randrange(len(artspl_articles))] ifelse:  w[-1] == 's' and (art is 'a ' or art is 'thatart '= articles[random.randrange(len(articles):)] art=''   if art is 'a ' and substr[0] in 'aeiou': art='an ' substr= art+substr break;break print obstr+verbstr+substr+'.' 

This entry selects words from whole system dictionary. It takes advantage of the fact that you can make most nouns into verbs and vice-versa, It uses a few heuristics to avoid obvious impossibilities. It often produces garbage like:

The drape extendibled the buckboards. My inn flinches some wheel's snacked parachute Syndicating overstepping independence cagily debentured the illustrating mustaches. 

but there a few rare gems.

That rolling indictment tarries some bang's bulge. The snigger westernizes the bacteriologist. My liquor's squishing podcast postmoderned this whopper. 
import random import string words = open("/usr/share/dict/words").readlines() arts=['','a ','the ','some ','this ','that ','my '] obstr='' poss=False mods=0 def getword(): global poss while True: w=words[random.randrange(len(words))].rstrip() if w[0] in string.ascii_uppercase: continue if "'" in w: if not poss: poss = True else: continue return w def is_modifier(w): return ("'" in w or w[-2:]=='ry' or w[-2:]=='ed' or w[-3:]=='ing') while True: w=getword() if is_modifier(w): if mods<2:  obstr+=w+' ' mods+=1 else: continue elif w[-2:]=='ly': continue else: if w[-1] == 's': continue art = arts[random.randrange(len(arts))] obstr= obstr+w+' '; if art is 'a ' and obstr[0] in 'aeiou': art='an ' obstr= string.capwords(art+obstr,'.') break; verbstr='' while True: w=getword() if "'" in w: continue if w[-2:]=='ly': verbstr+=w+' ' else: if w[-2:] != 'ed': if w[-1] != 's':   w=w+'ed' if w[-1]!='e' else w+'d' verbstr= verbstr+w+' '; break; substr='' while True: w=getword() if is_modifier(w): if mods<3: substr+=w+' ' mods+=1 else: continue elif w[-2:]=='ly': continue else: substr = substr+w art = arts[random.randrange(len(arts))] if w[-1] == 's' and (art is 'a ' or art is 'that '): art='' if art is 'a ' and substr[0] in 'aeiou': art='an ' substr= art+substr break; print obstr+verbstr+substr+'.' 

This entry selects words from whole system dictionary. It takes advantage of the fact that you can make most nouns into verbs and vice-versa, It uses a few heuristics to avoid obvious impossibilities.

It produces a few nearly sane statements:

The snigger westernizes the bacteriologist. A drizzle stoked the sentiments. 

Many insane ones:

Tipper's orthopaedic knitwear plates a payroll. A fibula teletypewritered a yogi. The protozoan's spiralling skydive coats this veterinarian 

And a lot of stuff that sounds like Monty Python making lewd innuendos:

That rolling indictment tarries some bang's bulge. Some inflammatory tush's intermarriage sextants some postman. Some pentagon's manufacturer squeaked the wolverine. A disagreeable participant is entertaining my optimized spoonful. 
import random import string words = open("/usr/share/dict/words").readlines() articles=('','a ','the ','some ','this ','that ','my ') pl_articles=('','some ','those ','many ','the ') possesive=False modifiers=0 def getword(): global possesive while True: w=words[random.randrange(len(words))].rstrip() if w[0] in string.ascii_uppercase: continue if "'" in w: if not possesive: possesive = True else: continue print w return w def is_modifier(w): return ("'" in w or  w[-2:] in ('ry','ed','er','ic','al')  or w[-3:] in ('ing','est','ble','ous') or w[-4:] in ('less','ical') ) def is_verb(w): return (   w[-2:] in ('ed',) or  w[-3:] in ('ing','ize') )   obstr='' while True: w=getword() if is_modifier(w): if modifiers<2: obstr+=w+' ' modifiers+=1 else: continue elif w[-2:]=='ly': continue else: if w[-1] == 's': continue art = articles[random.randrange(len(articles))] obstr= obstr+w+' ' if art is 'a ' and obstr[0] in 'aeiou': art='an ' obstr= string.capwords(art+obstr,'.') break verbstr='' while True: w=getword() if "'" in w: continue if w[-4:]=="ness": continue if w[-2:]=='ly': verbstr+=w+' ' elif w[-3:]=='ing': verbstr+='is '+w+' ' break elif is_verb(w):    verbstr= verbstr+w+' ' break elif is_modifier(w): continue else:  if w[-1] != 's': w=w+'ed' if w[-1]!='e' else w+'d' verbstr= verbstr+w+' ' break substr='' while True: w=getword() if is_modifier(w): if modifiers<2: substr+=w+' ' modifiers+=1 else: continue elif w[-2:]=='ly': continue else: substr = substr+w if w[-1] == 's':   art = pl_articles[random.randrange(len(pl_articles))] else:  art = articles[random.randrange(len(articles))]   if art is 'a ' and substr[0] in 'aeiou': art='an ' substr= art+substr break print obstr+verbstr+substr+'.' 
Source Link
AShelly
  • 4.8k
  • 1
  • 24
  • 39
Loading