#!/usr/bin/env python import random import re import string class Token(): def __init__(self, string): self.words = ''.join(ch for ch in string if ch not in string.punctuation).split() def contains(self, word): if len(self.words) > 0: return [True for w in self.words if w == word][0] else: return False def squish(text): "Remove series of spaces from the text." return re.sub(' *', ' ', text) def routput(string): """ Return string with internal word choices replaced randomly. For instance, the string: 'This feels [|very|quite] [nice|cozy|comfortable].' Could return any of the following strings: 'This feels quite nice.' 'This feels cozy.' 'This feels very comfortable.' """ # string = acc = [] for s in string.split("["): choices, *rest = s.split("]") choice = random.choice(choices.split('|')) acc = acc + [choice] + rest return squish(''.join(acc))