Some tidying
[cipher-tools.git] / text_prettify.py
1 from segment import segment
2 from utilities import cat, sanitise
3 import string
4
5
6 def tpack(text, width=100):
7 """Pack a list of words into lines, so long as each line (including
8 intervening spaces) is no longer than _width_"""
9 lines = [text[0]]
10 for word in text[1:]:
11 if len(lines[-1]) + 1 + len(word) <= width:
12 lines[-1] += (' ' + word)
13 else:
14 lines += [word]
15 return lines
16
17
18 def depunctuate_character(c):
19 """Record the punctuation of a character"""
20 if c in string.ascii_uppercase:
21 return 'UPPER'
22 elif c in string.ascii_lowercase:
23 return 'LOWER'
24 else:
25 return c
26
27
28 def depunctuate(text):
29 """Record the punctuation of a string, so it can be applied to a converted
30 version of the string.
31
32 For example,
33 punct = depunctuate(ciphertext)
34 plaintext = decipher(sanitise(ciphertext))
35 readable_plaintext = repunctuate(plaintext, punct)
36 """
37 return [depunctuate_character(c) for c in text]
38
39
40 def repunctuate_character(letters, punctuation):
41 """Apply the recorded punctuation to a character. The letters must be
42 an iterator of base characters."""
43 if punctuation == 'UPPER':
44 return next(letters).upper()
45 elif punctuation == 'LOWER':
46 return next(letters).lower()
47 else:
48 return punctuation
49
50
51 def repunctuate(text, punctuation):
52 """Apply the recored punctuation to a sanitised string.
53
54 For example,
55 punct = depunctuate(ciphertext)
56 plaintext = decipher(sanitise(ciphertext))
57 readable_plaintext = repunctuate(plaintext, punct)
58 """
59 letters = iter(sanitise(text))
60 return cat(repunctuate_character(letters, p) for p in punctuation)