| 1 |
""" |
| 2 |
Converts ASCII text to Grade 1 Braille as described in |
| 3 |
braille.sty, summary.tex |
| 4 |
Essentially, it marks up |
| 5 |
{Letter}, {Number}, |
| 6 |
{``}...{''} for double quotation which must be typed as ``...'', |
| 7 |
{.`}...{'.} for single quotation which must be typed as .`...'., |
| 8 |
{percent} |
| 9 |
so that the text is acceptable as input to \braille{}. The output is |
| 10 |
striped and returned all in one line. |
| 11 |
|
| 12 |
Usage: |
| 13 |
>>> grade1.convert("I like computer") |
| 14 |
"I like computer" |
| 15 |
>>> grade1.convert("April 1999") |
| 16 |
"April {Number}1999" |
| 17 |
>>> grade1.convert("I said ``hello'', and she said ``goodbye''.") |
| 18 |
"I said {``}hello{''}, and she said {``}goodbye{''}." |
| 19 |
|
| 20 |
Usage: |
| 21 |
python grade1.py < text > tags |
| 22 |
""" |
| 23 |
import string |
| 24 |
|
| 25 |
Number = '0123456789' |
| 26 |
|
| 27 |
def convert(line): |
| 28 |
line = string.replace(line, "``", "{``}") # ``...'' |
| 29 |
line = string.replace(line, "''", "{''}") |
| 30 |
line = string.replace(line, ".`", "{.`}") # .`...'. |
| 31 |
line = string.replace(line, "'.", "{'.}") |
| 32 |
line = string.replace(line, '%', '{percent}') # % |
| 33 |
s, oldi, oldii, skipchars = '', ' ', ' ', 0 |
| 34 |
for i in line: |
| 35 |
if i == '{': |
| 36 |
skipchars = 1 |
| 37 |
elif i == '}': |
| 38 |
skipchars = 0 |
| 39 |
elif skipchars: # skip anything inside {...} |
| 40 |
pass |
| 41 |
elif oldi in Number and i in 'abcdefghij': |
| 42 |
s = s + '{Letter}' |
| 43 |
elif i in Number: |
| 44 |
if (oldii in Number and oldi in '.-') or (oldi in Number): |
| 45 |
pass |
| 46 |
else: |
| 47 |
s = s + '{Number}' |
| 48 |
s, oldi, oldii = s + i, i, oldi |
| 49 |
return string.join(string.split(s)) # return all in one line |
| 50 |
|
| 51 |
|
| 52 |
if __name__ == '__main__': |
| 53 |
import sys |
| 54 |
line = sys.stdin.read() # swallow the whole thing |
| 55 |
print convert(line) # may produce extra \n |