1 # -*- coding: utf-8 -*-
2 #Author: Pierre Ratinaud
3 #Copyright (c) 2008-2020 Pierre Ratinaud
4 #modification pour python 3 : Laurent Mérat, 6x7 - mai 2020
7 #------------------------------------
8 # import des modules python
9 #------------------------------------
15 mois = {'janvier' : '01',
41 def parsetxtpaste(txt):
43 parser de texte pour factiva
44 à partir d'un copier/coller de la fenêtre de visualisation
45 merci à Lucie Loubère pour l'astuce :)
48 no = ['NS','RE','IPD','CO','IN'] # les balises qui signalent une fin
49 txt = txt.splitlines()
53 if line.startswith('Article') :
56 if lp[2] == 'Article' or lp[2] == 'Next' or lp[2] == 'Previous':
57 ucis.append([['****'],''])
59 if line.startswith('SN ') : #source
60 jsource = re.sub('[\'" !\.?;,:\+\-°&]', '', line[4:])
61 source = '_'.join(['*source', jsource]).lower()
62 #source = '*source_' + line[4:].replace(' ','').replace('\'','').replace('´','').replace('’','').replace('-','').lower()
63 ucis[-1][0].append(source)
64 elif line.startswith('PD ') : #date
65 datemois = line[4:].split(' ')[1].lower()
66 datemois = mois.get(datemois, datemois)
67 dateannee = line[4:].split(' ')[2]
68 datejour = '%02d' % int(line[4:].split(' ')[0])
69 am = '_'.join(['*am', dateannee, datemois])
70 amj = '_'.join(['*amj', dateannee, datemois, datejour])
71 ucis[-1][0].append(am)
72 ucis[-1][0].append(amj)
73 annee = '_'.join(['*annee', dateannee])
74 ucis[-1][0].append(annee)
75 elif line.strip() in no : #fin
77 elif line.startswith('RF ') : #fin
79 elif line.strip() in ['LP', 'TD'] : #debut texte
83 if keepline and line.strip() not in ['LP', 'TD', ''] :
84 ucis[-1][1] = '\n'.join([ucis[-1][1],line.replace('*', ' ')])
87 def print_ucis(ucis, ofile, encodage) :
88 #elimination des articles vides
89 ucis = [uci for uci in ucis if uci[1].strip() != '']
90 toprint = '\n\n'.join(['\n'.join([' '.join(uci[0]),uci[1]]) for uci in ucis])
91 ofile.write(toprint + '\n')
94 class ParseFactivaPaste :
96 def __init__(self, txtdir, fileout, encodage_in, encodage_out) :
98 for root, subfolders, subfiles in os.walk(txtdir) :
99 nf = [os.path.join(root, f) for f in subfiles if f.split('.')[-1] == 'txt']
103 with open(fileout,'w') as outf :
106 with codecs.open(f, 'r', encodage_in) as infile :
107 content = infile.read()
108 ucis = parsetxtpaste(content)
109 print_ucis(ucis, outf, encodage_out)
111 print('ok', len(ucis), 'articles', ' - total : ', tot)
113 # execution en direct ???
114 if __name__ == '__main__' :
115 doparse(txtdir, fileout, encodage_in, encodage_out)