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