Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

████ #41

Open
leonardr opened this issue Nov 28, 2022 · 0 comments
Open

████ #41

leonardr opened this issue Nov 28, 2022 · 0 comments

Comments

@leonardr
Copy link

████ 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

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>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants