Python Forum
Obligatory fish out of water -- seeking opinion on gettind started
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Obligatory fish out of water -- seeking opinion on gettind started
#1
Hello all,

I will try to make this as painless as possible for all of us--I'm just looking for some opinions on the best place to get started here.
I am a writer--not a coder. I was struck by an idea recently, and I seem to have fallen down a wormhole.
Let me explain: I am interested in creating a computer generated novel. In college, I wrote a novel. It's terrible. I have since written better things, but some recent inspiration has inspired to use the terrible novel I've written as fodder for a better novel, and it seems Python is possibly the tool I need to use to reach my destination.
The problem is, it seems like learning an entire programming language is an obstacle to getting my project off the ground; so, I was going to jump in and see how far I get before I throw in the towel.
My ideal goal is to write some code that will contain, essentially, 10 - 13 paragraphs that function like Mad Libs, with certain elements being pulled from a list of variables (predefined by me. So, one example might be the following:


"I took a [bus, car, bike, train] across town."


Let's call that a paragraph. I want my program to randomly select one of those modes of transportation every time it creates that paragraph. And I want that paragraph (and nine or 10 others, all with variables) to print over and over and over and over again, to about 100,000 words.
Of the hundreds and hundreds of hours of tutorials laid out before me, I am not certain which path I should embark upon to get to my destination. The reader in me was drawn to The Hitchhiker's Guide to Python! as a starting point; any opinions would be welcome.

Thanks for reading my ramble--cheers!
Reply
#2
here's some things to look at:
https://github.com/willf/NaNoGenMo
https://www.gadgetdaily.xyz/make-a-visua...th-python/
https://www.pydanny.com/using-python-and...books.html
https://www.analyticsvidhya.com/blog/201...ython-nlp/
https://opensource.com/business/15/10/ja...-on-python
Reply
#3
(Jul-17-2018, 01:47 AM)Larz60+ Wrote: here's some things to look at:
https://github.com/willf/NaNoGenMo
https://www.gadgetdaily.xyz/make-a-visua...th-python/
https://www.pydanny.com/using-python-and...books.html
https://www.analyticsvidhya.com/blog/201...ython-nlp/
https://opensource.com/business/15/10/ja...-on-python

Thanks so much for the tips—greatly appreciated. I’ll dive in and see where I get. Wink
Reply
#4
What you are talking about would not be hard at all. Any decent starting book would get you the basics of the language, and then maybe you need to delve just a little deeper into string methods and the random module.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
As ichabood801 said it's not very hard.
here is basic example using json file to supply raw paragraphs.

paragraphs.json
Output:
[ { "sentence": "I took a {vehicle} across town.", "variables": { "vehicle": [ "bus", "car", "bike", "train" ] } }, { "sentence": "It was {which} {period} of {time_of_year}.", "variables": { "which": [ "first", "last", "second", "third" ], "period": [ "week", "weekend" ], "time_of_year": [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ] } } ]
book.py
import random
import json

def create_chapter(paragraphs):
    chapter = []
    for para in paragraphs:
        sentence = para["sentence"]
        vars = {key:random.choice(values) for key, values in para["variables"].items()}
        chapter.append(sentence.format(**vars))
    return " ".join(chapter)

    
def create_book(chapters, paragraphs):
    book = []
    for chapter in range(1, chapters+1):
        book.append('Chapter {}'.format(chapter))
        book.append(create_chapter(paragraphs=paragraphs))
    return '\n'.join(book)

if __name__ == '__main__':
    with open('paragraphs.json') as f:
        paragraphs = json.load(f)
        
    # create book
    book = create_book(chapters = 4, paragraphs=paragraphs)
    print(book)
    print('\n****\n')
    #single chapter
    print(create_chapter(paragraphs=paragraphs))
Output:
Chapter 1 I took a bus across town. It was third weekend of October. Chapter 2 I took a bus across town. It was second weekend of August. Chapter 3 I took a bus across town. It was first weekend of March. Chapter 4 I took a car across town. It was second week of February. **** I took a bike across town. It was last weekend of May.
Of course this could be done also using object-oriented programming (OOP) - more advanced example/approach
import random
import json


class Sentence:
    def __init__(self, sentence, variables):
        self.sentence = sentence
        self.variables = {key:random.choice(values) for key, values in variables}
 
    @property
    def text(self):
        return self.sentence.format(**self.variables)
        
    def __str__(self):
        return self.text

        
class Chapter:
    def __init__(self, paragraphs):
        self.sentences = []
        for para in paragraphs:
            sentence = para["sentence"]
            sentence_vars = para["variables"].items()
            self.sentences.append(Sentence(sentence, sentence_vars))
        
    def __str__(self):
        return self.text
        
    @property  
    def text(self):
        return ' '.join(sentence.text for sentence in self.sentences)

        
class Book:
    def __init__ (self, chapters, paragraphs):
        self.paragraphs = paragraphs
        self.chapters = [Chapter(paragraphs=self.paragraphs) for _ in range(chapters)]
            
    def add_chapter(self):
        self.chapters.append(Chapter(paragraphs=self.paragraphs))
    
    @property
    def num_chapters(self):
        return len(self.chapters)
    
    @property
    def text(self):
        return '\n'.join(chap.text for chap in self.chapters)
        
    def __str__(self):
        return self.text


if __name__ == '__main__':
    with open('paragraphs.json') as f:
        paragraphs = json.load(f)
        
    # create book
    book = Book(chapters=3, paragraphs=paragraphs)
    print('Your book has {} chapters'.format(book.num_chapters))
    print(book)
    print('\n****\n')
    book.add_chapter()
    print('Now your book has {} chapters'.format(book.num_chapters))
    print(book)
Output:
Your book has 3 chapters I took a bike across town. It was last week of July. I took a car across town. It was second week of February. I took a train across town. It was third weekend of December. **** Now your book has 4 chapters I took a bike across town. It was last week of July. I took a car across town. It was second week of February. I took a train across town. It was third weekend of December. I took a bike across town. It was first week of March.
Now, this are very basic examples, but they will give you an idea how easy it is. Of course you can use different setup, e.g. not json for input. I used it to separate logic from the source text.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cannot get started standenman 4 1,150 Feb-22-2023, 05:25 PM
Last Post: standenman
  Opinion: how should my scripts cache web download files? stevendaprano 0 705 Dec-17-2022, 12:19 AM
Last Post: stevendaprano
  Need help i just started the whole thing gabriel789 16 3,097 Sep-12-2022, 08:04 PM
Last Post: snippsat
  Can't even get started dr6 1 1,587 Aug-18-2020, 04:38 PM
Last Post: Larz60+
  New user seeking help EdRaponi 2 37,950 Jun-23-2020, 12:03 PM
Last Post: EdRaponi
  Getting started mba_110 0 1,699 Jan-18-2019, 05:23 PM
Last Post: mba_110
  seeking suggestions for function option name Skaperen 1 2,514 Dec-22-2018, 05:27 AM
Last Post: Gribouillis
  Newbie seeking help with DNS query DaytonJones 0 2,189 Sep-21-2018, 06:29 PM
Last Post: DaytonJones
  Getting Started wargasme 5 3,362 Jun-19-2018, 07:25 PM
Last Post: ichabod801
  Just getting started alwillia 6 3,531 May-20-2018, 07:22 PM
Last Post: ljmetzger

Forum Jump:

User Panel Messages

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