#!/usr/bin/env python import random import re def squish(text): "Remove series of spaces from the text." return re.sub('[ \n\t]+', ' ', text).strip() def routput(text, *substitutions): """ 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.' """ if text: acc = [] for s in text.split("{{"): selections, *rest = s.split("}}") choice = random.choice(re.split(r"\s*\^\s*", selections)) acc = acc + [choice] + rest proposal = squish(''.join(acc).format(*substitutions)) # If a choice is at the end of a sentence, and we could have # "no choice", as in: # "He searches {{thoroughly ^ quickly ^}}." # We don't want a version that looks like: # "He searches # ." # with a space before the punctuation: return re.sub(r"\s+([!?.,])", "\\1", proposal) def choices(text, *substitutions): """ Returns a section of 'text' based on separating semicolons. Note that text can already be separated as a list or tuple. """ if isinstance(text, str): selections = re.split(r"\s*;;\s*", text) elif isinstance(text, (tuple, list)): selections = text if selections: return routput(random.choice(selections), *substitutions) def split_party_msg(viewer, msg): # First a message for the view: viewer.msg( re.sub("", "You", re.sub("", "you", msg)) ) # Then the message for the rest of the area: viewer.location.msg_contents(re.sub("<[Yy]ou>", viewer.name.title(), msg), exclude=viewer) # def searsonal(text, **kwargs): # season = kwargs['season'] or kwargs['location'].get_season() # time_of_day = kwargs['time_of_day'] or kwargs['location'].get_time_of_day()