Statistics
| Revision:

root / tmp / org.txm.core / src / groovy / org / txm / importer / WExtractWithMode.groovy @ 187

History | View | Annotate | Download (9.3 kB)

1
// Copyright © 2010-2013 ENS de Lyon.
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
// 
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
// 
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
// 
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21
// 
22
// 
23
// 
24
// $LastChangedDate: 2011-10-19 17:50:26 +0200 (mer., 19 oct. 2011) $
25
// $LastChangedRevision: 2038 $
26
// $LastChangedBy: alavrentev $ 
27
//
28
package org.txm.importer
29

    
30
import javax.xml.parsers.DocumentBuilder;
31
import javax.xml.parsers.DocumentBuilderFactory;
32
import javax.xml.parsers.ParserConfigurationException;
33
import javax.xml.transform.OutputKeys;
34
import javax.xml.transform.Result;
35
import javax.xml.transform.Source;
36
import javax.xml.transform.Transformer;
37
import javax.xml.transform.TransformerFactory;
38
import javax.xml.transform.dom.DOMSource;
39
import javax.xml.transform.stream.StreamResult;
40

    
41
import org.w3c.dom.Document;
42
import org.w3c.dom.Element;
43
import org.w3c.dom.NodeList;
44
import org.xml.sax.SAXException;
45

    
46
import javax.xml.stream.*;
47
import java.io.File;
48
import java.net.URL;
49

    
50
// TODO: Auto-generated Javadoc
51
/**
52
 * Extract w tags from a tei file
53
 * not finished.
54
 *
55
 * @author mdecorde
56
 */
