-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
blackout.py
executable file
·118 lines (95 loc) · 2.69 KB
/
blackout.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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