ajout d'un saut de ligne
[iramuteq] / parse_factiva_xml.py
1 #!/bin/env python
2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2008-2010 Pierre Ratinaud
5 #Lisense: GNU/GPL
6
7 import xml.dom.minidom
8 import wx.lib.sized_controls as sc
9 import wx.lib.filebrowsebutton as filebrowse
10 import os
11 import codecs
12 import sys
13 import re
14 import wx
15 from parse_factiva_mail import ParseFactivaMail
16 from parse_factiva_txt import ParseFactivaPaste
17
18 def ParseDocument(filename) :
19     print filename
20     with codecs.open(filename, 'r', 'utf-8') as f :
21         content = f.read()
22     content = content.replace('<hlt>', ' ').replace('</hlt>', ' ')
23     dom = xml.dom.minidom.parseString(content.encode("utf-8"))
24     result = []
25     articles = dom.getElementsByTagName("article")
26     i = 0
27     for article in articles :
28         headline = article.getElementsByTagName("headline")
29         if headline != [] :
30             para_headline = headline[0].getElementsByTagName("paragraph")
31             val_headline = [val.firstChild.nodeValue.replace('\n', ' ') for val in para_headline]
32         else :
33             val_headline = []
34         leadParagraph = article.getElementsByTagName("leadParagraph")
35         if leadParagraph != [] :
36             para_leadParagraph = leadParagraph[0].getElementsByTagName("paragraph")
37             val_leadParagraph = [val.firstChild.nodeValue.replace('\n', ' ') for val in para_leadParagraph]
38         else :
39             val_leadParagraph = []
40         publicationDate = article.getElementsByTagName("publicationDate")
41         if publicationDate != [] :
42             para_publicationDate = publicationDate[0].getElementsByTagName("date")
43             if para_publicationDate == [] :
44                 para_publicationDate = publicationDate[0].getElementsByTagName("dateTime")
45             val_publicationDate = [val.firstChild.nodeValue.replace('\n', ' ') for val in para_publicationDate]
46         else :
47             val_publicationDate = []
48         sourceName = article.getElementsByTagName("sourceName")
49         if sourceName != [] :
50             val_sourceName = sourceName[0].firstChild.nodeValue.replace('\n', ' ')
51         else :
52             val_sourceName = INCONNU
53         tailParagraphs = article.getElementsByTagName("tailParagraphs")
54         if tailParagraphs != [] :
55             para_tailParagraphs = tailParagraphs[0].getElementsByTagName("paragraph")
56             val_tailParagraphs = [val.firstChild.nodeValue.replace('\n', ' ') for val in para_tailParagraphs]
57         else :
58             val_tailParagraphs = []
59         inter = [' '.join(val_headline), val_sourceName,' '.join(val_publicationDate), ' '.join(val_leadParagraph), ' '.join(val_tailParagraphs)]
60         inter = [re.sub(ur'[ "\n\r]+', ' ',  val).replace('"',' ').replace('\n', ' ').replace('\r', ' ')  for val in inter]
61         #inter = ['"' + val +'"' for val in inter]
62         result.append(inter)
63     return result
64     
65 def getcorpus_from_xml(xmldir, corpus_out):
66     files = os.listdir(xmldir)
67     files = [os.path.join(xmldir,file) for file in files if os.path.splitext(file)[1] == '.xml']
68     if len(files) == 0 :
69         return 'nofile'
70     result = []
71     fileout = codecs.open(corpus_out, 'w', 'utf-8')
72     for file in files :
73         rs = ParseDocument(file)
74         #dates = [row[2].split('-') for row in rs]
75         #dates = [[date[0],date[1],date[2].split('T')[0]] for date in dates]
76         #txt = '\n'.join(['\n'.join([' '.join([u'****', '*%s' % row[1].replace(' ','_').replace('\'','_'), '*%s' % row[2].replace('-','_')]), row[3], row[4]]) for row in rs])
77         #avec la date decompose
78         txt = '\n'.join(['\n'.join([' '.join([u'****', '*s_%s' % row[1].replace(' ','').replace('\'',''), '*annee_%s' % row[2].split('-')[0], '*mois_%s' % row[2].split('-')[1], '*jour_%s' % row[2].split('-')[2].split('T')[0]]), row[3], row[4]]) for row in rs])
79         fileout.write(txt+'\n\n')
80     fileout.close()
81     return 'ok'
82
83 class PrefImport(wx.Dialog):
84     def __init__(self, parent, size=wx.DefaultSize, pos=wx.DefaultPosition, style=wx.DEFAULT_DIALOG_STYLE, methode = 'mail'):
85         pre = wx.PreDialog()
86         pre.SetExtraStyle(wx.DIALOG_EX_CONTEXTHELP)
87         pre.Create(parent, -1, '', pos, size, style)
88         self.PostCreate(pre)
89         if methode == 'xml' :
90             txt = _(u'Select a directory of xml files')
91         else :
92             txt = _(u'Select a directory of txt files')
93         self.parent = parent
94         self.txt1 = wx.StaticText(self, -1, txt.encode('utf8'))
95         self.dbb = filebrowse.DirBrowseButton(self, -1, size=(450, -1), changeCallback = self.fbbCallback)
96         self.dbb.SetLabel("")
97         self.txt2 = wx.StaticText(self, -1, _(u'Output file').encode('utf8'))
98         self.fbb = filebrowse.FileBrowseButton(self, -1, size=(450, -1), fileMode = 2)
99         self.fbb.SetLabel("")
100
101         self.btnsizer = wx.StdDialogButtonSizer()
102         btn_ok = wx.Button(self, wx.ID_OK)
103         btn = wx.Button(self, wx.ID_CANCEL)
104         self.btnsizer.AddButton(btn_ok)
105         self.btnsizer.AddButton(btn)
106         self.btnsizer.Realize()
107
108
109         self.Bind(wx.EVT_BUTTON, self.checkfile, btn_ok)
110
111         #self.SetButtonSizer(self.CreateStdDialogButtonSizer(wx.OK | wx.CANCEL))
112         self.Bind(wx.EVT_BUTTON, self.checkfile)
113
114         self. __do_layout()
115         #self.Fit()
116         self.SetMinSize(self.GetSize())
117     
118     def __do_layout(self):
119         sizer = wx.BoxSizer(wx.VERTICAL)
120         grid_sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
121         grid_sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
122         grid_sizer_1.Add(self.txt1, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL, 0)
123         grid_sizer_1.Add(self.dbb, 2, wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL, 0)
124         grid_sizer_2.Add(self.txt2, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL, 0)
125         grid_sizer_2.Add(self.fbb, 2, wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL, 0)
126         sizer.Add(grid_sizer_1, 0,  wx.EXPAND, 0)
127         sizer.Add(grid_sizer_2, 0,  wx.EXPAND, 0)
128         sizer.Add(self.btnsizer, 0,  wx.EXPAND, 0)
129         self.SetSizer(sizer)
130         sizer.Fit(self)
131         self.Layout()
132
133    
134     def fbbCallback(self, evt):
135         if self.fbb.GetValue() == "" :
136              self.fbb.SetValue(os.path.join(self.dbb.GetValue(), 'corpus.txt'))
137         #self.log.write('FileBrowseButton: %s\n' % evt.GetString())
138
139     def checkfile(self, evt) :
140         if evt.GetId() == wx.ID_OK :
141             if self.dbb.GetValue() != "" :
142                 if os.path.exists(self.fbb.GetValue()):
143                     dlg = wx.MessageDialog(self, 
144                     u"%s\nCe fichier existe, continuer quand même ?" % self.fbb.GetValue(), 'ATTENTION', wx.NO | wx.YES | wx.ICON_WARNING)
145                     dlg.CenterOnParent()
146                     if dlg.ShowModal() not in [wx.ID_NO, wx.ID_CANCEL]:
147                         self.EndModal(wx.ID_OK)
148                 else :
149                     self.EndModal(wx.ID_OK)
150             else :
151                 dlg = wx.MessageDialog(self, u"Vous devez choisir le répertoire contenant le ou les fichier(s) xml", 'ATTENTION', wx.OK | wx.ICON_WARNING)
152                 dlg.CenterOnParent()
153                 dlg.ShowModal()
154
155         else :
156             self.EndModal(wx.ID_CANCEL)
157
158
159
160 class ImportFactiva():
161     def __init__(self, parent, methode):
162        self.dial =  PrefImport(parent, methode=methode)
163        self.dial.CenterOnParent()
164        val = self.dial.ShowModal()
165        if val == wx.ID_OK :
166            xmldir = self.dial.dbb.GetValue()
167            corp_out = self.dial.fbb.GetValue()
168            if methode == 'xml' :
169                 res = getcorpus_from_xml(xmldir, corp_out)
170            elif methode == 'mail' :
171                res = ParseFactivaMail(xmldir, corp_out, 'utf8', parent.syscoding)
172            elif methode == 'txt' :
173                res = ParseFactivaPaste(xmldir, corp_out, 'utf8', parent.syscoding)
174            if res == 'nofile' :
175                 dlg = wx.MessageDialog(parent, u"Pas de fichier \'.xml\' dans %s" % xmldir, 'ATTENTION', wx.OK | wx.NO_DEFAULT | wx.ICON_WARNING)
176                 dlg.CenterOnParent()
177                 dlg.ShowModal()
178            #else :
179            #     parent.filename = corp_out
180            #     parent.OpenText()