Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / importer / transcriber / transcriberLoader.groovy @ 479

History | View | Annotate | Download (11.4 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
// This file is part of the TXM platform.
24 321 mdecorde
//
25 321 mdecorde
// The TXM platform is free software: you can redistribute it and/or modif y
26 321 mdecorde
// it under the terms of the GNU General Public License as published by
27 321 mdecorde
// the Free Software Foundation, either version 3 of the License, or
28 321 mdecorde
// (at your option) any later version.
29 321 mdecorde
//
30 321 mdecorde
// The TXM platform is distributed in the hope that it will be useful,
31 321 mdecorde
// but WITHOUT ANY WARRANTY; without even the implied warranty of
32 321 mdecorde
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33 321 mdecorde
// GNU General Public License for more details.
34 321 mdecorde
//
35 321 mdecorde
// You should have received a copy of the GNU General Public License
36 321 mdecorde
// along with the TXM platform.  If not, see <http://www.gnu.org/licenses/>.
37 321 mdecorde
//
38 321 mdecorde
//
39 321 mdecorde
//
40 321 mdecorde
// $LastChangedDate:$
41 321 mdecorde
// $LastChangedRevision:$
42 321 mdecorde
// $LastChangedBy:$
43 321 mdecorde
//
44 321 mdecorde
package org.txm.importer.transcriber;
45 321 mdecorde
46 321 mdecorde
import java.io.File;
47 321 mdecorde
import org.txm.scripts.teitxm.*;
48 321 mdecorde
import org.txm.*;
49 321 mdecorde
import org.txm.objects.*;
50 321 mdecorde
import org.txm.utils.i18n.*;
51 321 mdecorde
import org.txm.utils.*;
52 321 mdecorde
import org.txm.importer.*;
53 321 mdecorde
import org.txm.metadatas.*;
54 479 mdecorde
import org.txm.utils.io.FileCopy;
55 321 mdecorde
import org.w3c.dom.Element
56 479 mdecorde
import org.txm.utils.xml.DomUtils;
57 321 mdecorde
58 321 mdecorde
//PARAMETERS
59 321 mdecorde
boolean removeInterviewer = false;//if true the transcription of speakers (en1 and enq2) defined in metadatas file will be ignored
60 321 mdecorde
boolean includeComments = false;
61 321 mdecorde
boolean ignoreTranscriberMetadata = false;
62 321 mdecorde
int csvHeaderNumber = 1;
63 321 mdecorde
int maxlines = 200;
64 321 mdecorde
65 321 mdecorde
String userDir = System.getProperty("user.home");
66 321 mdecorde
67 321 mdecorde
def MONITOR;
68 321 mdecorde
boolean debug = org.txm.utils.logger.Log.isPrintingErrors();
69 321 mdecorde
BaseParameters params;
70 321 mdecorde
try {params = paramsBinding;MONITOR=monitor} catch (Exception)
71 321 mdecorde
{        println "DEV MODE";//exception means we debug
72 321 mdecorde
        debug = true
73 321 mdecorde
        params = new BaseParameters(new File(userDir, "xml/anapovoas/import.xml"))
74 321 mdecorde
        params.load()
75 321 mdecorde
        if (!org.txm.Toolbox.isInitialized()) {
76 321 mdecorde
77 321 mdecorde
                Toolbox.setParam(Toolbox.INSTALL_DIR,new File("/usr/lib/TXM"));
78 321 mdecorde
                Toolbox.setParam(Toolbox.TREETAGGER_INSTALL_PATH,new File(userDir,"treetagger"));
79 321 mdecorde
                Toolbox.setParam(Toolbox.TREETAGGER_MODELS_PATH,new File(userDir,"treetagger/models"));
80 321 mdecorde
                Toolbox.setParam(Toolbox.METADATA_ENCODING, "UTF-8");
81 321 mdecorde
                Toolbox.setParam(Toolbox.METADATA_COLSEPARATOR, "\t");
82 321 mdecorde
                Toolbox.setParam(Toolbox.METADATA_TXTSEPARATOR, "");
83 321 mdecorde
                Toolbox.setParam(Toolbox.USER_TXM_HOME, new File(System.getProperty("user.home"), "TXM"));
84 321 mdecorde
        }
85 321 mdecorde
}
86 321 mdecorde
if (params == null) { println "no parameters. Aborting"; return; }
87 321 mdecorde
88 321 mdecorde
String corpusname = params.getCorpusName();
89 321 mdecorde
Element corpusElem = params.corpora.get(corpusname);
90 321 mdecorde
String basename = params.name;
91 321 mdecorde
String rootDir = params.rootDir;
92 321 mdecorde
String lang = corpusElem.getAttribute("lang");
93 321 mdecorde
String model = lang
94 321 mdecorde
String encoding = corpusElem.getAttribute("encoding");
95 321 mdecorde
boolean annotate = "true" == corpusElem.getAttribute("annotate");
96 321 mdecorde
String xsl = params.getXsltElement(corpusElem).getAttribute("xsl")
97 321 mdecorde
def xslParams = params.getXsltParams(corpusElem);
98 321 mdecorde
int wordsPerPage = params.getWordsPerPage("default")
99 321 mdecorde
String page_element = params.getPageElement("default")
100 321 mdecorde
boolean build_edition = params.getDoEdition("default")
101 321 mdecorde
102 321 mdecorde
File srcDir = new File(rootDir);
103 321 mdecorde
File binDir = new File(Toolbox.getParam(Toolbox.USER_TXM_HOME),"corpora/"+basename);
104 321 mdecorde
binDir.deleteDir();
105 321 mdecorde
binDir.mkdirs();
106 321 mdecorde
if (!binDir.exists()) {
107 321 mdecorde
        println "Could not create binDir "+binDir
108 321 mdecorde
        return;
109 321 mdecorde
}
110 321 mdecorde
111 321 mdecorde
File txmDir = new File(binDir,"txm/$corpusname");
112 321 mdecorde
txmDir.deleteDir();
113 321 mdecorde
txmDir.mkdirs();
114 321 mdecorde
115 321 mdecorde
//get metadata values from CSV
116 321 mdecorde
Metadatas metadatas; // text metadata
117 321 mdecorde
File allmetadatasfile = new File(srcDir, "metadata.csv");
118 321 mdecorde
println "Trying to read metadatas values from: "+allmetadatasfile
119 321 mdecorde
if (allmetadatasfile.exists()) {
120 321 mdecorde
        File copy = new File(binDir, "metadata.csv")
121 321 mdecorde
        if (!FileCopy.copy(allmetadatasfile, copy)) {
122 321 mdecorde
                println "Error: could not create a copy of metadata file "+allmetadatasfile.getAbsoluteFile();
123 321 mdecorde
                return;
124 321 mdecorde
        }
125 321 mdecorde
        metadatas = new Metadatas(copy, Toolbox.getParam(Toolbox.METADATA_ENCODING), Toolbox.getParam(Toolbox.METADATA_COLSEPARATOR), Toolbox.getParam(Toolbox.METADATA_TXTSEPARATOR), 1)
126 321 mdecorde
}
127 321 mdecorde
else
128 321 mdecorde
        println "no metadata file: "+allmetadatasfile
129 321 mdecorde
130 321 mdecorde
File propertyFile = new File(srcDir, "import.properties")//default
131 321 mdecorde
Properties props = new Properties();
132 321 mdecorde
String[] metadatasToKeep;
133 321 mdecorde
if (propertyFile.exists() && propertyFile.canRead()) {
134 321 mdecorde
        FileInputStream input = new FileInputStream(propertyFile);
135 321 mdecorde
        props.load(input);
136 321 mdecorde
        input.close();
137 321 mdecorde
138 321 mdecorde
        if (props.getProperty("removeInterviewer") != null)
139 321 mdecorde
                removeInterviewer = Boolean.parseBoolean(props.get("removeInterviewer").toString());
140 321 mdecorde
        if (props.getProperty("ignoreTranscriberMetadata") != null)
141 321 mdecorde
                ignoreTranscriberMetadata = Boolean.parseBoolean(props.get("ignoreTranscriberMetadata").toString());
142 321 mdecorde
        if (props.getProperty("metadataList") != null)
143 321 mdecorde
                metadatasToKeep = props.get("metadataList").toString().split("|");
144 321 mdecorde
        if (props.getProperty("csvHeaderNumber") != null)
145 321 mdecorde
                csvHeaderNumber = props.get("csvHeaderNumber").toString().split("|");
146 321 mdecorde
        //if (props.getProperty("includeComments") != null)
147 321 mdecorde
        //        includeComments = props.get("includeComments").toString();
148 321 mdecorde
149 321 mdecorde
        println "import properties: "
150 321 mdecorde
        println " removeInterviewer: "+removeInterviewer
151 321 mdecorde
        println " ignoreTranscriberMetadata: "+ignoreTranscriberMetadata
152 321 mdecorde
        println " metadataToKeep: "+metadatasToKeep
153 321 mdecorde
        println " ignored csvHeaderSize: "+csvHeaderNumber
154 321 mdecorde
        //println " includeComments: "+includeComments
155 321 mdecorde
}
156 321 mdecorde
157 321 mdecorde
// Apply XSL
158 321 mdecorde
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
159 321 mdecorde
if (MONITOR != null) MONITOR.worked(1, "XSL")
160 321 mdecorde
if (xsl != null && xsl.trim().length() > 0) {
161 321 mdecorde
        if (ApplyXsl2.processImportSources(new File(xsl), srcDir, new File(binDir, "src")))
162 321 mdecorde
                srcDir = new File(binDir, "src");
163 321 mdecorde
        println ""
164 321 mdecorde
}
165 321 mdecorde
166 321 mdecorde
try {
167 321 mdecorde
        // select only trs files
168 321 mdecorde
        String ext = "trs";
169 321 mdecorde
        ArrayList<File> trsfiles = srcDir.listFiles(); //find all trs files
170 321 mdecorde
        if (trsfiles  == null) {
171 321 mdecorde
                println ("No files in "+srcDir.getAbsolutePath())
172 321 mdecorde
                return false;
173 321 mdecorde
        }
174 321 mdecorde
        for (int i = 0 ; i < trsfiles.size() ; i++) {
175 321 mdecorde
                File f = trsfiles.get(i);
176 321 mdecorde
                if (!f.getName().endsWith(ext) || !f.canRead() || f.isHidden()) {
177 321 mdecorde
                        trsfiles.remove(i)
178 321 mdecorde
                        i--;
179 321 mdecorde
                }
180 321 mdecorde
        }
181 321 mdecorde
182 321 mdecorde
        if (trsfiles.size() == 0) {
183 321 mdecorde
                println ("No transcription file (*.trs) found in "+srcDir.getAbsolutePath()+". Aborting.")
184 321 mdecorde
                return false;
185 321 mdecorde
        }
186 321 mdecorde
187 321 mdecorde
        if (MONITOR != null) MONITOR.worked(1, "IMPORTER")
188 321 mdecorde
        if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
189 321 mdecorde
        println "-- IMPORTER"
190 321 mdecorde
        def imp = new importer(trsfiles, binDir, txmDir, metadatas, lang) //put result in the txm folder of binDir
191 321 mdecorde
        if (!imp.run()) {
192 321 mdecorde
                println "Failed to prepare files - Aborting";
193 321 mdecorde
                return;
194 321 mdecorde
        }
195 321 mdecorde
        if (MONITOR != null) MONITOR.worked(20)
196 321 mdecorde
197 321 mdecorde
        println "-- Xml Validation"
198 321 mdecorde
        if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
199 321 mdecorde
        for (File infile : txmDir.listFiles()) {
200 321 mdecorde
                if (!ValidateXml.test(infile)) {
201 321 mdecorde
                        println "$infile : Validation failed";
202 321 mdecorde
                        infile.delete();
203 321 mdecorde
                }
204 321 mdecorde
        }
205 321 mdecorde
206 321 mdecorde
        if (MONITOR != null) MONITOR.worked(5)
207 321 mdecorde
        if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
208 321 mdecorde
        println "-- Remove interviewer: "+removeInterviewer
209 321 mdecorde
        if (removeInterviewer)         {
210 321 mdecorde
                if (metadatas == null) {
211 321 mdecorde
                        println "Can't remove interviewer without a metadata.csv file defining who are the interviewers."
212 321 mdecorde
                } else {
213 321 mdecorde
                        println "Removing some speakers in "+txmDir.listFiles().length+" file(s)"
214 321 mdecorde
                        for (File infile : txmDir.listFiles()) {
215 321 mdecorde
                                String filename = infile.getName();
216 321 mdecorde
                                int idx = filename.indexOf(".xml");
217 321 mdecorde
                                if (idx > 0)
218 321 mdecorde
                                        filename = filename.substring(0, idx);
219 321 mdecorde
220 321 mdecorde
                                ArrayList<Pair<String, String>> metas = metadatas.get(filename)
221 321 mdecorde
                                //println "filename=$filename metas= $metas"
222 321 mdecorde
                                for (Pair p : metas) {
223 321 mdecorde
                                        if (p.getFirst().startsWith("enq")) {
224 321 mdecorde
                                                new RemoveSpeaker(infile, infile, p.getFirst())
225 321 mdecorde
                                        }
226 321 mdecorde
                                }
227 321 mdecorde
                        }
228 321 mdecorde
                }
229 321 mdecorde
        }
230 321 mdecorde
231 321 mdecorde
        if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
232 321 mdecorde
        if (MONITOR != null) MONITOR.worked(20, "ANNOTATE")
233 321 mdecorde
        println "-- ANNOTATE - Running NLP tools"
234 321 mdecorde
        boolean annotationSuccess = false;
235 321 mdecorde
        if (annotate && new Annotate().run(binDir, txmDir, model+".par")) {
236 321 mdecorde
                annotationSuccess = true;
237 321 mdecorde
        }
238 321 mdecorde
239 321 mdecorde
        if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
240 321 mdecorde
        if (MONITOR != null) MONITOR.worked(25, "COMPILING")
241 321 mdecorde
        println "--COMPILING - Building Search Engine indexes"
242 321 mdecorde
        trsfiles = txmDir.listFiles();
243 321 mdecorde
244 321 mdecorde
        def comp = new compiler()
245 321 mdecorde
        if(debug) comp.setDebug();
246 321 mdecorde
        comp.removeInterviewers(removeInterviewer);
247 321 mdecorde
        comp.setIgnoreTranscriberMetadata(ignoreTranscriberMetadata);
248 321 mdecorde
        comp.setAnnotationSucces(annotationSuccess)
249 321 mdecorde
        if (!comp.run(trsfiles, corpusname, "default", binDir)) {
250 321 mdecorde
                println "Failed to compile files";
251 321 mdecorde
                return;
252 321 mdecorde
        }
253 321 mdecorde
254 321 mdecorde
        if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
255 321 mdecorde
256 321 mdecorde
        File htmlDir = new File(binDir,"HTML/$corpusname");
257 321 mdecorde
        htmlDir.deleteDir()
258 321 mdecorde
        htmlDir.mkdirs();
259 321 mdecorde
        if (build_edition) {
260 321 mdecorde
261 321 mdecorde
                if (MONITOR != null) MONITOR.worked(20, "EDITION")
262 321 mdecorde
                println "-- EDITION - Building editions"
263 321 mdecorde
264 321 mdecorde
                List<File> filelist = txmDir.listFiles();
265 321 mdecorde
                Collections.sort(filelist);
266 321 mdecorde
                def second = 0
267 321 mdecorde
268 321 mdecorde
                println "Paginating texts: "
269 321 mdecorde
                for (File txmFile : filelist) {
270 321 mdecorde
                        print "."
271 321 mdecorde
                        String txtname = txmFile.getName();
272 321 mdecorde
                        int i = txtname.lastIndexOf(".");
273 321 mdecorde
                        if(i > 0) txtname = txtname.substring(0, i);
274 321 mdecorde
275 321 mdecorde
                        List<String> NoSpaceBefore = LangFormater.getNoSpaceBefore(lang);
276 321 mdecorde
                        List<String> NoSpaceAfter = LangFormater.getNoSpaceAfter(lang);
277 321 mdecorde
278 321 mdecorde
                        Element text = params.addText(corpusElem, txtname, txmFile);
279 321 mdecorde
280 321 mdecorde
                        def ed = new pager(txmFile, htmlDir, txtname, NoSpaceBefore, NoSpaceAfter, wordsPerPage, basename, page_element, metadatas);
281 321 mdecorde
                        Element edition = params.addEdition(text, "default", htmlDir.getAbsolutePath(), "html");
282 321 mdecorde
283 321 mdecorde
                        for (i = 0 ; i < ed.getPageFiles().size();) {
284 321 mdecorde
                                File f = ed.getPageFiles().get(i);
285 321 mdecorde
                                String wordid = ed.getIdx().get(i);
286 321 mdecorde
                                params.addPage(edition, ""+(++i), wordid);
287 321 mdecorde
                        }
288 321 mdecorde
289 321 mdecorde
                        if (ed.getPageFiles().size() > 0) {
290 321 mdecorde
                                Element editionBD = params.addEdition(text, "onepage", htmlDir.getAbsolutePath(), "html");
291 321 mdecorde
                                params.addPage(editionBD, "1", ed.getIndexes().get(0));
292 321 mdecorde
                        }
293 321 mdecorde
                }
294 321 mdecorde
295 321 mdecorde
                //copy transcriber.css
296 321 mdecorde
                File cssfile = new File(Toolbox.getParam(Toolbox.USER_TXM_HOME), "css/transcriber.css")
297 321 mdecorde
                if (cssfile.exists() && htmlDir.exists()) {
298 321 mdecorde
                        FileCopy.copy(cssfile, new File(htmlDir, "transcriber.css"));
299 321 mdecorde
                        FileCopy.copy(cssfile, new File(htmlDir, "onepage/transcriber.css"));
300 321 mdecorde
                        FileCopy.copy(cssfile, new File(htmlDir, "default/transcriber.css"));
301 321 mdecorde
                }
302 321 mdecorde
        }
303 321 mdecorde
}
304 321 mdecorde
catch (Exception e){org.txm.utils.logger.Log.printStackTrace(e);}
305 321 mdecorde
306 321 mdecorde
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
307 321 mdecorde
if (MONITOR != null) MONITOR.worked(20, "FINALIZING")
308 321 mdecorde
File paramFile = new File(binDir, "import.xml");
309 321 mdecorde
DomUtils.save(params.root.getOwnerDocument(), paramFile);readyToLoad = true;