Created
November 13, 2024 23:11
-
-
Save s3rgeym/72b388b6114c89dfea80d0351ca39580 to your computer and use it in GitHub Desktop.
Randomize Text via Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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