Python Forum
Obligatory fish out of water -- seeking opinion on gettind started - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Obligatory fish out of water -- seeking opinion on gettind started (/thread-11582.html)



Obligatory fish out of water -- seeking opinion on gettind started - griffinxi - Jul-17-2018

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!


RE: Obligatory fish out of water -- seeking opinion on gettind started - Larz60+ - Jul-17-2018

here's some things to look at:
https://github.com/willf/NaNoGenMo
https://www.gadgetdaily.xyz/make-a-visual-novel-with-python/
https://www.pydanny.com/using-python-and-google-docs-to-build-books.html
https://www.analyticsvidhya.com/blog/2018/03/text-generation-using-python-nlp/
https://opensource.com/business/15/10/jane-austen-on-python


RE: Obligatory fish out of water -- seeking opinion on gettind started - griffinxi - Jul-17-2018

(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-visual-novel-with-python/
https://www.pydanny.com/using-python-and-google-docs-to-build-books.html
https://www.analyticsvidhya.com/blog/2018/03/text-generation-using-python-nlp/
https://opensource.com/business/15/10/jane-austen-on-python

Thanks so much for the tips—greatly appreciated. I’ll dive in and see where I get. Wink


RE: Obligatory fish out of water -- seeking opinion on gettind started - ichabod801 - Jul-17-2018

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.


RE: Obligatory fish out of water -- seeking opinion on gettind started - buran - Jul-17-2018

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.