Statistics
| Revision:

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

History | View | Annotate | Download (6.4 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.discours
29

    
30
import org.txm.importer.cwb.BuildCwbEncodeArgsFromTEITXM;
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.objects.*
38
import javax.xml.stream.*;
39
import java.net.URL;
40
import java.io.File;
41
import java.util.HashMap;
42
import java.util.List;
43
import org.txm.searchengine.cqp.corpus.*
44

    
45
/**
46
 * The Class compiler.
47
 */
48
class compiler {
49

    
50
        /** The input data. */
51
        private def inputData;
52

    
53
        /** The factory. */
54
        private def factory;
55

    
56
        /** The parser. */
57
        private XMLStreamReader parser;
58

    
59
        /** The dir. */
60
        private def dir;
61

    
62
        /** The output. */
63
        private def output;
64

    
65
        /** The url. */
66
        private def url;
67

    
68
        /** The anahash. */
69
        private HashMap<String,String> anahash = new HashMap<String,String>() ;
70

    
71
        /** The text. */
72
        String text="";
73

    
74
        /** The base. */
75
        String base="";
76

    
77
        /** The lang. */
78
        private String lang ="fr";
79

    
80
        /** The text attributes. */
81
        static String textAttributes = "";
82

    
83
        /**
84
         * initialize.
85
         *
86
         */
87

    
88
        public compiler(){
89
        }
90

    
91
        /**
92
         * set the language of the corpus.
93
         *
94
         * @param lang the lang
95
         * @return the java.lang. object
96
         */
97
        public setLang(String lang)
98
        {
99
                this.lang = lang;
100
        }
101

    
102
        /**
103
         * Creates the output.
104
         *
105
         * @param f the f
106
         * @return true, if successful
107
         */
108
        private boolean createOutput(File f){
109
                try {
110
                        output = new OutputStreamWriter(new FileOutputStream(f,f.exists()) , "UTF-8");
111
                        return true;
112
                } catch (Exception e) {
113
                        System.err.println(e);
114
                        return false;
115
                }
116
        }
117

    
118
        /**
119
         * Go to text.
120
         */
121
        private void GoToText() {
122
                for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
123
                        if(event == XMLStreamConstants.END_ELEMENT)
124
                                if(parser.getLocalName().equals("teiHeader"))
125
                                        return;
126
                }
127
        }
128

    
129
        /** The debug. */
130
        boolean debug = false;
131

    
132
        /**
133
         * Sets the debug.
134
         */
135
        public void setDebug()
136
        {
137
                debug = true;
138
        }
139

    
140
        /**
141
         * Run.
142
         *
143
         * @param files the files
144
         * @param basename the basename
145
         * @return true, if successful
146
         */
147
        public boolean run(Project project, List<File> files, File binDir, File txmDir, String corpusname) {
148
                String binDirPath = binDir.getAbsolutePath()+"/";
149

    
150
                if (!(CwbEncode.isExecutableAvailable() && CwbMakeAll.isExecutableAvailable())) {
151
                        println ("Error: CWB executables not well set.")
152
                        return false;
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("Built with the CNR+CSV 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
                //start corpus
173
                if (createOutput(cqpFile)) {
174
                        output.write("<txmcorpus lang=\""+lang+"\">\n");
175
                        output.close();
176
                }
177

    
178
                def second = 0
179
                //1- Transform into CQP file
180

    
181
                Collections.sort(files);
182
                XMLTXM2CQP cqpbuilder = null;
183
                for (File f : files) {
184
                        if (second) { print(", ") }
185
                        if (second > 0 && (second % 5) == 0) println ""
186
                        print(f.getName().replaceFirst("\\.xml", ""));
187
                        second++
188

    
189
                        cqpbuilder = new XMLTXM2CQP(f.toURI().toURL());
190
                        String txtname = f.getName().substring(0,f.getName().length()-4);
191
                        cqpbuilder.setTextInfo(txtname, corpusname, "project");
192

    
193
                        cqpbuilder.setBalisesToKeep(["text","p","s"]);
194
                        cqpbuilder.setSendToPAttributes(["s":["id"], "p":["id"]]);
195
                        cqpbuilder.setLang(lang);
196
                        if (!cqpbuilder.transformFile(cqpFile)) {
197
                                println("Failed to compile "+f)
198
                        }
199

    
200
                }
201

    
202
                //end corpus
203
                if (createOutput(cqpFile)) {
204
                        output.write("</txmcorpus>\n");
205
                        output.close();
206
                }
207

    
208
                if (cqpbuilder == null) {
209
                        println "there was no files to process: "+files
210
                        return false;
211
                }
212

    
213
                //2- Import into CWB
214
                def outDir = binDirPath;
215

    
216
                CwbEncode cwbEn = new CwbEncode();
217
                CwbMakeAll cwbMa = new CwbMakeAll();
218

    
219
                List<String> pAttributesList = cqpbuilder.getpAttributs();
220
                List<String> sAttributesList = cqpbuilder.getsAttributs();
221
                println "pAttrs : "+pAttributesList
222
                println "sAttrs : "+sAttributesList
223
                String[] pAttributes = pAttributesList.toArray(new String[pAttributesList.size()])
224
                String[] sAttributes = sAttributesList.toArray(new String[sAttributesList.size()])
225

    
226
                try {
227
                        //println "Indexing "+compiler.textAttributes
228
                        cwbEn.setDebug(debug);
229
                        String regPath = outDir + "registry/"+corpusname.toLowerCase()
230
                        cwbEn.run(
231
                                        outDir + "data/$corpusname",
232
                                        outDir + "/cqp/"+corpusname+".cqp",
233
                                        regPath,
234
                                        pAttributes,
235
                                        sAttributes);
236
                        if (!new File(regPath).exists()) {
237
                                println "Error: The registry file was not created: $regPath. See https://groupes.renater.fr/wiki/txm-users/public/faq"
238
                                return false;
239
                        }
240
                        //println "Binding all indexes"
241
                        cwbMa.setDebug(debug);
242
                        cwbMa.run(corpusname, outDir + "registry");
243

    
244
                } catch (Exception ex) { System.err.println(ex); return false;}
245

    
246
                return true;
247
        }
248

    
249
        /**
250
         * The main method.
251
         *
252
         * @param args the arguments
253
         */
254
        public static void main(String[] args) {
255
                File dir = new File(System.getProperty("user.home"),"xml/discours/txm/");
256
                List<File> files = dir.listFiles();
257
                def c = new compiler()
258
                c.setCwbLoc(System.getProperty("user.home")+"/TXM/cwb/bin/")
259
                if (!c.run(files,"discours")) {
260
                        println "Compiler failed"
261
                        return;
262
                }
263
        }
264
}