SS-NT / ssnt

A remake of the game "Space Station 13" in 3D
20 stars 1 forks source link

speech accents system #26

Closed Fogapod closed 8 months ago

Fogapod commented 1 year ago

Accents are defined using ron, see examples in tests. Syntax explanation, also exists in accents/example.ron

// substitues pattern matches with replacements. all rules are applied from top to bottom
(
    // accent name, the only required field
    name: "example",

    // pairs of (regex, replacement)
    // this is same as `patterns` except that each regex is surrounded with \b to avoid copypasting.
    // `words` are applied before `patterns`
    words: [
        // this is the simplest rule to replace all "windows" words (separated by regex \b) 
        // occurences with "linux", case sensitive
        ("windows", Simple("linux")),
        // this replaces word "OS" with one of replacements, with equal probability
        ("OS", Any([
            Simple("Ubuntu"),
            Simple("Arch"),
            Simple("Gentoo"),
        ])),
    ],

    // pairs of (regex, replacement)
    // this is same as `words` except these are used as is, without \b
    patterns: [
        // inserts one of the honks. first value of `Weights` is relative weight. higher is better
        ("$", Weights([
            (32, Simple(" HONK!")),
            (16, Simple(" HONK HONK!")),
            (08, Simple(" HONK HONK HONK!")),
            // ultra rare sigma honk - 1 / 56
            (01, Simple(" HONK HONK HONK HONK!!!!!!!!!!!!!!!")),
        ])),
    ],

    // accent can be used with severity (non negative value). higher severities can either extend
    // lower level or completely replace it.
    // default severity is 0. higher ones are defined here
    severities: {
        // extends previous severity (level 0, base one in this case), adding additional rules
        // below existingones. words and patterns keep their relative order though - words are
        // processed first
        1: Extend(
            (
                words: [
                    // even though we are extending, defining same rule will overwrite result.
                    // relative order of rules remain the same: "windows" will remain first
                    ("windows", Simple("windoos")),
                ],

                // extend patterns, adding 1 more rule
                patterns: [
                    // replacements can be nested arbitrarily
                    ("[A-Z]", Weights([
                        // 50% to replace capital letter with one of the Es
                        (1, Any([
                            Simple("E"),
                            Simple("Ē"),
                            Simple("Ê"),
                            Simple("Ë"),
                            Simple("È"),
                            Simple("É"),
                        ])),
                        // 50% to do nothing, no replacement
                        (1, Noop),
                    ])),
                ],
            ),
        ),
        // replace severity 1 entirely. in this case with nothing. remove all rules on severity 2+
        2: Replace(()),
    },
)

Accents support severity starting from 0. This allows increasing slurred speech, adding more honks, adding more words etc as number go higher. Currently i have no idea how you would define more complex accents that would need rust code.

Usage:

let accent = Accent::from_str(ron_string).unwrap();
// higher is stronger as long as accent defines higher severities
let severity = 5;
let replaced = accent.apply("Hello World", severity);
Fogapod commented 1 year ago

Moved accent code to separate repo at https://github.com/Fogapod/pink_accents

Syntax is the same except that i added name and description fields and accent itself exists in rules field

Fogapod commented 1 year ago

I'm not entirely sure how to integrate it into the chat right now

my very short term plan was to add a menu near or within chat box that lets you click on accents to enable/disable each one. havent thought further, this is just a prototype

Fogapod commented 8 months ago

This is too outdated, I will start from scratch