X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=text_prettify.py;fp=text_prettify.py;h=ea7da4e1d59ec640e95b652e64c97c7f1c991070;hb=837af0a78bfaf51266a1e21fbfd2baa80857d5bb;hp=0000000000000000000000000000000000000000;hpb=f3e1998f73574cc3178ab45c93fd8aa27cf7d6ae;p=cipher-tools.git diff --git a/text_prettify.py b/text_prettify.py new file mode 100644 index 0000000..ea7da4e --- /dev/null +++ b/text_prettify.py @@ -0,0 +1,61 @@ +from segment import segment +from cipher import cat +from language_models import sanitise +import string + + +def tpack(text, width=100): + """Pack a list of words into lines, so long as each line (including + intervening spaces) is no longer than _width_""" + lines = [text[0]] + for word in text[1:]: + if len(lines[-1]) + 1 + len(word) <= width: + lines[-1] += (' ' + word) + else: + lines += [word] + return lines + + +def depunctuate_character(c): + """Record the punctuation of a character""" + if c in string.ascii_uppercase: + return 'UPPER' + elif c in string.ascii_lowercase: + return 'LOWER' + else: + return c + + +def depunctuate(text): + """Record the punctuation of a string, so it can be applied to a converted + version of the string. + + For example, + punct = depunctuate(ciphertext) + plaintext = decipher(sanitise(ciphertext)) + readable_plaintext = repunctuate(plaintext, punct) + """ + return [depunctuate_character(c) for c in text] + + +def repunctuate_character(letters, punctuation): + """Apply the recorded punctuation to a character. The letters must be + an iterator of base characters.""" + if punctuation == 'UPPER': + return next(letters).upper() + elif punctuation == 'LOWER': + return next(letters).lower() + else: + return punctuation + + +def repunctuate(text, punctuation): + """Apply the recored punctuation to a sanitised string. + + For example, + punct = depunctuate(ciphertext) + plaintext = decipher(sanitise(ciphertext)) + readable_plaintext = repunctuate(plaintext, punct) + """ + letters = iter(sanitise(text)) + return cat(repunctuate_character(letters, p) for p in punctuation)