-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverterBase.py
312 lines (253 loc) · 10.3 KB
/
converterBase.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# -*- coding: utf-8 -*-
# !/usr/bin/env python3
# Base class for Docx converters
from __future__ import absolute_import, division, print_function
import os
import re
import sys
thisDefaultOutputFont = 'NotoSansRegular'
class ConverterBase:
def __init__(self, old_font_list=None, new_font=None,
default_output_font=thisDefaultOutputFont):
self.forceFont = True # May be used to set all font fields to the Unicode font
self.encodingScripts = [] # If given, tells the Script of incoming characters
self.oldFonts = []
self.font_resize_factor = 1.0
self.not_converted = {}
self.old_font_name = None
self.font_index = -1
# This may be set up by the individual converter
self.split_by_script = {}
# Only convert fonts from the provided list. This can be overridden if needed.
self.check_all_fonts = False
if new_font:
self.unicodeFont = new_font
else:
self.unicodeFont = default_output_font
# The fonts detected for conversion
for item in old_font_list:
if isinstance(item, list):
self.oldFonts.append(item[0])
self.encodingScripts.append(item[1])
else:
self.oldFonts.append(item)
# If set, sets output font to be a complex script
# This is needed for a number of Unicode scripts
self.set_complex_font = False
# Dictionary of script or other identifiers for conversions
# of individual characters.
self.description = ''
# Range of characters for simple alphabetic
self.first = chr(0x20)
self.last = chr(0x7f)
self.first_lower = chr(0x61)
self.last_lower = chr(0x81)
self.first_upper = chr(0x41)
self.last_upper = chr(0x61)
self.lowerOffset = ord(self.first_lower) - ord(self.first_upper)
self.encoding = None
self.debug = False # False
self.lower_mode = True
self.sentence_mode = True
# Recording information on the document details
# Word frequency of the converted words
self.collectConvertedWordFrequency = False
self.convertedWordFrequency = {}
# For reordering rules
self.pattern_replace_list = []
# For communicating to specific instances
self.current_tag = ''
# If set, can convert output characters to U+ representations
self.output_u_mode = False
def preprocess(self, textIn, current_tag):
# Possibly do some preprocessing on each line, maybe dependent on a tab
# or a regular expression
return textIn
def get_outfile_name(self, infile_name):
name_split = os.path.splitext(infile_name)
out_file_name = name_split[0] + '_Unicode' + name_split[-1]
return out_file_name
def setScriptRange(self, first, last):
self.first = chr(first)
self.last = chr(last)
def setUpperCaseRange(self, first_upper, last_upper):
self.first_upper = chr(first_upper)
self.last_upper = chr(last_upper)
def setLowerCaseRange(self, first, last):
self.first_lower = chr(first)
self.last_lower = chr(last)
self.lowerOffset = ord(self.first_lower) - ord(self.first_upper)
def isRtl(self):
# Override for RTL language
return False
def unicode_to_u_plus(char):
# Gives the string version of a Unicode code point
return "U+" + hex(ord(char))[2:].upper().zfill(4)
def convertText(self, in_text, font_text_info=None, font_index=0, inputFont=None):
return in_text
def toLower(self, in_text):
# Does this work for all scripts?
return in_text.lower()
def reorderText(self, in_text):
# Next, move some code points in context to get proper Unicode ordering.
new_text = in_text
for pair in self.pattern_replace_list:
new_text = re.sub(pair[0], pair[1], new_text)
return new_text
def tokenizeText(self, textIn):
# ASCII and whitespace characters
if self.old_font_name and self.split_by_script and self.old_font_name in self.split_by_script:
token_regex = self.split_by_script[self.old_font_name]
return token_regex.split(textIn)
if self.scriptIndex == 0:
return [i for i in re.split(r'([\w\s;.])', textIn) if i]
else:
return textIn
def setScriptIndex(self, newIndex=0):
# 0 = '', 1 = 'latn'
self.scriptIndex = newIndex
self.scriptToConvert = self.encodingScripts[self.scriptIndex]
# Handles details of converting the text, including case conversion.
def convertString(self, textIn, fontInfo,
conversion_map):
# type: (object, object, object) -> object
convertedList = []
convertResult = ''
tokens = self.tokenizeText(textIn)
if not tokens:
# print('????? WHY NO TOKENS in %s' % textIn)
pass
for c in tokens:
# Special handling if needed
if not c:
continue
out = c
if c in conversion_map:
out = conversion_map[c]
else:
key = '%s-%s' % (self.encoding, c)
if not key in self.not_converted:
self.not_converted[key] = 1
#for i in range(len(c)):
# print('** Code point %s' % hex(ord(c[i])))
#print('Cannot convert %s in %s' % (c, self.encoding))
else:
self.not_converted[key] += 1
# Special case for handling underlined text
convertedList.append(out)
convertResult = self.reorderText(''.join(convertedList))
try:
if self.lower_mode:
convertResult = self.toLower(convertResult)
except BaseException:
pass
return convertResult
def setLowerMode(self, lower_expected):
self.lower_mode = lower_expected
def setSentenceMode(self, sentence_mode):
self.lower_mode = sentence_mode
def toSentenceCase(self, in_text):
if not in_text:
return in_text
first = in_text[0].upper()
return first + in_text[1:]
# Implemented by specific class for language and script.
def processParagraphRuns(self, p):
# Handle the text within each paragraph
if not p.text:
# Nothing to process
return
# Check on the language of the paragraph. May not convert.
if self.detectLang:
detected = self.detectLang.classify(p.text.strip())
# print('%s in %s' % (detected, p.text))
if detected[0] in self.ignoreLangs:
return
for run in p.runs:
try:
text_to_convert = run.text
font_name = None
scriptIndex = 0
font_name = run.font.name
# TODO: Check if unknown font regions should be converted.
try:
scriptIndex = self.FONTS_TO_CONVERT.index(font_name)
except BaseException as error:
print('Unknown font error %s: %s for text %s' % (error, font_name, text_to_convert))
if not self.check_all_fonts:
# Unknown fonts should not be examined
continue
new_text = text_to_convert
try:
new_text = self.convertText(text_to_convert, None, scriptIndex)
except BaseException as error:
print('p.text failure in convertText: %s for text %s' % (error, p.text))
# Replace font in empty regions, too!
if run.text == '' or new_text != run.text:
try:
# Special processing for some types of runs
new_text = self.special_run_handling(new_text)
print('TRY SPECIAL %s' % new_text)
except:
pass
if self.set_complex_font:
run.font.complex_script = True
run.text = new_text
run.font.name = self.unicodeFont
try:
new_font_size = int(run.font.size * self.font_resize_factor)
run.font.size = new_font_size
except TypeError:
# There may be no associated font
pass
except ValueError as error:
print('ValueError %s with p.text: %s' % (error, p.text))
continue
# Check on the font for the full paragraph
try:
self.FONTS_TO_CONVERT.index(p.style.font.name)
p.style.font.name = self.unicodeFont
except ValueError:
pass
if self.handle_sentences:
self.processSentences(p)
try:
if self.collectConvertedWordFrequency:
self.updateWordsFrequencies(p)
except BaseException:
pass # Not a fatal error
return
# Methods for word frequency information
def setFrequencyCollection(self, new_state):
self.collectConvertedWordFrequency = new_state
def clearWordFrequencies(self):
self.convertedWordFrequency.clear()
# Adds a word to the word frequency list
def addWordToFrequencyList(self, word):
if word in self.convertedWordFrequency:
self.convertedWordFrequency[word] += 1
else:
self.convertedWordFrequency[word] = 1
def updateWordsFrequencies(self, paragraph):
# implemented by specific converter class
return
def getWordFrequencies(self):
return self.convertedWordFrequency
def getSortedWordList(self):
if self.convertedWordFrequency:
return sorted(
self.convertedWordFrequency.items(),
key=lambda x: x[1],
reverse=True
)
else:
return None
def getWordListByAlpha(self):
if self.convertedWordFrequency:
return sorted(
self.convertedWordFrequency.items(),
key=lambda x: x[0],
reverse=True
)
else:
return None