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