d3a6ffa6e008f8ab72c8764a5b33d03bb00b81a0
1 from segment
import segment
2 from utilities
import cat
, sanitise
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_"""
11 if len(lines
[-1]) + 1 + len(word
) <= width
:
12 lines
[-1] += (' ' + word
)
18 def depunctuate_character(c
):
19 """Record the punctuation of a character"""
20 if c
in string
.ascii_uppercase
:
22 elif c
in string
.ascii_lowercase
:
28 def depunctuate(text
):
29 """Record the punctuation of a string, so it can be applied to a converted
30 version of the string.
33 punct = depunctuate(ciphertext)
34 plaintext = decipher(sanitise(ciphertext))
35 readable_plaintext = repunctuate(plaintext, punct)
37 return [depunctuate_character(c
) for c
in text
]
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()
51 def repunctuate(text
, punctuation
):
52 """Apply the recored punctuation to a sanitised string.
55 punct = depunctuate(ciphertext)
56 plaintext = decipher(sanitise(ciphertext))
57 readable_plaintext = repunctuate(plaintext, punct)
59 letters
= iter(sanitise(text
))
60 return cat(repunctuate_character(letters
, p
) for p
in punctuation
)