Statistics
| Revision:

root / tmp / org.txm.core / src / java / org / txm / scripts / importer / WExtract.groovy @ 1000

History | View | Annotate | Download (8.6 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: 2015-12-17 12:11:39 +0100 (jeu. 17 déc. 2015) $
25
// $LastChangedRevision: 3087 $
26
// $LastChangedBy: mdecorde $ 
27
//
28
package org.txm.scripts.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 WExtract 
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, int max)
69
        {
70
                println "Process "+infile.getName()+", keep $max words"
71
                int count = this.countW(infile);
72
                if(count < max)
73
                {
74
                        println "can't extract $max words, the file "+infile.getName()+" contains only $count words"
75
                        return;
76
                }
77
                String ms = "#ms_K"
78
                int tier = max/3;
79
                int from1 = 0 
80
                int to1 = tier;
81
                int from2 = (count/2) - (tier/2);
82
                int to2 =(count/2) + (tier/2);
83
                int from3 = count -tier;
84
                int to3= count-1;
85
                boolean isSic = false;
86
                boolean isW = false;
87
                boolean isText = false;
88
                boolean printW = true;
89
                int wcount=0;
90
                
91
                println " count : "+count
92
                println "  get from "+from1+" to "+to1
93
                println "  get from "+from2+" to "+to2
94
                println "  get from "+from3+" to "+to3
95
                
96
                
97
                String localname;
98
                String prefix;
99
                InputStream inputData = infile.toURI().toURL().openStream();
100
                XMLInputFactory inputfactory = XMLInputFactory.newInstance();
101
                XMLStreamReader parser = inputfactory.createXMLStreamReader(inputData);
102
                XMLOutputFactory factory = XMLOutputFactory.newInstance();
103
                
104
                FileOutputStream output = new FileOutputStream(outfile);
105
                XMLStreamWriter writer = factory.createXMLStreamWriter(output, "UTF-8");
106
                
107
                writer.writeStartDocument("utf-8", "1.0");
108
                
109
                for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) 
110
                {
111
                        if(isText)
112
                        {
113
                                if((wcount >= from1 && wcount <= to1 )||
114
                                (wcount >= from2 && wcount <= to2) ||
115
                                (wcount >= from3 && wcount <= to3))
116
                                        printW = true;
117
                                else
118
                                        printW = false;
119
                        }
120
                        else
121
                                printW = true;
122
                        
123
                        switch (event) 
124
                        {
125
                                case XMLStreamConstants.START_ELEMENT:
126
                                        localname = parser.getLocalName();
127
                                        prefix = parser.getPrefix();
128
                                
129
                                /*
130
                                 if(localname == "supplied")
131
                                 if(parser.getAttributeValue(null,"source") != null)
132
                                 ms = parser.getAttributeValue(null,"source")
133
                                 if(localname == "sic")
134
                                 {
135
                                 isSic= true;
136
                                 }
137
                                 */
138
                                        if(localname == "text")
139
                                                isText = true;
140
                                
141
                                        if(localname == "w")
142
                                        {
143
                                                isW= true;
144
                                                wcount++;
145
                                                
146
                                                if(isText)
147
                                                {
148
                                                        if((wcount >= from1 && wcount <= to1 )||
149
                                                        (wcount >= from2 && wcount <= to2) ||
150
                                                        (wcount >= from3 && wcount <= to3))
151
                                                                printW = true;
152
                                                        else
153
                                                                printW = false;
154
                                                }
155
                                                else
156
                                                        printW = true;
157
                                        }
158
                                
159
                                /*if(!isSic)
160
                                 if(localname != "choice" && localname != "corr" && localname != "sic" && localname != "supplied" && localname != "seg")
161
                                 {*/
162
                                        if(localname == "w")
163
                                        {
164
                                                if(printW)
165
                                                {
166
                                                        if(prefix != null && prefix.length() > 0)
167
                                                                writer.writeStartElement(prefix+":"+localname);
168
                                                        else
169
                                                                writer.writeStartElement(localname);
170
                                                        
171
                                                        for(int i= 0 ; i < parser.getAttributeCount() ;i++ )
172
                                                        {
173
                                                                if(parser.getAttributePrefix(i)!= "")
174
                                                                        writer.writeAttribute(parser.getAttributePrefix(i)+":"+parser.getAttributeLocalName(i), parser.getAttributeValue(i));
175
                                                                else
176
                                                                        writer.writeAttribute(parser.getAttributeLocalName(i), parser.getAttributeValue(i));
177
                                                        }
178
                                                        //writer.writeAttribute("srcmf:src", ms);
179
                                                }
180
                                        }
181
                                        else
182
                                        {
183
                                                if(prefix != null && prefix.length() > 0)
184
                                                        writer.writeStartElement(prefix+":"+localname);
185
                                                else
186
                                                        writer.writeStartElement(localname);
187
                                                
188
                                                if(localname == "teiHeader")
189
                                                {
190
                                                        writer.writeAttribute("xmlns:me", "http://www.menota.org/ns/1.0");
191
                                                        writer.writeAttribute("xmlns:bfm", "http://bfm.ens-lsh.fr/ns/1.0");
192
                                                        //writer.writeAttribute("xmlns:srcmf", "https://listes.cru.fr/wiki/srcmf/index");
193
                                                }
194
                                                
195
                                                if(localname == "TEI")
196
                                                {
197
                                                        writer.writeAttribute("xmlns","http://www.tei-c.org/ns/1.0");
198
                                                }
199
                                                
200
                                                for(int i= 0 ; i < parser.getAttributeCount() ;i++ )
201
                                                {
202
                                                        if(parser.getAttributePrefix(i)!= "")
203
                                                                writer.writeAttribute(parser.getAttributePrefix(i)+":"+parser.getAttributeLocalName(i), parser.getAttributeValue(i));
204
                                                        else
205
                                                                writer.writeAttribute(parser.getAttributeLocalName(i), parser.getAttributeValue(i));
206
                                                }
207
                                        }
208
                                //}
209
                                        break;
210
                                
211
                                case XMLStreamConstants.END_ELEMENT:
212
                                        localname =parser.getLocalName()
213
                                
214
                                /*if(localname == "sic")
215
                                 isSic= false;
216
                                 if(localname == "w")
217
                                 isW= false;
218
                                 if(localname == "supplied" && ms != "#ms_K")
219
                                 ms = "#ms_K";
220
                                 if(!isSic)
221
                                 if(localname != "choice" && localname != "corr" && localname != "sic" && localname != "supplied" && localname != "seg")
222
                                 {*/
223
                                        if(localname == "w")
224
                                        {
225
                                                if(printW)
226
                                                {
227
                                                        writer.writeEndElement();
228
                                                        writer.writeCharacters("\n");
229
                                                }
230
                                        }
231
                                        else
232
                                        {        
233
                                                writer.writeEndElement();
234
                                                writer.writeCharacters("\n");
235
                                        }
236
                                //        }
237
                                
238
                                        break;
239
                                
240
                                case XMLStreamConstants.CHARACTERS:
241
                                //if(!isSic)
242
                                        if(isW)
243
                                        {
244
                                                if(printW)
245
                                                {
246
                                                        writer.writeCharacters(parser.getText().trim());
247
                                                }
248
                                        }
249
                                        else
250
                                                writer.writeCharacters(parser.getText().trim());
251
                                        break;
252
                        }
253
                }
