...
[iramuteq] / import_txm.py
1 #!/bin/env python
2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2013 Pierre Ratinaud
5 #License: GNU/GPL
6
7
8 import os
9 import xml.sax
10 import glob
11
12
13
14 #infiledir = '/home/pierre/TXM/corpus/voeux-bin/txm/VOEUX/'
15 #fileout = 'VOEUXExportfromTXM.txt'
16
17
18 class TXMParser(xml.sax.ContentHandler) :
19     def __init__(self, fileout, encodage_out) :
20         self.fileout = fileout
21         self.encodage_out = encodage_out
22         self.sent = []
23
24     def startElement(self, name, attrs) :
25         self.name = name
26         if name == 'title' :
27             pass
28         if name == 's' :
29             pass
30         if name == 'taxonomy' :
31             pass
32         if name == 'text' :
33             self.text2stars(attrs)
34         if name == 'w' :
35             pass
36
37     def endElement(self, name) :
38         if name == 's' or name == 'w' :
39             self.printsent()
40         if name == 'p' :
41             self.printsent()
42             self.fileout.write('\n')
43           
44     def characters(self, content) :
45         if self.name == 'txm:form' :
46             if content not in [u'', u' ', u'\n', '\r'] :
47                 self.sent.append(content.rstrip('\n\r'))
48             #self.fileout.write(content.encode('utf8'))
49
50     def text2stars(self, attrs) :
51         stars = ['_'.join(val).replace(' ', '_').replace("'", '_').replace('/','').replace('.','').replace(';', '').replace(':', '').replace(u'ยท','') for val in attrs.items()]
52         stars = [''.join([u'*', val]) for val in stars]
53         stars = u'**** ' + ' '.join(stars)
54         self.fileout.write(stars.encode(self.encodage_out))
55         self.fileout.write('\n')
56
57     def printsent(self) :
58         if self.sent != [] :
59             sent = ' ' + ' '.join(self.sent)
60             for val in [u' .', u' ,', u' ;', u' :', u' ?', u' !', u' -'] :
61                 sent = sent.replace(val, val.strip())
62             sent = sent.replace("' ", "'")
63             self.fileout.write(sent.encode(self.encodage_out))
64         self.sent = []
65
66
67
68 class TXM2IRA :
69     def __init__(self, pathin, fileout, encodage_in, encodage_out) :
70         parser = xml.sax.make_parser()
71         files = glob.glob(os.path.join(pathin,'*.xml'))
72         with open(fileout, 'w') as fout :
73             parser.setContentHandler(TXMParser(fout, encodage_out))
74             for f in files :
75                 parser.parse(open(f, 'r'))
76                 fout.write('\n\n')
77         print 'done'
78
79 #TXM2IRA(infiledir, fileout)