Skip to content

Instantly share code, notes, and snippets.

@s3rgeym
Created November 13, 2024 23:11
Show Gist options
  • Save s3rgeym/72b388b6114c89dfea80d0351ca39580 to your computer and use it in GitHub Desktop.
Save s3rgeym/72b388b6114c89dfea80d0351ca39580 to your computer and use it in GitHub Desktop.
Randomize Text via Python
In [4]: def random_text(s:str)->str:
...: while (res:=re.sub(r'{([^{}]+)}', lambda m: random.choice(m.group(1).split('|')),s))!=s:
...: s=res
...: return s
...:
In [5]: random_text('{Ку|Привет}')
Out[5]: 'Привет'
In [6]: random_text('{Ку|Привет}, {как {ты|сам}|что делаешь}?')
Out[6]: 'Привет, как сам?'
In [7]: random_text('{Ку|Привет}, {как {ты|сам}|что делаешь}?')
Out[7]: 'Ку, как ты?'
In [8]: random_text('{Ку|Привет}, {как {ты|сам}|что делаешь}?')
Out[8]: 'Ку, как сам?'
In [9]: random_text('{Ку|Привет}, {как {ты|сам}|что делаешь}?')
Out[9]: 'Привет, как ты?'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment