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 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.hyperbase;
29 321 mdecorde
30 1000 mdecorde
import org.txm.importer.cwb.CwbEncode
31 1000 mdecorde
import org.txm.importer.cwb.CwbMakeAll
32 986 mdecorde
import org.txm.scripts.importer.*;
33 321 mdecorde
import org.txm.scripts.*;
34 1000 mdecorde
import org.txm.importer.scripts.xmltxm.*;
35 321 mdecorde
import org.txm.utils.treetagger.TreeTagger;
36 1115 mdecorde
import org.txm.objects.*
37 1115 mdecorde
import org.txm.searchengine.cqp.corpus.*
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 321 mdecorde
44 321 mdecorde
// TODO: Auto-generated Javadoc
45 321 mdecorde
/**
46 321 mdecorde
 * The Class compiler.
47 321 mdecorde
 */
48 321 mdecorde
class compiler
49 321 mdecorde
{
50 321 mdecorde
51 321 mdecorde
        /** The debug. */
52 321 mdecorde
        private boolean debug= false;
53 321 mdecorde
54 321 mdecorde
        /** The input data. */
55 321 mdecorde
        private def inputData;
56 321 mdecorde
57 321 mdecorde
        /** The factory. */
58 321 mdecorde
        private def factory;
59 321 mdecorde
60 321 mdecorde
        /** The parser. */
61 321 mdecorde
        private XMLStreamReader parser;
62 321 mdecorde
63 321 mdecorde
        /** The dir. */
64 321 mdecorde
        private def dir;
65 321 mdecorde
66 321 mdecorde
        /** The output. */
67 321 mdecorde
        private def output;
68 321 mdecorde
69 321 mdecorde
        /** The url. */
70 321 mdecorde
        private def url;
71 321 mdecorde
72 321 mdecorde
        /** The anahash. */
73 321 mdecorde
        private HashMap<String,String> anahash = new HashMap<String,String>() ;
74 321 mdecorde
75 321 mdecorde
        /** The text. */
76 321 mdecorde
        String text="";
77 321 mdecorde
78 321 mdecorde
        /** The base. */
79 321 mdecorde
        String base="";
80 321 mdecorde
81 321 mdecorde
        /** The text attributes. */
82 321 mdecorde
        String[] textAttributes = null;
83 321 mdecorde
84 321 mdecorde
        /** The lang. */
85 321 mdecorde
        private String lang ="fr";
86 321 mdecorde
87 321 mdecorde
        /**
88 321 mdecorde
         * initialize.
89 321 mdecorde
         *
90 321 mdecorde
         */
91 321 mdecorde
        public compiler(){}
92 321 mdecorde
93 321 mdecorde
        /**
94 321 mdecorde
         * set the language of the corpus.
95 321 mdecorde
         *
96 321 mdecorde
         * @param lang the lang
97 321 mdecorde
         * @return the java.lang. object
98 321 mdecorde
         */
99 321 mdecorde
        public setLang(String lang)
100 321 mdecorde
        {
101 321 mdecorde
                this.lang = lang;
102 321 mdecorde
        }
103 321 mdecorde
104 321 mdecorde
        /** The annotation success. */
105 321 mdecorde
        boolean annotationSuccess = false;
106 321 mdecorde
107 321 mdecorde
        /**
108 321 mdecorde
         * Sets the annotation success.
109 321 mdecorde
         *
110 321 mdecorde
         * @param value the value
111 321 mdecorde
         * @return the java.lang. object
112 321 mdecorde
         */
113 321 mdecorde
        public setAnnotationSuccess(boolean value)
114 321 mdecorde
        {
115 321 mdecorde
                this.annotationSuccess = value
116 321 mdecorde
        }
117 321 mdecorde
118 321 mdecorde
        /**
119 321 mdecorde
         * Creates the output.
120 321 mdecorde
         *
121 321 mdecorde
         * @param f the f
122 321 mdecorde
         * @return true, if successful
123 321 mdecorde
         */
124 321 mdecorde
        private boolean createOutput(File f){
125 321 mdecorde
                try {
126 321 mdecorde
                        output = new OutputStreamWriter(new FileOutputStream(f,f.exists()) , "UTF-8");
127 321 mdecorde
                        return true;
128 321 mdecorde
                } catch (Exception e) {
129 321 mdecorde
                        System.err.println(e);
130 321 mdecorde
131 321 mdecorde
                        return false;
132 321 mdecorde
                }
133 321 mdecorde
        }
134 321 mdecorde
135 321 mdecorde
        /**
136 321 mdecorde
         * Go to text.
137 321 mdecorde
         */
138 321 mdecorde
        private void GoToText()
139 321 mdecorde
        {
140 321 mdecorde
                for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next())
141 321 mdecorde
                {
142 321 mdecorde
                        if(event == XMLStreamConstants.END_ELEMENT)
143 321 mdecorde
                                if(parser.getLocalName().equals("teiHeader"))
144 321 mdecorde
                                        return;
145 321 mdecorde
                }
146 321 mdecorde
        }
