Statistics
| Revision:

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

History | View | Annotate | Download (6.5 kB)

1 321 mdecorde
// Copyright © 2010-2013 ENS de Lyon.
2 321 mdecorde
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3 321 mdecorde
// Lyon 2, University of Franche-Comté, University of Nice
4 321 mdecorde
// Sophia Antipolis, University of Paris 3.
5 321 mdecorde
//
6 321 mdecorde
// The TXM platform is free software: you can redistribute it
7 321 mdecorde
// and/or modify it under the terms of the GNU General Public
8 321 mdecorde
// License as published by the Free Software Foundation,
9 321 mdecorde
// either version 2 of the License, or (at your option) any
10 321 mdecorde
// later version.
11 321 mdecorde
//
12 321 mdecorde
// The TXM platform is distributed in the hope that it will be
13 321 mdecorde
// useful, but WITHOUT ANY WARRANTY; without even the implied
14 321 mdecorde
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 321 mdecorde
// PURPOSE. See the GNU General Public License for more
16 321 mdecorde
// details.
17 321 mdecorde
//
18 321 mdecorde
// You should have received a copy of the GNU General
19 321 mdecorde
// Public License along with the TXM platform. If not, see
20 321 mdecorde
// http://www.gnu.org/licenses.
21 321 mdecorde
//
22 321 mdecorde
//
23 321 mdecorde
//
24 479 mdecorde
// $LastChangedDate: 2016-05-26 17:42:36 +0200 (jeu. 26 mai 2016) $
25 321 mdecorde
// $LastChangedRevision: 3219 $
26 321 mdecorde
// $LastChangedBy: mdecorde $
27 321 mdecorde
//
28 986 mdecorde
package org.txm.scripts.importer.discours
29 321 mdecorde
30 1000 mdecorde
import org.txm.importer.cwb.BuildCwbEncodeArgsFromTEITXM;
31 1000 mdecorde
import org.txm.importer.cwb.CwbEncode
32 1000 mdecorde
import org.txm.importer.cwb.CwbMakeAll
33 986 mdecorde
import org.txm.scripts.importer.*;
34 321 mdecorde
import org.txm.scripts.*;
35 1000 mdecorde
import org.txm.importer.scripts.xmltxm.*;
36 321 mdecorde
import org.txm.utils.treetagger.TreeTagger;
37 1115 mdecorde
import org.txm.objects.*
38 321 mdecorde
import javax.xml.stream.*;
39 321 mdecorde
import java.net.URL;
40 321 mdecorde
import java.io.File;
41 321 mdecorde
import java.util.HashMap;
42 321 mdecorde
import java.util.List;
43 1115 mdecorde
import org.txm.searchengine.cqp.corpus.*
44 321 mdecorde
45 321 mdecorde
/**
46 321 mdecorde
 * The Class compiler.
47 321 mdecorde
 */
48 321 mdecorde
class compiler {
49 321 mdecorde
50 321 mdecorde
        /** The input data. */
51 321 mdecorde
        private def inputData;
52 321 mdecorde
53 321 mdecorde
        /** The factory. */
54 321 mdecorde
        private def factory;
55 321 mdecorde
56 321 mdecorde
        /** The parser. */
57 321 mdecorde
        private XMLStreamReader parser;
58 321 mdecorde
59 321 mdecorde
        /** The dir. */
60 321 mdecorde
        private def dir;
61 321 mdecorde
62 321 mdecorde
        /** The output. */
63 321 mdecorde
        private def output;
64 321 mdecorde
65 321 mdecorde
        /** The url. */
66 321 mdecorde
        private def url;
67 321 mdecorde
68 321 mdecorde
        /** The anahash. */
69 321 mdecorde
        private HashMap<String,String> anahash = new HashMap<String,String>() ;
70 321 mdecorde
71 321 mdecorde
        /** The text. */
72 321 mdecorde
        String text="";
73 321 mdecorde
74 321 mdecorde
        /** The base. */
75 321 mdecorde
        String base="";
76 321 mdecorde
77 321 mdecorde
        /** The lang. */
78 321 mdecorde
        private String lang ="fr";
79 321 mdecorde
80 321 mdecorde
        /** The text attributes. */
81 321 mdecorde
        static String textAttributes = "";
82 321 mdecorde
83 321 mdecorde
        /**
84 321 mdecorde
         * initialize.
85 321 mdecorde
         *
86 321 mdecorde
         */
87 321 mdecorde
88 321 mdecorde
        public compiler(){
89 321 mdecorde
        }
90 321 mdecorde
91 321 mdecorde
        /**
92 321 mdecorde
         * set the language of the corpus.
93 321 mdecorde
         *
94 321 mdecorde
         * @param lang the lang
95 321 mdecorde
         * @return the java.lang. object
96 321 mdecorde
         */
97 321 mdecorde
        public setLang(String lang)
98 321 mdecorde
        {
99 321 mdecorde
                this.lang = lang;
100 321 mdecorde
        }
101 321 mdecorde
102 321 mdecorde
        /**
103 321 mdecorde
         * Creates the output.
104 321 mdecorde
         *
105 321 mdecorde
         * @param f the f
106 321 mdecorde
         * @return true, if successful
107 321 mdecorde
         */
108 321 mdecorde
        private boolean createOutput(File f){
109 321 mdecorde
                try {
110 321 mdecorde
                        output = new OutputStreamWriter(new FileOutputStream(f,f.exists()) , "UTF-8");
111 321 mdecorde
                        return true;
112 321 mdecorde
                } catch (Exception e) {
113 321 mdecorde
                        System.err.println(e);
114 321 mdecorde
                        return false;
115 321 mdecorde
                }
116 321 mdecorde
        }
117 321 mdecorde
118 321 mdecorde
        /**
119 321 mdecorde
         * Go to text.
120 321 mdecorde
         */
121 321 mdecorde
        private void GoToText() {
122 321 mdecorde
                for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
123 321 mdecorde
                        if(event == XMLStreamConstants.END_ELEMENT)
124 321 mdecorde
                                if(parser.getLocalName().equals("teiHeader"))
125 321 mdecorde
                                        return;
126 321 mdecorde
                }
127 321 mdecorde
        }
