Debian Codegraph @

texmf-dist/doc/latex/braille/grade2.py

raw
1 """
2 Converts ASCII text to Grade 2 Braille tags as described in
3 braille.sty, summary.tex
4 Grade 2 has 189 multi-character contractions and rules where they can
5 be used. It uses grade1.py for final markup.
6
7 Usage:
8 >>> grade2.convert("I like computer")
9 "I {like} {com}put{er}"
10 >>> grade2.convert("April 1999")
11 "April {Number}1999"
12 >>> grade2.convert("I said ``hello'', and she said ``goodbye''.")
13 "I {said} {``}hello{''}, {and} {sh}e {said} {``}goodbye{''}."
14
15 Usage:
16 python grade2.py < text > tags
17 """
18 import string, re, grade1
19
20 part_of_word = [
21 'and', 'for', 'of', 'the', 'with',
22
23 'ch', 'sh', 'th', 'wh', 'ou', 'st',
24
25 'ar', 'er', 'ed', 'gh', 'ow',
26
27 'en', 'in',
28 ]
29 beginning_of_word = [
30 'be', 'com', 'con', 'dis',
31 ]
32 middle_of_word = [
33 'bb', 'ble', 'cc', 'dd', 'ea', 'ff', 'gg', 'ing',
34 ]
35 end_of_word = [
36 'ble', 'ing',
37 ]
38 final_letter_contraction = [ # middle or end of word
39 'ound', 'ance', 'sion', 'less', 'ount',
40
41 'ence', 'ong', 'ful', 'tion', 'ness', 'ment', 'ity',
42
43 'ation', 'ally',
44 ]
45 initial_letter_contraction = [ # whole or part of word
46 'these', 'those', 'upon', 'whose', 'word',
47
48 'cannot', 'had', 'many', 'spirit', 'their', 'world',
49
50 'character', 'day', 'ever', 'father', 'here', 'know', 'lord',
51 'mother', 'name', 'one', 'ought', 'part', 'question', 'right',
52 'some', 'there', 'through', 'time', 'under', 'where', 'work',
53 'young',
54 ]
55 whole_word = [
56 'and', 'for', 'of', 'the', 'with',
57
58 'child', 'shall', 'this', 'which', 'out', 'still',
59
60 'but', 'can', 'do', 'every', 'from', 'go', 'have', 'just',
61 'knowledge', 'like', 'more', 'not', 'people', 'quite', 'rather',
62 'so', 'that', 'us', 'very', 'will', 'it', 'you', 'as',
63
64 'be', 'by', 'enough', 'his', 'in', 'into', 'to', 'was', 'were',
65
66 'about', 'above', 'according', 'across', 'after', 'afternoon',
67 'afterward', 'again', 'against', 'almost', 'already', 'also',
68 'although', 'altogether', 'always', 'because', 'before', 'behind',
69 'below', 'beneath', 'beside', 'between', 'beyond', 'blind',
70 'braille', 'children', 'conceive', 'conceiving', 'could',
71 'deceive', 'deceiving', 'declare', 'declaring', 'either', 'first',
72 'friend', 'good', 'great', 'herself', 'him', 'himself',
73 'immediate', 'its', 'itself', 'letter', 'little', 'much', 'must',
74 'myself', 'necessary', 'neither', "o'clock", 'oneself',
75 'ourselves', 'paid', 'perceive', 'perceiving', 'perhaps', 'quick',
76 'receive', 'receiving', 'rejoice', 'rejoicing', 'said', 'should',
77 'such', 'themselves', 'thyself', 'today', 'together', 'tomorrow',
78 'tonight', 'would', 'your', 'yourself', 'yourselves',
79 ]
80
81
82 def begintag(tag, word):
83 return re.sub('^%s(.)' % tag, r'{%s}\1' % tag, word)
84
85 def middletag(tag, word):
86 return re.sub('(.)%s(.)' % tag, r'\1{%s}\2' % tag, word)
87
88 def endtag(tag, word):
89 return re.sub('(.)%s$' % tag, r'\1{%s}' % tag, word)
90
91 def parttag(tag, word):
92 return string.replace(word, tag, '{' + tag + '}')
93
94
95 # Check if word or part of word can be replaced by Grade 2
96 # contractions. Returns string with Braille tags.
97 #
98 def replace(word):
99 if word in whole_word + initial_letter_contraction: # whole word
100 return '{' + word + '}'
101
102 for tag in initial_letter_contraction: # whole or part of word
103 word = parttag(tag, word)
104 for tag in final_letter_contraction: # middle or end of word
105 word = middletag(tag, word)
106 word = endtag(tag, word)
107 for tag in beginning_of_word: word = begintag(tag, word)
108 for tag in end_of_word: word = endtag(tag, word)
109 for tag in middle_of_word: word = middletag(tag, word)
110 for tag in part_of_word: word = parttag(tag, word)
111
112 s, braces = '', 0
113 for i in word: # remove nested braces
114 if i == '{':
115 braces = braces + 1
116 if braces == 1: s = s + i
117 elif i == '}':
118 braces = braces - 1
119 if braces == 0: s = s + i
120 else:
121 s = s + i
122 return s
123
124
125 # Check if word, containing [a-zA-Z] only, is all UPPER case.
126 #
127 def alluppercase(word): # ^[A-Z]+$
128 if len(word) == 0: return 0
129 for i in word:
130 if i not in string.uppercase: return 0
131 return 1
132
133
134 # Check if word, containing [a-zA-Z] only, is all lower case.
135 #
136 def alllowercase(word): # ^[a-z]+$
137 if len(word) == 0: return 0
138 for i in word:
139 if i not in string.lowercase: return 0
140 return 1
141
142
143 # Check if word, containing [a-zA-Z] only, is Capitalized.
144 #
145 def capitalized(word): # ^[A-Z][a-z]*$
146 if len(word) == 0:
147 return 0
148 elif len(word) == 1:
149 return alluppercase(word[0])
150 else:
151 return alluppercase(word[0]) and alllowercase(word[1:])
152
153
154 def convert(line):
155 s = ''
156 for word in re.split('([a-zA-Z]+)', line):
157 if len(word) <= 1 or word[0] not in string.letters:
158 s = s + word # no work if not 2 or more letters
159 elif alluppercase(word):
160 s = s + '{Upper}' + replace(string.lower(word))
161 elif capitalized(word):
162 w = replace(string.lower(word))
163 if w[0] in string.lowercase:
164 s = s + word[0] + w[1:] # preserve Capital letter
165 else:
166 s = s + '{Capital}' + w
167 else:
168 s = s + replace(word)
169
170 # manually handle whole word {o'clock}
171 s = re.sub(r"\bo'clock\b", "{o'clock}", s)
172
173 # whole words {in}, {be}, {enough}, {his}, {was}, {were}
174 # cannot touch punctuations
175 s = nopunctuations('{in}', 'in', s)
176 s = nopunctuations('{be}', 'be', s)
177 s = nopunctuations('{enough}', '{en}{ou}{gh}', s)
178 s = nopunctuations('{his}', 'his', s)
179 s = nopunctuations('{was}', 'was', s)
180 s = nopunctuations('{were}', 'w{er}e', s)
181
182 # no space after whole words {by}, {into}, {to}
183 s = re.sub(r'{(by|into|to)}\s+', r'{\1}', s)
184
185 return grade1.convert(s)
186
187
188 # Change whole word '{tag}' to 'tag' when touching punctuations
189 #
190 def nopunctuations(tag, newtag, s):
191 s = re.sub(r'(\s|\A)%s([^\s\w{}])' % tag, r'\1%s\2' % newtag, s)
192 s = re.sub(r'([^\s\w{}])%s(\s|\Z)' % tag, r'\1%s\2' % newtag, s)
193 s = re.sub(r'([^\s\w{}])%s([^\s\w{}])' % tag, r'\1%s\2' % newtag, s)
194 return s
195
196
197 if __name__ == '__main__':
198 import sys
199 line = sys.stdin.read() # swallow the whole thing
200 print convert(line) # may produce extra \n