You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import random
import re
import sys
class Book(object):
def __init__(self, filename):
self.word_bag = set()
self.words = []
text = open(filename).read()
self.parse(text)
self.patterns = []
def parse(self, text):
for paragraph in re.compile("\n\n").split(text):
for word in re.compile("[—\s]+").split(paragraph):
word = word.strip()
normalized = self.normalize_word(word)
if not normalized:
continue
self.word_bag.add(normalized)
self.words.append(word)
self.words.append("\n\n")
def normalize_word(self,w):
w = w.lower().replace("’", "'")
return re.compile("[^A-Za-z]").sub("", w)
def blackout_word(self,w):
return re.compile("[A-Za-z0-9]").sub("█", w)
def blackout(self):
data = ""
para = []
for word in self.words:
n = self.normalize_word(word)
if any(n in x.word_bag for x in self.patterns):
para.append(self.blackout_word(word))
else:
if word == '\n\n':
data += "<p>{}</p>\n\n".format(" ".join(para))
para = []
else:
para.append(word)
if para:
data += "<p>{}</p>".format(" ".join(para))
return data.replace("█ █", "██")
source = Book(sys.argv[1])
source.patterns = [Book(x) for x in sys.argv[2:]]
target = source.blackout()
print("""<html>
<body style="width: 760px; text-align: left; margin: 0 auto;">
print(target)
print("</body></html>")
The text was updated successfully, but these errors were encountered:
████ uses two science fiction novels as an interference pattern to make a blackout text out of a third. More details are in my blog post.
Preview
███████ not-quite-spacesuits █████████████████████████████████████. ███████, ██████████████████ baked-on ████████████████. ████████████████████, ██████████████████████ punched-metal decorations █████████. ████████████ shucked ███, █████████, ████████████████████ skinny-dipping, ████████████████████████████████████████████. ███████████████████████████ sloppiness ███████████████.
Source
The text was updated successfully, but these errors were encountered: