#!/usr/bin/env python import random import re import string class Token(): def __init__(self, string): cleaned_words = re.sub(r"[\.,\?!'\"=:;#\&\*\(\)\[\]\{\}]*", "", string) self.words = cleaned_words.split() def empty(self): if len(self.words) == 0: return True def contains(self, word): if len(self.words) > 0: result = [True for w in self.words if w.lower() == word.lower()] if len(result) > 0: return result[0] def character_has(holder, item): """ Return true if character, holder, has an item. Where item is probably a string name to match an item's key. It can also be a type, for instance: character_has(character, typeclasses.drinkables.TeaCup) """ for i in holder.contents: if isinstance(item, str) and i.key == item: return i elif type(i) == item: return i elif i == item: return i def squish(text): "Remove series of spaces from the text." return re.sub('[ \n\t]+', ' ', text).strip() 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))