254
                writer.flush();
255
                writer.close();
256
                output.close()
257
                inputData.close();
258
        }
259
        
260
        /**
261
         * Count w.
262
         *
263
         * @param infile the infile
264
         * @return the int
265
         */
266
        public int countW(File infile)
267
        {
268
                InputStream inputData = infile.toURI().toURL().openStream();
269
                XMLInputFactory inputfactory = XMLInputFactory.newInstance();
270
                XMLStreamReader parser = inputfactory.createXMLStreamReader(inputData);
271
                
272
                int count = 0;
273
                for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) 
274
                {
275
                        switch (event) 
276
                        {
277
                                case XMLStreamConstants.START_ELEMENT:
278
                                        if(parser.getLocalName() == "w")
279
                                                count++;
280
                        }
281
                }
282
                inputData.close()
283
                return count;
284
        }
285
        
286
        /**
287
         * The main method.
288
         *
289
         * @param args the arguments
290
         */
291
        public static void main(String[] args)
292
        {
293
                String userDir = System.getProperty("user.home");
294
                
295
                File directory = new File(userDir+"/xml/extract/");
296
                File outdir = new File(userDir+"/xml/extract/","results");
297
                outdir.mkdir();
298
                
299
                File maxfile = new File(userDir+"/xml/extract/maxfile");
300
                /*
301
                 * maxfile format:
302
                 * 
303
                 * filename1.xml        45000
304
                 * filename2.xml        22500
305
                 * filename3.xml        45000
306
                 */
307
                HashMap<File, Integer> maxperfile = new HashMap<File, Integer>();
308
                maxfile.eachLine{it->
309
                        String[] split = it.split("\t");
310
                        if(split.length == 2)
311
                        {
312
                                try
313
                                {
314
                                String filename = it.split("\t")[0];
315
                                int max = Integer.parseInt(it.split("\t")[1])
316
                                maxperfile.put(filename, max);
317
                                }catch(Exception e ){}
318
                        }
319
                }
320
                println maxperfile;
321
                
322
                def files = directory.listFiles();
323
                for(File infile : files)
324
                {
325
                        
326
                        if(maxperfile.containsKey(infile.getName()))
327
                        {
328
                                File outfile = new File(outdir, infile.getName());
329
                                int max= maxperfile.get(infile.getName());
330
                                new WExtract().process(infile, outfile, max)
331
                        }
332
                }
333
        }
334
}