Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / scripts / importer / hyperprince / compiler.groovy @ 1000

History | View | Annotate | Download (9.2 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: 2017-01-24 18:11:42 +0100 (mar. 24 janv. 2017) $
25
// $LastChangedRevision: 3400 $
26
// $LastChangedBy: mdecorde $ 
27
//
28
package org.txm.scripts.importer.hyperprince
29
;
30

    
31
import org.txm.importer.cwb.CwbEncode
32
import org.txm.importer.cwb.CwbMakeAll
33
import org.txm.scripts.importer.*;
34
import org.txm.scripts.*;
35
import org.txm.importer.scripts.xmltxm.BuildTTSrc;
36
import org.txm.importer.scripts.xmltxm.*;
37
import org.txm.utils.treetagger.TreeTagger;
38

    
39
import javax.xml.stream.*;
40
import java.net.URL;
41
import java.io.File;
42
import java.net.URL;
43
import java.util.HashMap;
44
import java.util.List;
45

    
46
// TODO: Auto-generated Javadoc
47
/**
48
 * The Class compiler.
49
 */
50
class compiler 
51
{
52
        
53
        /** The input data. */
54
        private def inputData;
55
        
56
        /** The factory. */
57
        private def factory;
58
        
59
        /** The parser. */
60
        private def parser;
61
        
62
        /** The dir. */
63
        private def dir;
64
        
65
        /** The output. */
66
        private def output;
67
        
68
        /** The url. */
69
        private def url;
70
        
71
        /** The anahash. */
72
        private HashMap<String,String> anahash =new HashMap<String,String>() ;
73
        
74
        /** The text. */
75
        String text="";
76
        
77
        /** The base. */
78
        String base="";
79
        
80
        /** The project. */
81
        String project="";
82
        
83
        /** The lang. */
84
        private String lang ="fr";
85
        
86
        /**
87
         * Instantiates a new compiler.
88
         */
89
        public compiler(){}
90
        
91
        /**
92
         * initialize.
93
         *
94
         * @param url the url
95
         * @param text the text
96
         * @param base the base
97
         * @param project the project
98
         */
99
        public compiler(URL url,String text,String base, String project)
100
        {
101
                this.text = text
102
                this.base = base;
103
                this.project = project;
104
                
105
                try {
106
                        this.url = url;
107
                        inputData = url.openStream();
108
                        
109
                        factory = XMLInputFactory.newInstance();
110
                        parser = factory.createXMLStreamReader(inputData);
111
                } catch (XMLStreamException ex) {
112
                        System.out.println(ex);
113
                }catch (IOException ex) {
114
                        System.out.println("IOException while parsing ");
115
                }
116
        }
117
        
118
        /**
119
         * set the language of the corpus.
120
         *
121
         * @param lang the lang
122
         * @return the java.lang. object
123
         */
124
        public setLang(String lang)
125
        {
126
                this.lang = lang;
127
        }
128
        
129
        /**
130
         * Creates the output.
131
         *
132
         * @param dirPathName the dir path name
133
         * @param fileName the file name
134
         * @return true, if successful
135
         */
136
        private boolean createOutput(String dirPathName, String fileName){
137
                try {
138
                        File f = new File(dirPathName, fileName)
139
                        output = new OutputStreamWriter(new FileOutputStream(f,f.exists()) , "UTF-8")
140
                        return true;
141
                } catch (Exception e) {
142
                        System.out.println(e.getLocalizedMessage());
143
                        return false;
144
                }
145
        }
146
        
147
        /**
148
         * clear anaHash variable, it is used to store ana tags values then print it when the end element </ana> is found.
149
         */
150
        private void fillanaHash()
151
        {
152
                anahash.clear();
153
                for(String s : types)
154
                anahash.put( s,"-" );
155
        }
156
        
157
        /**
158
         * Transfom file cqp.
159
         *
160
         * @param dirPathName the dir path name
161
         * @param fileName the file name
162
         * @param idtext the idtext
163
         * @return true, if successful
164
         */
165
        public boolean transfomFileCqp(String dirPathName, String fileName,idtext)
166
        {
167
                createOutput(dirPathName, fileName);
168
                
169
                String author = "";
170
                String date = "";
171
                String lang = "";
172
                String vAna = "";
173
                String vForm = "";
174
                String wordid= "";
175
                
176
                boolean flagAuthor = false;
177
                boolean flagDate = false;
178
                boolean flagForm = false;
179
                boolean flagAna = false;
180
                output.write("<txmcorpus lang=\""+lang+"\">\n"); 
181
                try 
182
                {
183
                        for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) 
184
                        {
185
                                switch (event) 
186
                                {
187
                                        case XMLStreamConstants.START_ELEMENT:
188
                                        switch (parser.getLocalName()) 
189
                                        {
190
                                                
191
                                                case "monogr"://get attr lang
192
                                                lang = parser.getAttributeValue(null,"lang");
193
                                                //idtext = parser.getAttributeValue(null,"id");
194
                                                break;
195
                                                
196
                                                case "author"://get text
197
                                                flagAuthor = true;
198
                                                author ="";
199
                                                break;
200
                                                
201
                                                case "date"://get text
202
                                                flagDate = true;
203
                                                date = "";
204
                                                break;
205
                                                
206
                                                case "text"://write tag+ lang + date + author
207
                                                output.write("<text lang=\""+lang+"\" auteur=\""+author+"\" date=\""+date+"\" id=\""+idtext+"\" base=\""+base+"\" project=\""+project+"\">\n");
208
                                                break;
209
                                                
210
                                                case "front"://get type,id, write tag
211
                                                case "back"://get type,id, write tag
212
                                                case "head"://get type,id, write tag
213
                                                case "seg"://get type,id,n write tag
214
                                                output.write("<"+parser.getLocalName()+" id=\""+parser.getAttributeValue(null,"id")+"\" type=\""+parser.getAttributeValue(null,"type")+"\">\n");
215
                                                break;
216
                                                
217
                                                case "body"://get type,id, write tag BODY
218
                                                //        for(int i = 0 ; i < parser.getAttributeCount(); i++)
219
                                                                
220
                                                output.write("<body id=\""+parser.getAttributeValue(null,"id")+"\" type=\""+parser.getAttributeValue(null,"type")+"\">\n");
221
                                                break;
222
                                                
223
                                                case "s":
224
                                                output.write( "<s>\n");
225
                                                break;
226
                                                
227
                                                case "w":
228
                                                wordid = parser.getAttributeValue(null,"id")
229
                                                break;
230
                                                case "form":
231
                                                flagForm = true;
232
                                                vForm = "";
233
                                                vAna ="";
234
                                                break;
235
                                                
236
                                                case "ana":
237
                                                flagAna = true;
238
                                                break;
239
                                        }
240
                                        break;
241
                                        
242
                                        case XMLStreamConstants.END_ELEMENT:
243
                                        switch (parser.getLocalName()) 
244
                                        {
245
                                                case "author"://get text
246
                                                flagAuthor = false;
247
                                                break;
248
                                                
249
                                                case "date"://get text
250
                                                flagDate = false;
251
                                                break;
252
                                                
253
                                                case "text"://write tag+ lang + date + author
254
                                                output.write("</text>\n");
255
                                                break;
256
                                                
257
                                                case "front"://get type,id, write tag
258
                                                case "back"://get type,id, write tag
259
                                                case "head"://get type,id, write tag
260
                                                case "seg"://get type,id,n write tag
261
                                                output.write("</"+parser.getLocalName()+">\n");
262
                                                break;
263
                                                
264
                                                case "div0"://get type,id, write tag BODY
265
                                                output.write("</body>\n");
266
                                                break;
267
                                                
268
                                                case "s":
269
                                                output.write( "</s>\n");
270
                                                break;
271
                                                
272
                                                case "w":
273
                                                output.write( vForm +vAna+"\t"+wordid+"\n");
274
                                                vAna = "";
275
                                                vForm = "";
276
                                                break;
277
                                                
278
                                                case "form":
279
                                                flagForm = false;
280
                                                break;
281
                                                
282
                                                case "ana":
283
                                                flagAna = false;
284
                                                break;
285
                                        }
286
                                        break;
287
                                        
288
                                        case XMLStreamConstants.CHARACTERS:
289
                                        if(flagForm)
290
                                        vForm += parser.getText().trim();
291
                                        if(flagAna)
292
                                        vAna += "\t" +parser.getText().trim();
293
                                        if(flagDate)
294
                                        date += parser.getText().trim();
295
                                        if(flagAuthor)
296
                                        author += parser.getText().trim();
297
                                        break;
298
                                }
299
                        }
300
                        output.write("</txmcorpus>"); 
301
                        output.close();
302
                        parser.close();
303
                        inputData.close();
304
                }
305
                catch (XMLStreamException ex) {
306
                        System.out.println(ex);
307
                }
308
                catch (IOException ex) {
309
                        System.out.println("IOException while parsing " + inputData);
310
                }
311
                
312
                return true;
313
        }
314
        
315
        /**
316
         * Run.
317
         *
318
         * @param rootDir the root dir
319
         */
320
        public boolean run(String rootDir) 
321
        {
322
                if (!(CwbEncode.isExecutableAvailable() && CwbMakeAll.isExecutableAvailable())) {
323
                        println ("Error: CWB executables not well set.")
324
                        return false;
325
                }
326
                new File(rootDir,"cqp/hyperprince.cqp").delete();//cleaning&preparing
327
                new File(rootDir,"cqp/").deleteDir();
328
                new File(rootDir,"cqp/").mkdir();
329
                new File(rootDir,"registry/").mkdir();
330
                
331
                //1- Transform into CQP file
332
                List<File> files = new File(rootDir,"txm").listFiles();
333
                for(File f : files) {
334
                        if(!f.exists()) {
335
                                println("file "+f+ " does not exists")        
336
                        }
337
                        else
338
                        {        
339
                                println("process file "+f)
340
                                String txtname = f.getName().substring(0,f.getName().length()-4);
341
                                def builder = new compiler(f.toURL(),txtname, "hyperprince", "default");
342
                                builder.setLang lang
343
                                builder.transfomFileCqp(rootDir,"cqp/hyperprince.cqp",f.getName());
344
                        }
345
                }
346
                
347
                //2- Import into CWB
348
                def outDir =rootDir;
349
                def outDirTxm = rootDir;
350
                
351
                CwbEncode cwbEn = new CwbEncode();
352
                cwbEn.setDebug true;
353
                CwbMakeAll cwbMa = new CwbMakeAll();
354
                cwbMa.setDebug true;
355
                
356
                String[] pAttributes = ["pos","lemme","id"];
357
                String[] sAttributes = ["txmcorpus:0+lang", "text:0+auteur+date+id+lang+base+project","front:0+type+id","body:0+type+id","back:0+type+id","head:0+type+id", "seg:0+type+n+id"];
358
                
359
                try {
360
                        String regPath =outDirTxm + "/registry/"+"hyperprince"
361
                        cwbEn.run(outDirTxm + "data", outDir + "/cqp/"+"hyperprince.cqp", regPath,pAttributes, sAttributes);
362
                        if (!new File(regPath).exists()) {
363
                                println "Error: The registry file was not created: $regPath. See https://groupes.renater.fr/wiki/txm-users/public/faq"
364
                                return false;
365
                        }
366
                        cwbMa.run("HYPERPRINCE", outDirTxm + "/registry");
367
                } catch (Exception ex) {System.out.println(ex); return false;}
368
                
369
                System.out.println("Done.") 
370
                return true;
371
        }
372
}