57
class WExtractWithMode 
58
{
59
        
60
        /**
61
         * Process.
62
         *
63
         * @param infile the infile
64
         * @param outfile the outfile
65
         * @param max the max
66
         * @return the java.lang. object
67
         */
68
        public process(File infile, File outfile, String modemax)
69
        {
70
                println "Process "+infile.getName()+", keep $modemax words"
71
                int count = this.countW(infile);
72
                
73
                int max = 0
74
                String mode = ""
75
                
76
                try {
77
                mode = modemax.split("/")[0]
78
                max = Integer.parseInt(modemax.split("/")[1])                
79
                }catch(Exception e ){}
80
                
81
                if(count < max)
82
                {
83
                        println "can't extract $max words, the file "+infile.getName()+" contains only $count words"
84
                        return;
85
                }
86
                //String ms = "#ms_K"
87
                int part = 0; 
88
                if (mode == "3")
89
                {
90
                        part = max/3                
91
                }
92
                else if (mode == "2")
93
                {
94
                        part = max/2
95
                }
96
                else if (mode == "1a" || mode == "1m" || mode == "1z")
97
                {
98
                        part = max
99
                }
100
                else
101
                {
102
                        println "mode must be 1a, 1m, 1z, 2 or 3"
103
                        return
104
                }
105
                int from1 = 0 
106
                int to1 = 0
107
                if (mode != "1m" && mode != "1z")
108
                {
109
                        to1 = part
110
                }
111
                int from2 = 0
112
                int to2 = 0
113
                if (mode == "3" || mode == "1m")
114
                {
115
                        from2 = (count/2) - (part/2);
116
                        to2 =(count/2) + (part/2);                        
117
                }
118
                int from3 = 0
119
                int to3 = 0
120
                if (mode != "1a" && mode != "1m")
121
                {
122
                        from3 = count -part;
123
                        to3= count-1;                        
124
                }
125
                boolean isSic = false;
126
                boolean isW = false;
127
                boolean isText = false;
128
                boolean printW = true;
129
                int wcount=0;
130
                
131
                println " count : "+count
132
                println "  get from "+from1+" to "+to1
133
                println "  get from "+from2+" to "+to2
134
                println "  get from "+from3+" to "+to3
135
                
136
                
137
                String localname;
138
                String prefix;
139
                InputStream inputData = infile.toURI().toURL().openStream();
140
                XMLInputFactory inputfactory = XMLInputFactory.newInstance();
141
                XMLStreamReader parser = inputfactory.createXMLStreamReader(inputData);
142
                XMLOutputFactory factory = XMLOutputFactory.newInstance();
143
                
144
                FileOutputStream output = new FileOutputStream(outfile)
145
                XMLStreamWriter writer = factory.createXMLStreamWriter(output, "UTF-8");
146
                
147
                writer.writeStartDocument("utf-8", "1.0");
148
                
149
                for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) 
150
                {
151
                        if(isText)
152
                        {
153
                                if((wcount >= from1 && wcount <= to1 )||
154
                                (wcount >= from2 && wcount <= to2) ||
155
                                (wcount >= from3 && wcount <= to3))
156
                                        printW = true;
157
                                else
158
                                        printW = false;
159
                        }
160
                        else
161
                                printW = true;
162
                        
163
                        switch (event) 
164
                        {
165
                                case XMLStreamConstants.START_ELEMENT:
166
                                        localname = parser.getLocalName();
167
                                        prefix = parser.getPrefix();
168
                                
169
                                /*
170
                                 if(localname == "supplied")
171
                                 if(parser.getAttributeValue(null,"source") != null)
172
                                 ms = parser.getAttributeValue(null,"source")
173
                                 if(localname == "sic")
174
                                 {
175
                                 isSic= true;
176
                                 }
177
                                 */
178
                                        if(localname == "text")
179
                                                isText = true;
180
                                
181
                                        if(localname == "w")
182
                                        {
183
                                                isW= true;
184
                                                wcount++;
185
                                                
186
                                                if(isText)
187
                                                {
188
                                                        if((wcount >= from1 && wcount <= to1 )||
189
                                                        (wcount >= from2 && wcount <= to2) ||
190
                                                        (wcount >= from3 && wcount <= to3))
191
                                                                printW = true;
192
                                                        else
193
                                                                printW = false;
194
                                                }
195
                                                else
196
                                                        printW = true;
197
                                        }
198
                                
199
                                /*if(!isSic)
200
                                 if(localname != "choice" && localname != "corr" && localname != "sic" && localname != "supplied" && localname != "seg")
201
                                 {*/
202
                                        if(localname == "w")
203
                                        {
204
                                                if(printW)
205
                                                {
206
                                                        if(prefix != null && prefix.length() > 0)
207
                                                                writer.writeStartElement(prefix+":"+localname);
208
                                                        else
209
                                                                writer.writeStartElement(localname);
210
                                                        
211
                                                        for(int i= 0 ; i < parser.getAttributeCount() ;i++ )
212
                                                        {
213
                                                                if(parser.getAttributePrefix(i)!= "")
214
                                                                        writer.writeAttribute(parser.getAttributePrefix(i)+":"+parser.getAttributeLocalName(i), parser.getAttributeValue(i));
215
                                                                else
216
                                                                        writer.writeAttribute(parser.getAttributeLocalName(i), parser.getAttributeValue(i));
217
                                                        }
218
                                                        //writer.writeAttribute("srcmf:src", ms);
219
                                                }
220
                                        }
221
                                        else
222
                                        {
223
                                                if(prefix != null && prefix.length() > 0)
224
                                                        writer.writeStartElement(prefix+":"+localname);
225
                                                else
226
                                                        writer.writeStartElement(localname);
227
                                                
228
                                                if(localname == "teiHeader")
229
                                                {
230
                                                        writer.writeAttribute("xmlns:me", "http://www.menota.org/ns/1.0");
231
                                                        writer.writeAttribute("xmlns:bfm", "http://bfm.ens-lsh.fr/ns/1.0");
232
                                                        //writer.writeAttribute("xmlns:srcmf", "https://listes.cru.fr/wiki/srcmf/index");
233
                                                }
234
                                                
235
                                                if(localname == "TEI")
236
                                                {
237
                                                        writer.writeAttribute("xmlns","http://www.tei-c.org/ns/1.0");
238
                                                }
239
                                                
240
                                                for(int i= 0 ; i < parser.getAttributeCount() ;i++ )
241
                                                {
242
                                                        if(parser.getAttributePrefix(i)!= "")
243
                                                                writer.writeAttribute(parser.getAttributePrefix(i)+":"+parser.getAttributeLocalName(i), parser.getAttributeValue(i));
244
                                                        else
245
                                                                writer.writeAttribute(parser.getAttributeLocalName(i), parser.getAttributeValue(i));
246
                                                }
247
                                        }
248
                                //}
249
                                        break;
250
                                
251
                                case XMLStreamConstants.END_ELEMENT:
252
                                        localname =parser.getLocalName()
253
                                
254
                                /*if(localname == "sic")
255
                                 isSic= false;
256
                                 if(localname == "w")
257
                                 isW= false;
258
                                 if(localname == "supplied" && ms != "#ms_K")
259
                                 ms = "#ms_K";
260
                                 if(!isSic)
261
                                 if(localname != "choice" && localname != "corr" && localname != "sic" && localname != "supplied" && localname != "seg")
262
                                 {*/
263
                                        if(localname == "w")
264
                                        {
265
                                                if(printW)
266
                                                {
267
                                                        writer.writeEndElement();
268
                                                        writer.writeCharacters("\n");
269
                                                }
270
                                        }
271
                                        else
272
                                        {        
273
                                                writer.writeEndElement();
274
                                                writer.writeCharacters("\n");
275
                                        }
276
                                //        }
277
                                
278
                                        break;
279
                                
280
                                case XMLStreamConstants.CHARACTERS:
281
                                //if(!isSic)
282
                                        if(isW)
283
                                        {
284
                                                if(printW)
285
                                                {
286
                                                        writer.writeCharacters(parser.getText().trim());
287
                                                }
288
                                        }
289
                                        else
290
                                                writer.writeCharacters(parser.getText().trim());
291
                                        break;
292
                        }
293
                }
294
                writer.flush();
295
                writer.close();
296
                output.close()
297
                inputData.close();
298
        }
