Python Forum
Random Sentance Generator
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Random Sentance Generator
#1
Hey guys so I need to write a Random Sentance Generator that reads 
nouns.
adjectives,
adverbs,
conjunctions,
Intransitive verbs,
Leadin,
Nounmarkes,
Transitive verbs 

From files that contain these parameters then  produces random but structurally correct English sentences, according to the syntax (rules of grammar).

I have the contents of these files made up in txt files ready to go yet I am just confused as to how to read all of them and put this into the code.

I have written an example program to test random just with some different variables

import random

s_nouns = ["A dude", "My mom", "The king", "Some guy", "A cat with rabies", "A sloth", "Your homie", "This cool guy my gardener met yesterday", "Superman"]
p_nouns = ["These dudes", "Both of my moms", "All the kings of the world", "Some guys", "All of a cattery's cats", "The multitude of sloths living under your bed", "Your homies", "Like, these, like, all these people", "Supermen"]
s_verbs = ["eats", "kicks", "gives", "treats", "meets with", "creates", "hacks", "configures", "spies on", "retards", "meows on", "flees from", "tries to automate", "explodes"]
p_verbs = ["eat", "kick", "give", "treat", "meet with", "create", "hack", "configure", "spy on", "retard", "meow on", "flee from", "try to automate", "explode"]
infinitives = ["to make a pie.", "for no apparent reason.", "because the sky is green.", "for a disease.", "to be able to make toast explode.", "to know more about archeology."]

def sing_sen_maker():
    '''Makes a random senctence from the different parts of speech. Uses a SINGULAR subject'''
    if input("Would you like to add a new word?").lower() == "yes":
        new_word = input("Please enter a singular noun.")
        s_nouns.append(new_word)
    else:
    print (random.choice(s_nouns), random.choice(s_verbs), random.choice(s_nouns).lower() or random.choice(p_nouns).lower(), random.choice(infinitives))
sing_sen_maker()
Also at some point I will need to include the following commands available using a menu and prompt for a command.

CMD Effect L Load all the files of words from disk. 
T Test – display the first word from each list to make sure they've been loaded. 
E Easy sentence: display a two word sentence - a randomly selected noun followed by a randomly selected intransitive verb and then a full stop. 
S new Sentence - generate & display a sentence If the wordlists haven't been loaded, display an error message.
Q Quit the program.
Reply
#2
(May-19-2017, 07:21 AM)Liquid_Ocelot Wrote: I have the contents of these files made up in txt files ready to go yet I am just confused as to how to read all of them and put this into the code.

If the file has one word per line, you would want something like this:

with open('file_name.txt') as word_file:
    words = [line.strip() for line in word_file]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,408 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  Random quiz generator Truman 16 14,577 Jun-01-2018, 05:23 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020