Statistics
| Revision:

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

History | View | Annotate | Download (6.5 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.hyperbase;
29

    
30
import org.txm.importer.cwb.CwbEncode
31
import org.txm.importer.cwb.CwbMakeAll
32
import org.txm.scripts.importer.*;
33
import org.txm.scripts.*;
34
import org.txm.importer.scripts.xmltxm.*;
35
import org.txm.utils.treetagger.TreeTagger;
36
import org.txm.objects.*
37
import org.txm.searchengine.cqp.corpus.*
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

    
44
// TODO: Auto-generated Javadoc
45
/**
46
 * The Class compiler.
47
 */
48
class compiler 
49
{
50
        
51
        /** The debug. */
52
        private boolean debug= false;
53
        
54
        /** The input data. */
55
        private def inputData;
56
        
57
        /** The factory. */
58
        private def factory;
59
        
60
        /** The parser. */
61
        private XMLStreamReader parser;
62
        
63
        /** The dir. */
64
        private def dir;
65
        
66
        /** The output. */
67
        private def output;
68
        
69
        /** The url. */
70
        private def url;
71
        
72
        /** The anahash. */
73
        private HashMap<String,String> anahash = new HashMap<String,String>() ;
74
        
75
        /** The text. */
76
        String text="";
77
        
78
        /** The base. */
79
        String base="";
80
        
81
        /** The text attributes. */
82
        String[] textAttributes = null;
83
        
84
        /** The lang. */
85
        private String lang ="fr";
86
        
87
        /**
88
         * initialize.
89
         *
90
         */
91
        public compiler(){}
92
        
93
        /**
94
         * set the language of the corpus.
95
         *
96
         * @param lang the lang
97
         * @return the java.lang. object
98
         */
99
        public setLang(String lang)
100
        {
101
                this.lang = lang;
102
        }
103
        
104
        /** The annotation success. */
105
        boolean annotationSuccess = false;
106
        
107
        /**
108
         * Sets the annotation success.
109
         *
110
         * @param value the value
111
         * @return the java.lang. object
112
         */
113
        public setAnnotationSuccess(boolean value)
114
        {
115
                this.annotationSuccess = value
116
        }
117
        
118
        /**
119
         * Creates the output.
120
         *
121
         * @param f the f
122
         * @return true, if successful
123
         */
124
        private boolean createOutput(File f){
125
                try {
126
                        output = new OutputStreamWriter(new FileOutputStream(f,f.exists()) , "UTF-8");
127
                        return true;
128
                } catch (Exception e) {
129
                        System.err.println(e);
130
                        
131
                        return false;
132
                }
133
        }
134
        
135
        /**
136
         * Go to text.
137
         */
138
        private void GoToText()
139
        {
140
                for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) 
141
                {
142
                        if(event == XMLStreamConstants.END_ELEMENT)
143
                                if(parser.getLocalName().equals("teiHeader"))
144
                                        return;
145
                }
146
        }
147
        
148
        /**
149
         * Run.
150
         *
151
         * @param rootDirFile the root dir file
152
         * @param basename the basename
153
         * @return true, if successful
154
         */
155
        public boolean run(Project project, File binDir, File txmDir, String corpusname) 
156
        {
157
                String rootDir = binDir.getAbsolutePath();
158
                
159
                if (!(CwbEncode.isExecutableAvailable() && CwbMakeAll.isExecutableAvailable())) {
160
                        println ("Error: CWB executables not well set.")
161
                        return false;
162
                }
163
                CorpusBuild corpus = project.getCorpusBuild(project.getName());
164
                if (corpus != null) {
165
                        //println "CLEAN PREVIOUS CORPUS"
166
                        corpus.delete(); // remove old files
167
                }
168
                        
169
                // make new one
170
                corpus = new MainCorpus(project);
171
                corpus.setID(project.getName());
172
                corpus.setName(project.getName());
173
                corpus.setDescription("Built with the Hyperbase import module");
174
                
175
                File cqpFile = new File(binDir,"cqp/"+corpusname+".cqp");
176
cqpFile.delete()
177
                new File(binDir,"cqp").mkdirs()
178
                new File(binDir,"data").mkdirs()
179
                new File(binDir,"registry").mkdirs()
180
                
181
                //start corpus
182
                if (createOutput(cqpFile)) {
183
                        output.write("<txmcorpus lang=\""+lang+"\">\n");
184
                        output.close();
185
                }
186
                
187
                String textid="";
188
                int counttext =0;
189
                List<File> files = txmDir.listFiles();
190
                Collections.sort(files);
191
                //1- Transform into CQP file
192
                XMLTXM2CQP cqpbuilder = null;
193
                println("Compiling "+files.size()+" files")
194
                for (File f : files) {
195
                        print "."
196
                        counttext++;
197
                        if (!f.exists()) {
198
                                println("file "+f+ " does not exists")        
199
                        }
200
                        else {        
201
                                cqpbuilder = new XMLTXM2CQP(f.toURI().toURL());
202
                                String txtname = f.getName().substring(0,f.getName().length()-4);
203
                                cqpbuilder.setTextInfo(txtname, corpusname, "project");
204

    
205
                                cqpbuilder.setBalisesToKeep(["text","p","s"]);
206
                                cqpbuilder.setSendToPAttributes(["s":["n"]]);
207
                                cqpbuilder.setLang(lang);
208
                                if (!cqpbuilder.transformFile(cqpFile)) {
209
                                        println("Failed to compile "+f)
210
                                }
211
                        }
212
                }
213
                println ""
214
                
215
                //end corpus
216
                if (createOutput(cqpFile)) {
217
                        output.write("</txmcorpus>\n");
218
                        output.close();
219
                }
220
                
221
                if (cqpbuilder == null) return false;
222
                
223
                //2- Import into CWB
224
                def outDir =rootDir;
225
                
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 "word properties : "+pAttributesList
234
                println "structures : "+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();
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, 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
}