Statistics
| Revision:

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

History | View | Annotate | Download (6.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.objects.*
31
import org.txm.importer.cwb.CwbAlign;
32
import org.txm.importer.cwb.CwbEncode
33
import org.txm.importer.cwb.CwbMakeAll
34
import org.txm.scripts.importer.*;
35
import org.txm.scripts.*;
36
import org.txm.importer.scripts.xmltxm.*;
37
import org.txm.utils.treetagger.TreeTagger;
38
import org.txm.core.preferences.TXMPreferences
39
import org.txm.libs.cqp.CQPLibPreferences
40
import org.txm.searchengine.cqp.corpus.*
41
import javax.xml.stream.*;
42

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

    
49
// TODO: Auto-generated Javadoc
50
/**
51
 * build cqp
52
 * build CWB indexes
53
 * create registry file.
54
 *
55
 * @author mdecorde
56
 */
57
class compiler {
58
        /** The debug. */
59
        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 lang. */
89
        String lang = "fr";
90
        
91
        /**
92
         * initialize.
93
         *
94
         */
95
        public compiler(){}
96
        
97
        /** The annotation success. */
98
        boolean annotationSuccess = false;
99
        
100
        /**
101
         * Sets the annotation success.
102
         *
103
         * @param val the new annotation success
104
         */
105
        public void setAnnotationSuccess(boolean val)
106
        {
107
                this.annotationSuccess = val;
108
        }
109
        
110
        /**
111
         * Creates the output.
112
         *
113
         * @param f the f
114
         * @return true, if successful
115
         */
116
        private boolean createOutput(File f){
117
                try {
118
                        output = new OutputStreamWriter(new FileOutputStream(f,f.exists()) , "UTF-8");
119
                        return true;
120
                } catch (Exception e) {
121
                        System.err.println(e);
122
                        return false;
123
                }
124
        }
125
        
126
        /**
127
         * Sets the lang.
128
         *
129
         * @param lang the lang
130
         * @return the java.lang. object
131
         */
132
        public setLang(String lang)
133
        {
134
                this.lang = lang;
135
        }
136
        
137
        /**
138
         * Run.
139
         *
140
         * @param rootDirFile the root dir file
141
         * @param basename the basename
142
         * @return true, if successful
143
         */
144
        public boolean run(Project project) 
145
        {
146
                File binDir = project.getProjectDirectory()
147
                String corpusname = project.getName();
148
                File txmDir = new File(binDir, "txm/"+corpusname)
149
                if (!(CwbEncode.isExecutableAvailable() && CwbMakeAll.isExecutableAvailable())) {
150
                        println ("Error: CWB executables not well set.")
151
                        return false;
152
                }
153
                
154
                
155
                CorpusBuild corpus = project.getCorpusBuild(project.getName(), MainCorpus.class);
156
                if (corpus != null) {
157
                        if (project.getDoUpdate()) {
158
                                corpus.clean(); // remove old files
159
                        } else {
160
                                corpus.delete(); // remove old files and TXMResult children
161
                        }
162
                } else {
163
                        corpus = new MainCorpus(project);
164
                        corpus.setID(project.getName());
165
                        corpus.setName(project.getName());
166
                }
167
                corpus.setDescription("Build the Alceste import module");
168

    
169
                File cqpFile = new File(binDir,"cqp/"+corpusname+".cqp");
170
cqpFile.delete()
171
                new File(binDir,"cqp").mkdirs()
172
                new File(binDir,"data").mkdirs()
173
                new File(binDir,"registry").mkdirs()
174
                
175
                String textid = "";
176
                int counttext = 0;
177
                List<File> files = txmDir.listFiles();
178
                if (files == null || files.size() == 0) {
179
                        println "no file to compile"
180
                        return false;
181
                }
182
                
183
                //start corpus
184
                if (createOutput(cqpFile)) {
185
                        output.write("<txmcorpus lang=\""+lang+"\">\n");
186
                        output.close();
187
                }
188
                
189
                Collections.sort(files);
190
                //1- Transform into CQP file
191
                XMLTXM2CQP cqpbuilder = null;
192
                println("process "+files.size()+" files")
193
                for (File f : files) {
194
                        print "."
195
                        counttext++;
196
                        if (!f.exists()) {
197
                                println("file "+f+ " does not exists")        
198
                        } else {                        
199
                                cqpbuilder = new XMLTXM2CQP(f.toURI().toURL());
200
                                String txtname = f.getName().substring(0, f.getName().length()-4);
201
                                cqpbuilder.setTextInfo(txtname, corpusname, "default");
202

    
203
                                cqpbuilder.setBalisesToKeep(["text", "s", "p", "lb"]);
204
                                cqpbuilder.setSendToPAttributes(["lb":["n"], "p":["n"], "s":["n"]])
205
                                cqpbuilder.setLang(lang);
206
                                if (!cqpbuilder.transformFile(cqpFile)) {
207
                                        println("Failed to compile "+f)
208
                                }
209
                        }
210
                }
211
                println ""
212
                
213
                //end corpus
214
                if (createOutput(cqpFile)) {
215
                        output.write("</txmcorpus>\n");
216
                        output.close();
217
                }
218
                
219
                if (cqpbuilder == null) {
220
                        println "there was no files in bin dir : "+txmDir
221
                        return false;
222
                }
223
                
224
                //2- Import into CWB
225
                def outDir = binDir.getAbsolutePath();
226
                CwbEncode cwbEn = new CwbEncode();
227
                cwbEn.setDebug(debug);
228
                CwbMakeAll cwbMa = new CwbMakeAll();
229
                cwbMa.setDebug(debug);
230
                
231
                List<String> pAttributesList = cqpbuilder.getpAttributs();
232
                List<String> sAttributesList = cqpbuilder.getsAttributs();
233
                println "pAttrs : "+pAttributesList
234
                println "sAttrs : "+sAttributesList
235
                String[] pAttributes = pAttributesList.toArray(new String[pAttributesList.size()])
236
                String[] sAttributes = sAttributesList.toArray(new String[sAttributesList.size()])
237
                
238
                try {
239
                        String regPath = outDir + "/registry/"+corpusname.toLowerCase(); // CQP wants lowercase registry name
240
                        cwbEn.run(outDir + "/data/${corpusname}", outDir + "/cqp/"+corpusname+".cqp", regPath,pAttributes, sAttributes);
241
                        if (!new File(regPath).exists()) {
242
                                println "Error: The registry file was not created: $regPath. See https://groupes.renater.fr/wiki/txm-users/public/faq"
243
                                return false;
244
                        }
245
                        cwbMa.run(corpusname.toUpperCase(), outDir + "/registry");
246
                        
247
                } catch (Exception ex) {System.out.println(ex); return false;}
248

    
249
                return true;
250
        }
251
        
252
        /**
253
         * Sets the debug.
254
         */
255
        public void setDebug()
256
        {
257
                this.debug = true;
258
        }
259
        
260
        /**
261
         * The main method.
262
         *
263
         * @param args the arguments
264
         */
265
        public static void main(String[] args)
266
        {
267
                File dir = new File("~/xml/geo");
268
                def c = new compiler();
269
                c.setDebug();
270
                c.setCwbPath("~/TXM/cwb/bin");
271
                c.run(dir,"geo");
272
        }
273
}