147 321 mdecorde
148 321 mdecorde
        /**
149 321 mdecorde
         * Run.
150 321 mdecorde
         *
151 321 mdecorde
         * @param rootDirFile the root dir file
152 321 mdecorde
         * @param basename the basename
153 321 mdecorde
         * @return true, if successful
154 321 mdecorde
         */
155 1115 mdecorde
        public boolean run(Project project, File binDir, File txmDir, String corpusname)
156 321 mdecorde
        {
157 321 mdecorde
                String rootDir = binDir.getAbsolutePath();
158 321 mdecorde
159 714 mdecorde
                if (!(CwbEncode.isExecutableAvailable() && CwbMakeAll.isExecutableAvailable())) {
160 714 mdecorde
                        println ("Error: CWB executables not well set.")
161 321 mdecorde
                        return false;
162 321 mdecorde
                }
163 1115 mdecorde
                CorpusBuild corpus = project.getCorpusBuild(project.getName());
164 1115 mdecorde
                if (corpus != null) {
165 1115 mdecorde
                        //println "CLEAN PREVIOUS CORPUS"
166 1115 mdecorde
                        corpus.delete(); // remove old files
167 321 mdecorde
                }
168 1115 mdecorde
169 1115 mdecorde
                // make new one
170 1115 mdecorde
                corpus = new MainCorpus(project);
171 1115 mdecorde
                corpus.setID(project.getName());
172 1115 mdecorde
                corpus.setName(project.getName());
173 1615 mdecorde
                corpus.setDescription("Built with the Hyperbase import module");
174 1115 mdecorde
175 803 mdecorde
                File cqpFile = new File(binDir,"cqp/"+corpusname+".cqp");
176 1395 mdecorde
cqpFile.delete()
177 1115 mdecorde
                new File(binDir,"cqp").mkdirs()
178 1115 mdecorde
                new File(binDir,"data").mkdirs()
179 1115 mdecorde
                new File(binDir,"registry").mkdirs()
180 321 mdecorde
181 321 mdecorde
                //start corpus
182 803 mdecorde
                if (createOutput(cqpFile)) {
183 321 mdecorde
                        output.write("<txmcorpus lang=\""+lang+"\">\n");
184 321 mdecorde
                        output.close();
185 321 mdecorde
                }
186 321 mdecorde
187 321 mdecorde
                String textid="";
188 321 mdecorde
                int counttext =0;
189 321 mdecorde
                List<File> files = txmDir.listFiles();
190 321 mdecorde
                Collections.sort(files);
191 804 mdecorde
                //1- Transform into CQP file
192 804 mdecorde
                XMLTXM2CQP cqpbuilder = null;
193 321 mdecorde
                println("Compiling "+files.size()+" files")
194 321 mdecorde
                for (File f : files) {
195 321 mdecorde
                        print "."
196 321 mdecorde
                        counttext++;
197 321 mdecorde
                        if (!f.exists()) {
198 321 mdecorde
                                println("file "+f+ " does not exists")
199 321 mdecorde
                        }
200 321 mdecorde
                        else {
201 804 mdecorde
                                cqpbuilder = new XMLTXM2CQP(f.toURI().toURL());
202 321 mdecorde
                                String txtname = f.getName().substring(0,f.getName().length()-4);
203 803 mdecorde
                                cqpbuilder.setTextInfo(txtname, corpusname, "project");
204 321 mdecorde
205 803 mdecorde
                                cqpbuilder.setBalisesToKeep(["text","p","s"]);
206 803 mdecorde
                                cqpbuilder.setSendToPAttributes(["s":["n"]]);
207 803 mdecorde
                                cqpbuilder.setLang(lang);
208 803 mdecorde
                                if (!cqpbuilder.transformFile(cqpFile)) {
209 321 mdecorde
                                        println("Failed to compile "+f)
210 321 mdecorde
                                }
211 321 mdecorde
                        }
212 321 mdecorde
                }
213 321 mdecorde
                println ""
214 321 mdecorde
215 321 mdecorde
                //end corpus
216 803 mdecorde
                if (createOutput(cqpFile)) {
217 321 mdecorde
                        output.write("</txmcorpus>\n");
218 321 mdecorde
                        output.close();
219 321 mdecorde
                }
220 321 mdecorde
221 803 mdecorde
                if (cqpbuilder == null) return false;
222 321 mdecorde
223 321 mdecorde
                //2- Import into CWB
224 321 mdecorde
                def outDir =rootDir;
225 321 mdecorde
226 321 mdecorde
                CwbEncode cwbEn = new CwbEncode();
227 321 mdecorde
                cwbEn.setDebug(debug);
228 321 mdecorde
                CwbMakeAll cwbMa = new CwbMakeAll();
229 321 mdecorde
                cwbMa.setDebug(debug);
230 321 mdecorde
231 803 mdecorde
                List<String> pAttributesList = cqpbuilder.getpAttributs();
232 803 mdecorde
                List<String> sAttributesList = cqpbuilder.getsAttributs();
233 321 mdecorde
                println "word properties : "+pAttributesList
234 321 mdecorde
                println "structures : "+sAttributesList
235 321 mdecorde
                String[] pAttributes = pAttributesList.toArray(new String[pAttributesList.size()])
236 321 mdecorde
                String[] sAttributes = sAttributesList.toArray(new String[sAttributesList.size()])
237 321 mdecorde
238 321 mdecorde
                try {
239 321 mdecorde
                        String regPath = outDir + "/registry/"+corpusname.toLowerCase();
240 803 mdecorde
                        cwbEn.run(outDir + "/data/$corpusname", outDir + "/cqp/"+corpusname+".cqp", regPath,pAttributes, sAttributes);
241 321 mdecorde
                        if (!new File(regPath).exists()) {
242 321 mdecorde
                                println "Error: The registry file was not created: $regPath. See https://groupes.renater.fr/wiki/txm-users/public/faq"
243 321 mdecorde
                                return false;
244 321 mdecorde
                        }
245 714 mdecorde
                        cwbMa.run(corpusname, outDir + "/registry");
246 321 mdecorde
247 321 mdecorde
                } catch (Exception ex) {System.out.println(ex); return false;}
248 321 mdecorde
249 321 mdecorde
                return true;
250 321 mdecorde
        }
251 321 mdecorde
252 321 mdecorde
        /**
253 321 mdecorde
         * Sets the debug.
254 321 mdecorde
         */
255 321 mdecorde
        public void setDebug()
256 321 mdecorde
        {
257 321 mdecorde
                this.debug = true;
258 321 mdecorde
        }
259 321 mdecorde
260 321 mdecorde
        /**
261 321 mdecorde
         * The main method.
262 321 mdecorde
         *
263 321 mdecorde
         * @param args the arguments
264 321 mdecorde
         */
265 321 mdecorde
        public static void main(String[] args)
266 321 mdecorde
        {
267 321 mdecorde
                File dir = new File("~/xml/geo");
268 321 mdecorde
                def c = new compiler();
269 321 mdecorde
                c.setDebug();
270 321 mdecorde
                c.setCwbPath("~/TXM/cwb/bin");
271 321 mdecorde
                c.run(dir,"geo");
272 321 mdecorde
        }
273 321 mdecorde
}