2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2012 Pierre Ratinaud
11 txtdir = 'dev/factiva_txt'
12 fileout = 'dev/factiva_txt_out.txt'
19 parser de texte pour factiva
20 à partir d'un copier/coller de la fenêtre de visualisation
21 merci à Lucie Loubère pour l'astuce :)
23 no = ['NS','RE','IPD','CO','IN'] # les balises qui signalent une fin
24 txt = txt.splitlines()
28 if line.startswith('Article') :
31 if lp[2] == 'Article' :
32 ucis.append([[u'****'],''])
34 if line.startswith('SN ') : #source
35 source = '*source_' + line[4:].replace(' ','').replace('\'','').replace(u'´','').replace(u'’','').replace('-','').lower()
36 ucis[-1][0].append(source)
37 elif line.startswith('PD ') : #date
38 mois_annee = '*date_' + line[4:].split(' ')[1] + line[4:].split(' ')[2]
39 ucis[-1][0].append(mois_annee)
40 elif line.strip() in no : #fin
42 elif line.startswith('RF ') : #fin
44 elif line.strip() in ['LP', 'TD'] : #debut texte
48 if keepline and line.strip() not in ['LP', 'TD', ''] :
49 ucis[-1][1] = '\n'.join([ucis[-1][1],line])
53 def print_ucis(ucis, ofile, encodage) :
54 #elimination des articles vides
55 ucis = [uci for uci in ucis if uci[1] != '']
56 toprint = '\n\n'.join(['\n'.join([' '.join(uci[0]),uci[1]]) for uci in ucis])
57 ofile.write(toprint.encode(encodage))
59 def doparse(txtdir, fileout, encodage_in, encodage_out):
60 files = os.listdir(txtdir)
61 with open(fileout,'w') as outf :
63 f= os.path.join(txtdir, f)
64 with codecs.open(f, 'rU', encodage_in) as infile :
65 content = infile.read()
66 ucis = parsetxt(content)
67 print_ucis(ucis, outf, encodage_out)
69 #for dat in ['2001','2002','2003','2004', '2005','2006','2007','2008','2009','2010','2011'] :
70 # path = os.path.join(txtdir,dat)
71 # outfile = os.path.join(txtdir, 'corpus_' + dat + '.txt')
72 # doparse(path, outfile)
75 if __name__ == '__main__' :
76 doparse(txtdir, fileout, encodage_in, encodage_out)