Created
December 6, 2020 23:18
-
-
Save redoxate/fba78a74d278283a08cf5203d8979b0e to your computer and use it in GitHub Desktop.
shuffle and spin text
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
import re | |
import random | |
import spintax | |
''' | |
Code snippet to shuffle or scramble paragraphs before passing theme to spintax | |
Changing the order of paragraphs can be a pretty good way of raising the number of possible combination | |
Let's generate unique (?) content ! | |
Syntax : | |
{Hey|Hello|Hi} [ How you doing | {Mohamed Reda WICHOU|Mohamed WICHOU}] | |
shuffleSpin.generate('{Hey|Hello|Hi} [ How you doing | {Mohamed Reda WICHOU|Mohamed WICHOU}]') | |
Output : | |
- Hi How you doing Mohamed Reda WICHOU | |
- Hey Mohamed WICHOU How you doing | |
''' | |
class shuffleSpin(): | |
@staticmethod | |
def divide_and_shuffle(match): | |
text = match.group() | |
text = text[1:-1] | |
parts = re.split(r'[|]', text) | |
random.shuffle(parts) | |
return ''.join(parts) | |
@staticmethod | |
def generate(s): | |
# Everyday I'm shuffling .. | |
spinned = spintax.spin(s) | |
regex = re.compile(r'\[(.*?)\]') | |
result = regex.sub(shuffleSpin.divide_and_shuffle, spinned) | |
return spintax.spin(result) | |
#print(shuffleSpin.generate('{Hey|Hello|Hi} [ How you doing | {Mohamed Reda WICHOU|Mohamed WICHOU}]')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment