How should I structure a Python procedural story generator with character traits?

0
6
Asked By MellowOrbit42 On

I'm building a personal Python project that generates stories from my own writing. It currently uses the random library, nested lists, and functions that choose words or phrases based on the situation. For example, a character might sit down and the program could produce either "Xena sat lazily on the ledge" or "Our heroine rested her legs upon the cliff side."

I'd like actions such as sitting to consider the character and their emotional state. Different characters might have distinct styles, such as cool, elegant, or brutish, and those traits could change during the story because of events like a personality swap, training, or some other transformation.

I'm wondering how best to organize this. Should I use dictionaries, dataclasses, polymorphism, recursion, linked lists, or another design? I'd also appreciate suggestions for keeping the generated prose varied while making it easy to add new characters, traits, actions, and word choices.

4 Answers

Answered By AmberVale64 On

Polymorphism is another reasonable option if characters genuinely perform actions differently. A base Character class could define a default sitting style, while a specialized character overrides that method. However, inheritance can become unwieldy if every combination of personality and mood needs its own subclass. Trait-based composition is usually more flexible: keep style, mood, history, and temporary modifiers as state, then let the action system choose a phrase from the relevant combinations. You also may want to store both present and past-tense forms so the grammar can use the result correctly.

Answered By VelvetLynx8 On

You probably don't need a giant chain of if/elif statements. Store each character's traits in a dictionary or dataclass, then have action data organized by action and style. For example, an action lookup could use something like actions["sit"][character.style] to retrieve possible phrases before choosing one at random. A personality change then only requires updating the character's style, rather than rewriting every action function. Keeping the word banks as data, perhaps in dictionaries or a separate JSON file, will also make the system much easier to expand.

QuietHarbor31 -

You can still use functions for actions when they need more logic, but let those functions read the character's current traits instead of having a separate implementation for every character.

Answered By SilverPine27 On

Use a dedicated random number generator or set a seed while developing. That way, the same seed produces the same story, which makes bugs much easier to reproduce and lets you compare changes to the generator. Once the basic system works, weighted choices can help too: common phrases can have higher weights while unusual ones appear occasionally, preventing the output from feeling repetitive without making it completely random.

Answered By CedarFox17 On

Look into context-free grammars. They let you define rules for expanding symbols into larger pieces of text, which is very close to what you're describing. A grammar-based approach can keep the sentence structure separate from the actual word choices. Once traits such as "brutish" affect which expansions are allowed, you may also want a constraint-based system that selects phrases matching several properties at once. This is also related to social and behavioral simulation, where characters have state that changes over time.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.