299
        
300
        /**
301
         * Count w.
302
         *
303
         * @param infile the infile
304
         * @return the int
305
         */
306
        public int countW(File infile)
307
        {
308
                InputStream inputData = infile.toURI().toURL().openStream();
309
                XMLInputFactory inputfactory = XMLInputFactory.newInstance();
310
                XMLStreamReader parser = inputfactory.createXMLStreamReader(inputData);
311
                
312
                int count = 0;
313
                for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) 
314
                {
315
                        switch (event) 
316
                        {
317
                                case XMLStreamConstants.START_ELEMENT:
318
                                        if(parser.getLocalName() == "w")
319
                                                count++;
320
                        }
321
                }
322
                inputData.close()
323
                return count;
324
        }
325
        
326
        /**
327
         * The main method.
328
         *
329
         * @param args the arguments
330
         */
331
        public static void main(String[] args)
332
        {
333
                String userDir = System.getProperty("user.home");
334
                
335
                File directory = new File(userDir+"/xml/extract/");
336
                File outdir = new File(userDir+"/xml/extract/","results");
337
                outdir.mkdir();
338
                
339
                File maxfilemode = new File(userDir+"/xml/extract/maxfilemode");
340
                /*
341
                 * maxfilemode format:
342
                 * 
343
                 * filename1.xml        3        45000
344
                 * filename2.xml        1a        15000
345
                 * filename3.xml        1m        15000
346
                 * filename4.xml        1z        15000
347
                 * filename5.xml        2        22500
348
                 */
349
                HashMap<File, String> maxperfile = new HashMap<File, String>();
350
                maxfilemode.eachLine{it->
351
                        String[] split = it.split("\t");
352
                        if(split.length == 3)
353
                        {
354
                                try
355
                                {
356
                                String filename = it.split("\t")[0];
357
                                String modemax = it.split("\t")[1]+"/"+it.split("\t")[2]
358
                                maxperfile.put(filename, modemax);
359
                                }catch(Exception e ){}
360
                        }
361
                }
362
                println maxperfile;
363
                
364
                def files = directory.listFiles();
365
                for(File infile : files)
366
                {
367
                        
368
                        if(maxperfile.containsKey(infile.getName()))
369
                        {
370
                                File outfile = new File(outdir, infile.getName());
371
                                String modemax = maxperfile.get(infile.getName());
372
                                new WExtractWithMode().process(infile, outfile, modemax)
373
                        }
374
                }
375
        }
376
}