Statistics
| Revision:

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

History | View | Annotate | Download (6.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: 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
                CorpusBuild corpus = project.getCorpusBuild(project.getName());
155
                if (corpus != null) {
156
                        println "CLEAN PREVIOUS CORPUS"
157
                        corpus.delete(); // remove old files
158
                }
159
                        
160
                // make new one
161
                corpus = new MainCorpus(project);
162
                corpus.setID(project.getName());
163
                corpus.setName(project.getName());
164
                corpus.setDescription("Build the Alceste import module");
165

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

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

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