Created
March 12, 2016 21:58
-
-
Save stuartlangridge/49753c9971f5fa7d3072 to your computer and use it in GitHub Desktop.
Deredact stuff in the "redact" font
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
# call as | |
# python deredact.py "$(python redact.py hello world)" | |
instr = " ".join(sys.argv[1:]).replace(".", "").replace(",", "") | |
fp = open("/usr/share/dict/words") | |
words = [x.lower() for x in fp.read().split("\n")] | |
fp.close() | |
COMMON_TWO = ["of", "to", "in", "it", "is", "be", "as", "at", "so", "we", "he", "by", "or", "on", | |
"do", "if", "me", "my", "up", "an", "go", "no", "us", "am"] | |
COMMON_THREE = ["the", "and", "for", "are", "but", "not", "you", "all", "any", "can", "had", "her", | |
"was", "one", "our", "out", "day", "get", "has", "him", "his", "how", "man", "new", | |
"now", "old", "see", "two", "way", "who", "boy", "did", "its", "let", "put", "say", "she", "too", "use"] | |
expand = {"a": "abcde", "f": "fghij", "k": "klmno", "p": "pqrst", "u": "uvwxyz"} | |
phrase = [] | |
counter = 0 | |
for word in instr.split(): | |
print "%s/%s" % (counter, len(instr.split())) | |
counter = counter + 1 | |
possibilities = [] | |
for c in word: | |
possibilities.append(expand.get(c, c)) | |
exppos = list(possibilities[0]) | |
for item in possibilities[1:]: | |
newexppos = [] | |
for c in item: | |
for prev in exppos: | |
nstr = prev + c | |
if [x for x in words if x.startswith(nstr)]: | |
newexppos.append(nstr) | |
exppos = newexppos | |
exppos = [x for x in exppos if x in words] | |
apopexppos = [] | |
for x in exppos: | |
if len(x) == 2 and x in COMMON_TWO: | |
apopexppos.append(x.upper()) | |
elif len(x) == 3 and x in COMMON_THREE: | |
apopexppos.append(x.upper()) | |
else: | |
apopexppos.append(x) | |
exppos = apopexppos | |
if not exppos: | |
exppos = ["?" + word] | |
phrase.append("|".join(sorted(exppos))) | |
print " ".join(phrase) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
# call as redact.py hello world | |
s = " ".join(sys.argv[1:]).lower() | |
expand = {"a": "abcde", "f": "fghij", "k": "klmno", "p": "pqrst", "u": "uvwxyz"} | |
redact = {} | |
for k, v in expand.items(): | |
for c in v: | |
redact[c] = k | |
for k, v in redact.items(): | |
s = s.replace(k, v) | |
print s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment