Quite pleased with the results. This also fixed a utils issue to see if an object has an item. I'm surprised this isn't in the standard library.
46 lines
1.2 KiB
Python
Executable file
46 lines
1.2 KiB
Python
Executable file
#!/usr/bin/env python
|
|
|
|
import random
|
|
import re
|
|
import string
|
|
|
|
class Token():
|
|
def __init__(self, s):
|
|
# cleaned_words = re.sub(r"%s" % string.punctuation, "", s)
|
|
cleaned_words = re.sub(r"[\.,\?!'\":;^`\|%#\&\*<=>\(\)\[\]\{\}\+\/_-]*", "", s)
|
|
self.words = [word for word in cleaned_words.split() if word not in ["the", "a", "an"]]
|
|
|
|
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 squish(text):
|
|
"Remove series of spaces from the text."
|
|
return re.sub('[ \n\t]+', ' ', text).strip()
|
|
|
|
def routput(text):
|
|
"""
|
|
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.'
|
|
"""
|
|
acc = []
|
|
for s in text.split("["):
|
|
choices, *rest = s.split("]")
|
|
choice = random.choice(choices.split('|'))
|
|
acc = acc + [choice] + rest
|
|
return squish(''.join(acc))
|