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

████ ██ Ishmael (2022) #7

Merged
merged 2 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added blackout.pdf
Binary file not shown.
118 changes: 118 additions & 0 deletions blackout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/usr/bin/env python
"""
Black out all repeated words, preserving punctuation.

For NaNoGenMo 2022.
https://github.com/NaNoGenMo/2022/
"""

import argparse
import random
import re
import sys

all_words = set()


def is_word(thing):
found = re.match(r"\w+", thing, re.UNICODE)
return found


def meow_meow(line, converter_fun):
"""Meowify a line"""
meowed = []
# Break line into words and non-words (e.g. punctuation and space)
things = re.findall(r"\w+|[^\w]", line, re.UNICODE)
for thing in things:
if is_word(thing):
meowed.append(converter_fun(thing))
else:
meowed.append(thing)
return "".join(meowed)


def blackout(word):
if word.lower() in all_words:
return len(word) * "█"
all_words.add(word.lower())
return word


def meow(word):
"""Meowify a word"""
meowed = ""
length = len(word)

if length == 1:
return capify("m", word)
elif length == 2:
return capify("me", word)
elif length == 3:
return capify("mew", word)
elif length == 4:
return capify("meow", word)

# Words longer than four will have:
# * first letter M
# * last letter W
# * middle with a random number of Es, then some Os

# Number of EOs:
eeohs = length - len("m") - len("w")
# Number of Es:
ees = random.randrange(1, eeohs)
# Number of Os:
ohs = eeohs - ees

meowed = "m" + ("e" * ees) + ("o" * ohs) + "w"
return capify(meowed, word)


def capify(word, reference):
"""Make sure word has the same capitalisation as reference"""
new_word = ""

# First check whole word before char-by-char
if reference.islower():
return word.lower()
elif reference.isupper():
return word.upper()

# Char-by-char checks
for i, c in enumerate(reference):
if c.isupper():
new_word += word[i].upper()
else:
new_word += word[i]
return new_word


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Replace all words with meows, preserving punctuation."
)
parser.add_argument(
"infile",
nargs="?",
type=argparse.FileType("r"),
default=sys.stdin,
help="Input text",
)
parser.add_argument(
"-t",
"--translation",
action="store_true",
help="Output a line-by-line translation",
)
args = parser.parse_args()

# for line in fileinput.input(openhook=fileinput.hook_encoded("utf-8")):
for line in args.infile:
line = line.rstrip() # No BOM
if args.translation:
print()
print(line)
print(meow_meow(line, blackout))

# End of file
Loading