-
Notifications
You must be signed in to change notification settings - Fork 0
/
textFct.py
executable file
·390 lines (368 loc) · 15.2 KB
/
textFct.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZaàâbcdeéêèëfghiîïjkmlmnoôpqrstuûvwxyz0123456789-\xe7\xc7'
uppercaseLetters = ('aA', 'àA', 'bB', 'cC', '\xe7\xc7', 'dD', 'eE', 'éE', 'èE', 'êE', 'ëE', 'fF', 'gG', 'hH', 'iI', 'îI', 'ïI', 'jJ', 'kK', 'lL', 'mM', 'nN', 'oO', '\xf4\xe4', 'pP', 'qQ', 'rR', 'sS', 'tT', 'uU', 'vV', 'wW', 'xX', 'yY', 'zZ')
# liste des points, des chaines de caracteres suivies par une majuscule
wordsBeginMaj = ('lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche', 'janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre', 'deborah', 'powers', 'maman', 'mamie', 'papa', 'papi', 'victo', 'tony', 'robert', 'simplon', 'loïc', 'jared', 'leto', 'ville valo', 'valo', 'shelby', 'magritte', 'france', 'paris', 'rueil', 'malmaison', 'avon', 'fontainebleau', 'ivry', 'chateaudun', 'châteaudun', 'c:/', 'c:\\' )
wordsBeginMin = ('Deborah.powers', 'Deborah.noisetier', 'Http', 'File:///', '\nPg_')
codeKeywords =(
'set schema', 'declare', 'begin', 'do $$', 'update', 'select', 'from', 'inner join', 'outer join', 'left outer join', 'where',
'having', 'group by', 'order by', 'insert into', 'if', 'elseif', 'end', 'loop', 'perform', 'drop ',
'cd', 'psql', 'git', 'return', 'mvn', 'python', 'else',
'def', 'class', 'console.log', 'var', 'function', 'private', 'protected', 'public',
'log.debug', 'log.info'
)
weirdChars =(
('« ', '"'), (' »', '"'), ('«', '"'), ('»', '"'), ('–', '-'), ('‘', "'"), ('’', "'"), ('“', '"'), ('”', '"'), ('"', '"'), ('…', '...'), ('…', '...'), ('…', '...'),
('\n ', '\n'), ('\r', ''), (' \n', '\n'), ("\\'", "'"), ('\\n', '\n'), ('\\r', ''), ('\\t', '\t'),
('\\u00c2', 'Â'), ('\\u00ca', 'Ê'), ('\\u00cb', 'Ë'), ('\\u00ce', 'Î'), ('\\u00cf', 'Ï'), ('\\u00d4', 'Ô'), ('\\u00d6', 'Ö'), ('\\u00db', 'Û'), ('\\u00e0', 'à'), ('\\u00e2', 'â'), ('\\u00e7', 'ç'), ('\\u00e8', 'è'), ('\\u00e9', 'é'), ('\\u00ea', 'ê'), ('\\u00eb', 'ë'), ('\\u00ee', 'î'), ('\\u00ef', 'ï'), ('\\u00f4', 'ô'), ('\\u00f6', 'ö'), ('\\u00fb', 'û'),
('\\', '/'),
('\x85', '.'), ('\x92', "'"), ('\x96', '"'), ('\x97', "'"), ('\x9c', ' '), ('\xa0', ' '),
('à', 'à'), ('â', 'â'), ('ç', 'ç'), ('é', 'é'), ('è', 'è'), ('ê', 'ê'), ('î', 'î'), ('ï', 'ï'), ('ô', 'ô'), ('ù', 'ù'), ('û', 'û'), (''', "'"),
('—', ' '), (' ', ''), ('œ', 'oe'), ('"', ''), ('<', '<'), ('>', '>'), ('‘', '"'), ('“', '"'), ('”', '"'), ('’', "'"), ('«', '"'), ('»', '"'), ('“', '"'), ('”', '"'), ('–', '-'),
('&', '&'), (''', "'"), (''', "'"), (' ', ' '), (''', "'"), ('’', "'"), ('\n" ', '\n"')
)
urlWords =( ('c:', 'C:\\'), (': /', ':/'), ('localhost: ', 'localhost:'), ('www. ', 'www.'), ('. jpg', '.jpg'), ('. png', '.png'), ('. css', '.css'), ('. js', '.js'), (': 80', ':80'), ('. com', '.com'), ('. org', '.org'), ('. net', '.net'), ('. fr', '.fr'), ('. ico', '.ico') )
# ________________________ ma mise en forme perso ________________________
def upperCaseIntern (text):
text ='\n'+ text
points =( '\n', '. ', '! ', '? ', ': ', ':\t', '\n_ ', '\n* ', '\n- ', '\n--> ', '\n\t', '###### ', '______ ', '______ ', '------ ', '****** ', '====== ')
for i, j in uppercaseLetters:
for p in points: text = text.replace (p+i, p+j)
punctuation = '({[?!;.,:]})"\' \n\t'
for word in wordsBeginMaj:
for p in punctuation:
text = text.replace (" "+ word +p, " "+ word.capitalize() +p)
text = text.replace ("\t"+ word +p, "\t"+ word.capitalize() +p)
"""
for p in " "+ punctuation[-11:]:
for q in " "+ punctuation[0:5]:
for word in wordsBeginMaj:
if word == 'powers': print (p,q)
text = text.replace (q+ word +p, q+ word.capitalize() +p)
"""
for word in wordsBeginMin: text = text.replace (word, word.lower())
# le code
for artefact in codeKeywords:
text = text.replace ('\n'+ artefact.capitalize() +' ', '\n'+ artefact +' ')
text = text.replace ('\t'+ artefact.capitalize() +' ', '\t'+ artefact +' ')
text = text.replace ('\n'+ artefact.capitalize() +'\n', '\n'+ artefact +'\n')
text = text.replace ('\t'+ artefact.capitalize() +'\n', '\t'+ artefact +'\n')
text = text.strip()
return text
def upperCase (text, case=""):
""" rest: je supprime l'ancienne casse
upper: je rajoute les majuscules
log (type (text))
"""
if 'reset' in case: text = text.lower()
if 'upper' in case:
text = '\n'+ text +'\n'
if '\n/code\n' in text:
paragraphList = text.split ('\ncode\n')
paragraphRange = range (1, len (paragraphList))
for i in paragraphRange:
d= paragraphList[i].find ('\n/code\n') +7
paragraphList[i] = paragraphList[i][:d] + upperCaseIntern (paragraphList[i][d:])
text = '\ncode\n'.join (paragraphList)
else: text = upperCaseIntern (text)
text = text.strip()
return text
def findStickedWords (text):
punctuation = '?!;.:,[](){}_=*+-/\'\t\n"'
for p in punctuation: text = text.replace (p," ")
text = cleanBasic (text)
textList = text.split (" ")
textSet = set (textList)
for word in textSet:
if len (word) >15: print ('fusion probable', word)
def findEndUrl (text, pos=0):
charEndUrl = '\n\t \'",;!()[]{}'
lenText = len (text) +1
posEnd = lenText
posTmp = lenText
for char in charEndUrl:
posTmp = text.find (char, pos)
if posTmp >0 and posTmp < posEnd: posEnd = posTmp
return posEnd
def cleanBasic (text):
for i, j in weirdChars: text = text.replace (i, j)
text = text.strip()
while ' ' in text: text = text.replace (' ', ' ')
text = text.replace ('\n ', '\n')
text = text.replace (' \n', '\n')
text = text.replace ('\t ', '\t')
text = text.replace (' \t', '\t')
while '\t\n' in text: text = text.replace ('\t\n', '\n')
while '\n\n' in text: text = text.replace ('\n\n', '\n')
return text
def cleanHtml (text):
text = cleanBasic (text)
text = text.replace ('\n', ' ')
text = text.replace ('\t', ' ')
for i, j in weirdChars: text = text.replace (i, j)
text = text.strip()
while ' ' in text: text = text.replace (' ', ' ')
text = text.replace ('> ', '>')
text = text.replace (' <', '<')
text = text.replace (' >', '>')
text = text.replace (' />', '/>')
"""
text = text.replace ('><', '>\n<')
innerTagclosing =( 'a', 'p', 'span', 'i', 'strong', 'option', 'button', 'li', 'td', 'th', 'h1', 'h2', 'h3', 'h4')
for tag in innerTagclosing:
text = text.replace ('\n</'+ tag +'>', '</'+ tag +'>')
text = text.replace ('<'+ tag +'>\n', '<'+ tag +'>')
text = text.replace ('</tr>\n<tr', '</tr><tr')
"""
return text
def cleanCss (text):
text = cleanBasic (text)
text = text.replace ('\n', ' ')
text = text.replace ('\t', ' ')
for i, j in weirdChars: text = text.replace (i, j)
text = text.strip()
while ' ' in text: text = text.replace (' ', ' ')
tagCleanSpaces =( '{', '}', '/*', '*/', ':', ';' )
for tag in tagCleanSpaces:
text = text.replace (' '+ tag +' ', tag)
text = text.replace (' '+ tag, tag)
text = text.replace (tag +' ', tag)
text = text.replace ('}', ';}')
text = text.replace (';;', ';')
text = text.replace ('};', '}')
# nettoyer les commentaires
commList = text.split ('/*')
commRange = range (commList, 1)
for c in commRange:
f= commList[c].find ('*/')
if '{' in commList[c][:f]: commList[c] = commList[c][f:]
text = '/*'.join (commList)
text = text.replace ('/**/', "")
# optimiser les blocs
commList = text.split ('{')
commRange = range (commList, 1)
for c in commRange:
f= commList[c].find ('}')
if f>0:
innerComm = commList[c][:f].replace (' ', ';')
if innerComm.count (';') >1:
innerComm = innerComm.replace (';', ';\n\t')
innerComm = '\n\t' + innerComm +'\n'
elif innerComm.count (';') ==1: innerComm = ' '+ innerComm +' '
commList[c] = innerComm + commList[c][f:]
else: commList[c] = '\n\t' + commList[c]
text = ' {'.join (commList)
text = text.replace ('}', '}\n')
text = text.replace ('/*', '\n/* ')
text = text.replace ('*/', ' */\n')
while '\n\n' in text: text = text.replace ('\n\n', '\n')
return text
def cleanText (text):
text = cleanBasic (text)
# la ponctuation
punctuation = '?!;.:,'
for p in punctuation: text = text.replace (' '+p, p)
while '....' in text: text = text.replace ('....', '...')
for letter in letters:
text = text.replace (letter +'!', letter +' !')
text = text.replace (letter +'?', letter +' ?')
# text = text.replace (letter +';', letter +' ;')
text = text.replace ('...' + letter, '... '+ letter)
while ' ' in text: text = text.replace (' ', ' ')
# restaurer les url
if 'http' in text and '?' in text:
"""
textList = text.split ('http')
textRange = range (1, len (textList))
for t in textRange:
print ('t',t)
f= textList[t].find ('\n')
if '\t' in textList[t][:f]: f= textList[t].find ('\t')
if " ?" in textList[t][:f]: print (textList[t][:f])
"""
textList = text.split (' ?')
textRange = range (len (textList) -1)
for t in textRange:
if 'http' in textList[t]:
d= textList[t].rfind ('http')
if " " in textList[t][d:] or '\n' " " in textList[t][d:]: textList[t] = textList[t] +" "
else: textList[t] = textList[t] +" "
text = '?'.join (textList)
# restaurer les heures
textList = text.split (':')
textRange = range (len (textList) -1)
for t in textRange:
if len (textList[t]) >1 and len (textList[t+1]) >1 and textList[t][-2] in '012345' and textList[t][-1] in '0123456789' and textList[t+1][0] in '012345' and textList[t+1][1] in '0123456789': continue
else: textList[t+1] =" "+ textList[t+1]
text = ':'.join (textList)
while ' ' in text: text = text.replace (' ', ' ')
charEndUrl = '\n\t \'",;!()[]{}'
for wordStart, wordEnd in urlWords[:9]: text = text.replace (wordStart, wordEnd)
for wordStart, wordEnd in urlWords[9:]:
for e in charEndUrl: text = text.replace (wordStart +e, wordEnd +e)
text = text.replace (' \n', '\n')
text = text.replace (' \t', '\t')
text = text.replace ('\t ', '\t')
text = text.replace ('\n ', '\n')
return text
def cleanTextVa (text):
text = cleanBasic (text)
# la ponctuation
punctuation = '?!;.:,'
for p in punctuation: text = text.replace (' '+p, p)
while '....' in text: text = text.replace ('....', '...')
for letter in letters:
text = text.replace (letter +'?', letter +' ?')
text = text.replace (letter +'!', letter +' !')
text = text.replace (letter +';', letter +' ;')
text = text.replace ('...' + letter, '... '+ letter)
text = text.replace (':', ': ')
while ' ' in text: text = text.replace (' ', ' ')
# restaurer les heures
liste = text.split (': ')
rliste = range (1, len (liste))
for l in rliste:
if len (liste[l]) >1 and liste[l][0] in '012345' and liste[l][1] in '0123456789':
if len (liste[l]) >2 and liste[l][2] != '.':
d= findEndUrl (liste[l])
if d!=2: liste[l] =' '+ liste[l]
else: liste[l] =' '+ liste[l]
text = ':'.join (liste)
text = text.replace (' \n', '\n')
text = text.replace (' \t', '\t')
# restaurer les url
charEndUrl = '\n\t \'",;!()[]{}'
for wordStart, wordEnd in urlWords[:9]: text = text.replace (wordStart, wordEnd)
for wordStart, wordEnd in urlWords[9:]:
for e in charEndUrl: text = text.replace (wordStart +e, wordEnd +e)
liste = text.split (' ?')
rliste = range (1, len (liste))
for l in rliste:
d= findEndUrl (liste[l])
if '=' not in liste[l][:d]: liste[l-1] = liste[l-1] +' '
text = '?'.join (liste)
return text
def shape (text, case=""):
""" mettre le text en forme pour simplifier sa transformation
j'utilise une mise en forme personnelle
addCase
rien: je ne rajoute pas les majuscules
rest: je supprime l'ancienne casse
upper: je rajoute les majuscules
"""
text = cleanText (text)
text = '\n'+ text +'\n'
for char in '*#=~-_':
while 4* char in text: text = text.replace (4* char, 3* char)
text = text.replace (' '+ 3* char +'\n', ' '+ 12* char +'\n\n')
text = text.replace ('\n'+ 3* char +' ', '\n\n'+ 12* char +' ')
text = text.replace ('\n'+ 3* char +'\n', '\n\n'+ 48* char +'\n\n')
while '\n\n\n' in text: text = text.replace ('\n\n\n', '\n\n')
if case: text = upperCase (text, case)
return text
def cleanPdf (text):
# mettre en forme un texte copié - collé d'un pdf
text = cleanText (text)
points = ',;.:!?)]}"\''
for p in points: text = text.replace (p+'\n', p+'%%')
text = text.replace ('%%', '\n')
text = text.replace ('\n', ' ')
text = text.replace (' • ', '\n\t')
text = text.replace ('\n• ', '\n\t')
text = shape (text, 'reset upper')
findStickedWords (text)
return text
def toMarkdown (text):
text = shape (text, 'upper')
text = text.replace ('============', '=')
text = text.replace ('************', '==')
text = text.replace ('------------', "'''")
text = text.replace ('\n\t', '\n- ')
while '\n\n' in text: text = text.replace ('\n\n', '\n')
text = text.replace ('\n', '\n\n')
return text
# ________________________ fonctions de bases ________________________
def fromModel (text, model):
# remplacer scanf
# préparer le modèle
model = model.lower()
text = text.lower()
if '</' in model:
text = cleanHtml (text)
model = cleanHtml (model)
else:
text = cleanText (text)
model = cleanText (model)
typeList = ['d', 'f', 's']
model = model.replace ('%%', '$')
modelTmp = model
for t in typeList: modelTmp = modelTmp.replace ('%'+t, '%')
# récupérer les données du message sous forme de string
modelList = modelTmp.split ('%')
if not modelList[-1]: trash = modelList.pop (-1)
if not modelList[0]: trash = modelList.pop (0)
for line in modelList:
if text.count (line) ==1 or '%%': text = text.replace (line, '%%', 1)
elif '%%' in text:
d= text.rfind ('%%')
textTmp = text[d:].replace (line, '%%', 1)
text = text[:d] + textTmp
else: text = text.replace (line, '%%', 1)
results = text.split ('%%')
if not results[-1]: trash = results.pop (-1)
if not results[0]: trash = results.pop (0)
d=0
rangeRes = range (len (results))
for r in rangeRes:
d= model.find ('%', d) +1
if d==0: continue
if model[d] =='d': results[r] = int (results[r])
elif model[d] =='f': results[r] = float (results[r])
if (len (results) >2+ modelTmp.count ('%')): print ('erreur: %=', modelTmp.count ('%'), 'item =', len (results))
return results
def find (text, word, pos=0):
posFind =-1
if pos <0: pos = len (text) -pos
if word in text: posFind = str.find (text, word, pos)
elif '"' in word:
word = word.replace ('"', "'")
posFind = str.find (text, word, pos)
elif "'" in word:
word = word.replace ("'", '"')
posFind = str.find (text, word, pos)
return posFind
def rfind (text, word):
posFind =-1
if word in text: posFind = str.rfind (text, word)
elif '"' in word:
word = word.replace ('"', "'")
posFind = str.rfind (text, word)
elif "'" in word:
word = word.replace ("'", '"')
posFind = str.rfind (text, word)
return posFind
def sliceWord (text, wordStart, wordEnd):
res =""
if wordStart in text and wordEnd in text:
d= text.find (wordStart) + len (wordStart)
f= text.find (wordEnd, d)
if f>0 and f>d: res = text[d:f]
return res
def test():
text = """nfznvvz
zef,z "ofk" v,sknvdzkl.fnz fk" g
à 50:30:60
adresse: http://www.fr
"""
model = """fznvvz
%ssknvdzkl.fnz %s
"""
text = cleanText (text)
print ('clean\t', text)
text = upperCase (text, 'upper')
print ('upperCase\t', text)
print ('find\t', find (text, "fk'", 20))
print ('rfind\t', rfind (text, "fk'"))
textList = fromModel (text, model)
print (textList)