...
[iramuteq] / parse_factiva_txt.py
1 #!/bin/env python
2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2012-2013 Pierre Ratinaud
5 #Lisense: GNU/GPL
6
7 import os
8 import codecs
9
10
11 #txtdir = 'dev/factiva_txt'
12 #fileout = 'dev/factiva_txt_out.txt'
13 #encodage_in = 'utf8'
14 #encodage_out = 'utf8'
15
16
17 def parsetxtpaste(txt):
18     """
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 :)
22     """
23     no = ['NS','RE','IPD','CO','IN']  # les balises qui signalent une fin
24     txt = txt.splitlines()
25     keepline = False
26     ucis = []
27     for line in txt : 
28         if line.startswith('Article') :
29             lp = line.split()
30             if len(lp) > 2  :
31                 if lp[2] == 'Article' :
32                     ucis.append([[u'****'],''])
33                     keepline = False
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 = '*ma_' + line[4:].split(' ')[1] + line[4:].split(' ')[2]
39             ucis[-1][0].append(mois_annee)
40             annee = u'*annee_' + line[4:].split(' ')[2]
41             ucis[-1][0].append(annee)
42         elif line.strip() in no : #fin
43             keepline = False
44         elif line.startswith('RF ') : #fin
45             keepline = False
46         elif line.strip() in ['LP', 'TD'] : #debut texte
47             keepline = True
48         else :
49             pass
50         if keepline and line.strip() not in ['LP', 'TD', ''] :
51             ucis[-1][1] = '\n'.join([ucis[-1][1],line])
52     return ucis
53
54
55 def print_ucis(ucis, ofile, encodage) :
56     #elimination des articles vides
57     ucis = [uci for uci in ucis if uci[1].strip() != '']
58     toprint = '\n\n'.join(['\n'.join([' '.join(uci[0]),uci[1]]) for uci in ucis])
59     ofile.write(toprint.encode(encodage))
60
61 class ParseFactivaPaste :
62     def __init__(self, txtdir, fileout, encodage_in, encodage_out) :
63         files = os.listdir(txtdir) 
64         with open(fileout,'w') as outf : 
65             for f in files : 
66                 f= os.path.join(txtdir, f) 
67                 with codecs.open(f, 'rU', encodage_in) as infile : 
68                     content = infile.read() 
69                 ucis = parsetxtpaste(content)
70                 print_ucis(ucis, outf, encodage_out)
71
72 #for dat in ['2001','2002','2003','2004', '2005','2006','2007','2008','2009','2010','2011'] :
73 #    path = os.path.join(txtdir,dat)
74 #    outfile = os.path.join(txtdir, 'corpus_' + dat + '.txt')
75 #    doparse(path, outfile)
76
77
78 if __name__ == '__main__' :
79     doparse(txtdir, fileout, encodage_in, encodage_out)
80     print 'fini'