128 321 mdecorde
129 321 mdecorde
        /** The debug. */
130 321 mdecorde
        boolean debug = false;
131 321 mdecorde
132 321 mdecorde
        /**
133 321 mdecorde
         * Sets the debug.
134 321 mdecorde
         */
135 321 mdecorde
        public void setDebug()
136 321 mdecorde
        {
137 321 mdecorde
                debug = true;
138 321 mdecorde
        }
139 321 mdecorde
140 321 mdecorde
        /**
141 321 mdecorde
         * Run.
142 321 mdecorde
         *
143 321 mdecorde
         * @param files the files
144 321 mdecorde
         * @param basename the basename
145 321 mdecorde
         * @return true, if successful
146 321 mdecorde
         */
147 1115 mdecorde
        public boolean run(Project project, List<File> files, File binDir, File txmDir, String corpusname) {
148 321 mdecorde
                String binDirPath = binDir.getAbsolutePath()+"/";
149 321 mdecorde
150 714 mdecorde
                if (!(CwbEncode.isExecutableAvailable() && CwbMakeAll.isExecutableAvailable())) {
151 714 mdecorde
                        println ("Error: CWB executables not well set.")
152 321 mdecorde
                        return false;
153 321 mdecorde
                }
154 1804 mdecorde
                CorpusBuild corpus = project.getCorpusBuild(project.getName(), MainCorpus.class);
155 1115 mdecorde
                if (corpus != null) {
156 1804 mdecorde
                        if (project.getDoUpdate()) {
157 1804 mdecorde
                                corpus.clean(); // remove old files
158 1804 mdecorde
                        } else {
159 1804 mdecorde
                                corpus.delete(); // remove old files and TXMResult children
160 1804 mdecorde
                        }
161 1804 mdecorde
                } else {
162 1804 mdecorde
                        corpus = new MainCorpus(project);
163 1804 mdecorde
                        corpus.setID(project.getName());
164 1804 mdecorde
                        corpus.setName(project.getName());
165 1115 mdecorde
                }
166 1615 mdecorde
                corpus.setDescription("Built with the CNR+CSV import module");
167 1115 mdecorde
168 1115 mdecorde
                File cqpFile = new File(binDir,"cqp/"+corpusname+".cqp");
169 1395 mdecorde
cqpFile.delete()
170 1115 mdecorde
                new File(binDir,"cqp").mkdirs()
171 1115 mdecorde
                new File(binDir,"data").mkdirs()
172 1115 mdecorde
                new File(binDir,"registry").mkdirs()
173 321 mdecorde
174 321 mdecorde
                //start corpus
175 803 mdecorde
                if (createOutput(cqpFile)) {
176 321 mdecorde
                        output.write("<txmcorpus lang=\""+lang+"\">\n");
177 321 mdecorde
                        output.close();
178 321 mdecorde
                }
179 321 mdecorde
180 321 mdecorde
                def second = 0
181 804 mdecorde
                //1- Transform into CQP file
182 321 mdecorde
183 321 mdecorde
                Collections.sort(files);
184 804 mdecorde
                XMLTXM2CQP cqpbuilder = null;
185 321 mdecorde
                for (File f : files) {
186 321 mdecorde
                        if (second) { print(", ") }
187 321 mdecorde
                        if (second > 0 && (second % 5) == 0) println ""
188 321 mdecorde
                        print(f.getName().replaceFirst("\\.xml", ""));
189 321 mdecorde
                        second++
190 321 mdecorde
191 804 mdecorde
                        cqpbuilder = new XMLTXM2CQP(f.toURI().toURL());
192 321 mdecorde
                        String txtname = f.getName().substring(0,f.getName().length()-4);
193 803 mdecorde
                        cqpbuilder.setTextInfo(txtname, corpusname, "project");
194 321 mdecorde
195 803 mdecorde
                        cqpbuilder.setBalisesToKeep(["text","p","s"]);
196 803 mdecorde
                        cqpbuilder.setSendToPAttributes(["s":["id"], "p":["id"]]);
197 803 mdecorde
                        cqpbuilder.setLang(lang);
198 803 mdecorde
                        if (!cqpbuilder.transformFile(cqpFile)) {
199 321 mdecorde
                                println("Failed to compile "+f)
200 321 mdecorde
                        }
201 321 mdecorde
202 321 mdecorde
                }
203 321 mdecorde
204 321 mdecorde
                //end corpus
205 803 mdecorde
                if (createOutput(cqpFile)) {
206 321 mdecorde
                        output.write("</txmcorpus>\n");
207 321 mdecorde
                        output.close();
208 321 mdecorde
                }
209 321 mdecorde
210 803 mdecorde
                if (cqpbuilder == null) {
211 321 mdecorde
                        println "there was no files to process: "+files
212 321 mdecorde
                        return false;
213 321 mdecorde
                }
214 321 mdecorde
215 321 mdecorde
                //2- Import into CWB
216 321 mdecorde
                def outDir = binDirPath;
217 321 mdecorde
218 321 mdecorde
                CwbEncode cwbEn = new CwbEncode();
219 321 mdecorde
                CwbMakeAll cwbMa = new CwbMakeAll();
220 321 mdecorde
221 803 mdecorde
                List<String> pAttributesList = cqpbuilder.getpAttributs();
222 803 mdecorde
                List<String> sAttributesList = cqpbuilder.getsAttributs();
223 321 mdecorde
                println "pAttrs : "+pAttributesList
224 321 mdecorde
                println "sAttrs : "+sAttributesList
225 321 mdecorde
                String[] pAttributes = pAttributesList.toArray(new String[pAttributesList.size()])
226 321 mdecorde
                String[] sAttributes = sAttributesList.toArray(new String[sAttributesList.size()])
227 321 mdecorde
228 321 mdecorde
                try {
229 321 mdecorde
                        //println "Indexing "+compiler.textAttributes
230 321 mdecorde
                        cwbEn.setDebug(debug);
231 321 mdecorde
                        String regPath = outDir + "registry/"+corpusname.toLowerCase()
232 321 mdecorde
                        cwbEn.run(
233 321 mdecorde
                                        outDir + "data/$corpusname",
234 803 mdecorde
                                        outDir + "/cqp/"+corpusname+".cqp",
235 321 mdecorde
                                        regPath,
236 321 mdecorde
                                        pAttributes,
237 321 mdecorde
                                        sAttributes);
238 321 mdecorde
                        if (!new File(regPath).exists()) {
239 321 mdecorde
                                println "Error: The registry file was not created: $regPath. See https://groupes.renater.fr/wiki/txm-users/public/faq"
240 321 mdecorde
                                return false;
241 321 mdecorde
                        }
242 321 mdecorde
                        //println "Binding all indexes"
243 321 mdecorde
                        cwbMa.setDebug(debug);
244 714 mdecorde
                        cwbMa.run(corpusname, outDir + "registry");
245 321 mdecorde
246 321 mdecorde
                } catch (Exception ex) { System.err.println(ex); return false;}
247 321 mdecorde
248 321 mdecorde
                return true;
249 321 mdecorde
        }
250 321 mdecorde
251 321 mdecorde
        /**
252 321 mdecorde
         * The main method.
253 321 mdecorde
         *
254 321 mdecorde
         * @param args the arguments
255 321 mdecorde
         */
256 321 mdecorde
        public static void main(String[] args) {
257 321 mdecorde
                File dir = new File(System.getProperty("user.home"),"xml/discours/txm/");
258 321 mdecorde
                List<File> files = dir.listFiles();
259 321 mdecorde
                def c = new compiler()
260 321 mdecorde
                c.setCwbLoc(System.getProperty("user.home")+"/TXM/cwb/bin/")
261 321 mdecorde
                if (!c.run(files,"discours")) {
262 321 mdecorde
                        println "Compiler failed"
263 321 mdecorde
                        return;
264 321 mdecorde
                }
265 321 mdecorde
        }
266 321 mdecorde
}