Statistics
| Revision:

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

History | View | Annotate | Download (7 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: 2016-05-26 17:42:36 +0200 (jeu. 26 mai 2016) $
25
// $LastChangedRevision: 3219 $
26
// $LastChangedBy: mdecorde $ 
27
//
28
package org.txm.scripts.importer.alceste;
29

    
30
import org.txm.importer.cwb.CwbAlign;
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.*;
36
import org.txm.utils.treetagger.TreeTagger;
37
import org.txm.core.preferences.TXMPreferences
38
import org.txm.libs.cqp.CQPLibPreferences
39

    
40
import javax.xml.stream.*;
41

    
42
import java.net.URL;
43
import java.io.File;
44
import java.util.Collection;
45
import java.util.HashMap;
46
import java.util.List;
47

    
48
// TODO: Auto-generated Javadoc
49
/**
50
 * build cqp
51
 * build CWB indexes
52
 * create registry file.
53
 *
54
 * @author mdecorde
55
 */
56
class compiler 
57
{
58
        /** The debug. */
59
        private boolean debug = false;
60
        
61
        /** The input data. */
62
        private def inputData;
63
        
64
        /** The factory. */
65
        private def factory;
66
        
67
        /** The parser. */
68
        private XMLStreamReader parser;
69
        
70
        /** The dir. */
71
        private def dir;
72
        
73
        /** The output. */
74
        private def output;
75
        
76
        /** The url. */
77
        private def url;
78
        
79
        /** The anahash. */
80
        private HashMap<String, String> anahash = new HashMap<String, String>() ;
81
        
82
        /** The text. */
83
        String text = "";
84
        
85
        /** The base. */
86
        String base = "";
87
        
88
        /** The project. */
89
        String project = "";
90
        
91
        /** The text attributes. */
92
        String[] textAttributes = null;
93
        
94
        /** The lang. */
95
        String lang = "fr";
96
        
97
        /**
98
         * initialize.
99
         *
100
         */
101
        public compiler(){}
102
        
103
        
104
        /**
105
         * Instantiates a new compiler.
106
         *
107
         * @param url the url
108
         * @param text the text
109
         * @param base the base
110
         * @param project the project
111
         */
112
        public compiler(URL url, String text, String base, String project)
113
        {
114
                this.text = text
115
                this.base = base;
116
                this.project = project;
117
                this.textAttributes = textAttributes;
118
                try {
119
                        this.url = url;
120
                        inputData = url.openStream();
121
                        
122
                        factory = XMLInputFactory.newInstance();
123
                        parser = factory.createXMLStreamReader(inputData);
124
                } catch (Exception ex) {
125
                        System.err.println("IOException while parsing: "+ex);
126
                }
127
        }
128
        
129
        /** The annotation success. */
130
        boolean annotationSuccess = false;
131
        
132
        /**
133
         * Sets the annotation success.
134
         *
135
         * @param val the new annotation success
136
         */
137
        public void setAnnotationSuccess(boolean val)
138
        {
139
                this.annotationSuccess = val;
140
        }
141
        
142
        /**
143
         * Creates the output.
144
         *
145
         * @param f the f
146
         * @return true, if successful
147
         */
148
        private boolean createOutput(File f){
149
                try {
150
                        output = new OutputStreamWriter(new FileOutputStream(f,f.exists()) , "UTF-8");
151
                        return true;
152
                } catch (Exception e) {
153
                        System.err.println(e);
154
                        return false;
155
                }
156
        }
157
        
158
        /**
159
         * Sets the lang.
160
         *
161
         * @param lang the lang
162
         * @return the java.lang. object
163
         */
164
        public setLang(String lang)
165
        {
166
                this.lang = lang;
167
        }
168
        
169
        /**
170
         * Run.
171
         *
172
         * @param rootDirFile the root dir file
173
         * @param basename the basename
174
         * @param textAttributes the text attributes
175
         * @return true, if successful
176
         */
177
        public boolean run(File binDir, File txmDir, String corpusname, String[] textAttributes) 
178
        {
179
                if (!(CwbEncode.isExecutableAvailable() && CwbMakeAll.isExecutableAvailable())) {
180
                        println ("Error: CWB executables not well set.")
181
                        return false;
182
                }
183
                if (!binDir.exists()) {
184
                        println ("binary directory does not exists: "+binDir)
185
                        return false;
186
                }
187
                File cqpFile = new File(binDir,"cqp/"+corpusname+".cqp");
188
                new File(binDir, "cqp").deleteDir();
189
                new File(binDir, "cqp").mkdir();
190
                new File(binDir, "data/$corpusname").deleteDir();
191
                new File(binDir, "data/$corpusname").mkdir();
192
                new File(binDir, "registry").mkdir();
193
                
194
                String textid = "";
195
                int counttext = 0;
196
                List<File> files = txmDir.listFiles();
197
                if (files == null || files.size() == 0) {
198
                        println "no file to compile"
199
                        return false;
200
                }
201
                
202
                //start corpus
203
                if (createOutput(cqpFile)) {
204
                        output.write("<txmcorpus lang=\""+lang+"\">\n");
205
                        output.close();
206
                }
207
                
208
                Collections.sort(files);
209
                //1- Transform into CQP file
210
                XMLTXM2CQP cqpbuilder = null;
211
                println("process "+files.size()+" files")
212
                for (File f : files) {
213
                        print "."
214
                        counttext++;
215
                        if (!f.exists()) {
216
                                println("file "+f+ " does not exists")        
217
                        } else {                        
218
                                cqpbuilder = new XMLTXM2CQP(f.toURI().toURL());
219
                                String txtname = f.getName().substring(0, f.getName().length()-4);
220
                                cqpbuilder.setTextInfo(txtname, corpusname, "default");
221

    
222
                                cqpbuilder.setBalisesToKeep(["text", "s", "p", "lb"]);
223
                                cqpbuilder.setSendToPAttributes(["lb":["n"], "p":["n"], "s":["n"]])
224
                                cqpbuilder.setLang(lang);
225
                                if (!cqpbuilder.transformFile(cqpFile)) {
226
                                        println("Failed to compile "+f)
227
                                }
228
                        }
229
                }
230
                println ""
231
                
232
                //end corpus
233
                if (createOutput(cqpFile)) {
234
                        output.write("</txmcorpus>\n");
235
                        output.close();
236
                }
237
                
238
                if (cqpbuilder == null) {
239
                        println "there was no files in bin dir : "+txmDir
240
                        return false;
241
                }
242
                
243
                //2- Import into CWB
244
                String rootDir = binDir.getAbsolutePath();
245
                CwbEncode cwbEn = new CwbEncode();
246
                cwbEn.setDebug(debug);
247
                CwbMakeAll cwbMa = new CwbMakeAll();
248
                cwbMa.setDebug(debug);
249
                
250
                List<String> pAttributesList = cqpbuilder.getpAttributs();
251
                List<String> sAttributesList = cqpbuilder.getsAttributs();
252
                println "pAttrs : "+pAttributesList
253
                println "sAttrs : "+sAttributesList
254
                String[] pAttributes = pAttributesList.toArray(new String[pAttributesList.size()])
255
                String[] sAttributes = sAttributesList.toArray(new String[sAttributesList.size()])
256
                
257
                try {
258
                        String regPath = rootDir + "/registry/"+corpusname.toLowerCase(); // CQP wants lowercase registry name
259
                        cwbEn.run(rootDir + "/data/${corpusname}", rootDir + "/cqp/"+corpusname+".cqp", regPath,pAttributes, sAttributes);
260
                        if (!new File(regPath).exists()) {
261
                                println "The registry file was not created: $regPath. See https://groupes.renater.fr/wiki/txm-users/public/faq"
262
                                return false;
263
                        }
264
                        cwbMa.run(corpusname.toUpperCase(), rootDir + "/registry");
265
                } catch (Exception ex) {System.out.println(ex); return false;}
266

    
267
                return true;
268
        }
269
        
270
        /**
271
         * Sets the debug.
272
         */
273
        public void setDebug()
274
        {
275
                this.debug = true;
276
        }
277
        
278
        /**
279
         * The main method.
280
         *
281
         * @param args the arguments
282
         */
283
        public static void main(String[] args)
284
        {
285
                File dir = new File("~/xml/geo");
286
                def c = new compiler();
287
                c.setDebug();
288
                c.setCwbPath("~/TXM/cwb/bin");
289
                c.run(dir,"geo");
290
        }
291
}