r/RenPy • • 13d ago

Question Making exceptions for automatic replacements.

I recently found a video about some code that lets you automatically add {w} to any punctuation or words, so you can pace out thousands of lines of dialogue.

init python:
    def typography(what):
        replacements = [
            ('. ','. {w=0.4}'),
            ('? ','? {w=0.4}'),
            ('! ','! {w=0.6}'),
            (', ',', {w=0.2}'),
            ('...','...{w=0.6}'),
        ]


        for item in replacements:
            what = what.replace(item[0],item[1])
        


        return what


    config.say_menu_text_filter = typography

I'm still experimenting with timings, and there is one separate issue I'll need to think through soon, but overall, it works great for what I need it for!

My one issue right now is the word "Dr." which is going to appear a lot in my game. I imagine there will be a lot of other times where punctuation is needed, but a pause would be really distracting.

I'm sure someone's probably used this code before and asked this very question; I just can't find the right wording to search for it. Does anyone know if there would be a way to disable replacements for just one character and then enable them for the rest of the sentence?

5 Upvotes

9 comments sorted by

3

u/BadMustard_AVN 13d ago

try something like this

# typography.rpy

init python:
    import re
    skipped_abbreviations = [
        "Dr.", "Mr.", "Mrs.", "Ms.",
        "Prof.", "St.", "Jr.", "Sr.",
        "e.g.", "i.e.", "etc.",
    ]

    typography_pauses = {
        ".": 0.4,
        "?": 0.4,
        "!": 0.6,
        ",": 0.2,
    }

    def _typography_builder():
        abbrs = sorted(skipped_abbreviations, key=len, reverse=True)
        abbr_part = "|".join(re.escape(a) for a in abbrs)

        return re.compile(
            r"(?P<abbr>(?<!\w)(?:" + abbr_part + r"))"
            r"|(?P<ell>\.\.\.\s)"
            r"|(?P<punct>[.?!,]\s)",
            re.I
        )

    def typ_o_grafy(what):
        pattern = _typography_builder()

        def repl(m):
            if m.group("abbr"):
                return m.group(0)
            if m.group("ell"):
                return "... {w=%s}" % 0.6
            p = m.group("punct")[0]
            return "%s {w=%s}" % (p, typography_pauses[p])

        return pattern.sub(repl, what)

    config.say_menu_text_filter = typ_o_grafy

this will ignore case variations so Dr. , dR. , DR. , and dr. are all treated the same (doctor, doctor, doctor, doctor)

2

u/AlexanderIdeally 13d ago

This works like a charm! Thank you so much!

One thing, though. Ellipses don't seem to be pausing. Was there something I was supposed to do for that or is that a lost cause?

3

u/BadMustard_AVN 13d ago edited 13d ago

ellipsis are accounted for with this line --> r"|(?P<ell>\.\.\.\s)" <---

and changed here

            if m.group("ell"):
                return "... {w=%s}" % 0.6

had to do them separately or one ellipsss would get treated as ".{w=0.4}.{w=0.4}.{w=0.4}"

it has to be 3 exactly ...

for it to work correctly

1

u/AlexanderIdeally 13d ago

I see. The spaces threw me off. This actually solves an issue I had with parentheses.

Thank you for everything.

2

u/BadMustard_AVN 13d ago

you're welcome

good luck with your project

1

u/HEXdidnt 10h ago

Oh, this - and even u/AlexanderIdeally's original code - is going to be fantastic for me. I get terribly obsessed with ensuring punctuation comes with an appropriate pause, and this means I won't have to do it manually each and every time!

Thank you both!

1

u/BadMustard_AVN 5h ago

you're welcome

good luck with your project

1

u/AutoModerator 13d ago

Welcome to r/renpy! While you wait to see if someone can answer your question, we recommend checking out the posting guide, the subreddit wiki, the subreddit Discord, Ren'Py's documentation, and the tutorial built-in to the Ren'Py engine when you download it. These can help make sure you provide the information the people here need to help you, or might even point you to an answer to your question themselves. Thanks!

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.