1 from segment
import segment
3 from language_models
import sanitise
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_"""
12 if len(lines
[-1]) + 1 + len(word
) <= width
:
13 lines
[-1] += (' ' + word
)
19 def depunctuate_character(c
):
20 """Record the punctuation of a character"""
21 if c
in string
.ascii_uppercase
:
23 elif c
in string
.ascii_lowercase
:
29 def depunctuate(text
):
30 """Record the punctuation of a string, so it can be applied to a converted
31 version of the string.
34 punct = depunctuate(ciphertext)
35 plaintext = decipher(sanitise(ciphertext))
36 readable_plaintext = repunctuate(plaintext, punct)
38 return [depunctuate_character(c
) for c
in text
]
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()
52 def repunctuate(text
, punctuation
):
53 """Apply the recored punctuation to a sanitised string.
56 punct = depunctuate(ciphertext)
57 plaintext = decipher(sanitise(ciphertext))
58 readable_plaintext = repunctuate(plaintext, punct)
60 letters
= iter(sanitise(text
))
61 return cat(repunctuate_character(letters
, p
) for p
in punctuation
)