multisplit
[iramuteq] / parse_factiva_txt.py
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
5 #License: GNU/GPL
6
7 #------------------------------------
8 # import des modules python
9 #------------------------------------
10 import os
11 import codecs
12 import re
13
14
15 mois = {'janvier' : '01', 
16         'février' : '02',
17         'mars' : '03',
18         'avril' : '04', 
19         'mai' : '05',
20         'juin' : '06',
21         'juillet' : '07',
22         'août' : '08',
23         'septembre' : '09',
24         'octobre' : '10',
25         'novembre' : '11',
26         'décembre' : '12', 
27         'january' : '01',
28         'february': '02',
29         'march' : '03',
30         'april': '04',
31         'may': '05',
32         'june' : '06',
33         'july': '07',
34         'august': '08',
35         'september' : '09',
36         'october': '10',
37         'november': '11',
38         'december': '12'}
39
40
41 def parsetxtpaste(txt):
42     """
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 :)
46     """
47
48     no = ['NS','RE','IPD','CO','IN']  # les balises qui signalent une fin
49     txt = txt.splitlines()
50     keepline = False
51     ucis = []
52     for line in txt : 
53         if line.startswith('Article') :
54             lp = line.split()
55             if len(lp) > 2  :
56                 if lp[2] == 'Article' or lp[2] == 'Next' or lp[2] == 'Previous':
57                     ucis.append([['****'],''])
58                     keepline = False
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
76             keepline = False
77         elif line.startswith('RF ') : #fin
78             keepline = False
79         elif line.strip() in ['LP', 'TD'] : #debut texte
80             keepline = True
81         else :
82             pass
83         if keepline and line.strip() not in ['LP', 'TD', ''] :
84             ucis[-1][1] = '\n'.join([ucis[-1][1],line.replace('*', ' ')])
85     return ucis
86
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')
92
93
94 class ParseFactivaPaste :
95
96     def __init__(self, txtdir, fileout, encodage_in, encodage_out) :
97         files = []
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']
100             nf.sort()
101             files += nf
102         tot = 0
103         with open(fileout,'w') as outf : 
104             for f in files : 
105                 print(f)
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)
110                 tot += len(ucis)
111                 print('ok', len(ucis), 'articles', ' - total : ', tot)
112
113 # execution en direct ???
114 if __name__ == '__main__' :
115     doparse(txtdir, fileout, encodage_in, encodage_out)
116     print('fini')