Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / importer / tmx / tmxLoader.groovy @ 479

History | View | Annotate | Download (6.8 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: 2015-08-31 13:45:22 +0200 (lun. 31 août 2015) $
25
// $LastChangedRevision: 3028 $
26
// $LastChangedBy: mdecorde $
27
//
28
package org.txm.importer.tmx;
29
import org.txm.searchengine.cqp.corpus.MainCorpus;
30

    
31
import java.io.File;
32
import org.txm.importer.tmx.importer;
33
import org.txm.importer.tmx.compiler;
34
import org.txm.importer.tmx.pager;
35
import org.txm.objects.*;
36
import org.txm.utils.*;
37
import org.txm.*;
38
import org.txm.scripts.teitxm.*;
39
import org.txm.utils.i18n.*;
40
import org.w3c.dom.Element
41
import org.txm.utils.xml.DomUtils;
42

    
43
String userDir = System.getProperty("user.home");
44
boolean debug = org.txm.utils.logger.Log.isPrintingErrors();
45
def MONITOR;
46
BaseParameters params;
47
try {params = paramsBinding;MONITOR=monitor} catch (Exception)
48
{        println "DEV MODE";//exception means we debug
49
        debug = true
50
        params = new BaseParameters(new File(userDir, "xml/tmx/import.xml"))
51
        params.load()
52
        if (!org.txm.Toolbox.isInitialized()) {
53

    
54
                Toolbox.setParam(Toolbox.INSTALL_DIR,new File("/usr/lib/TXM"));
55
                Toolbox.setParam(Toolbox.TREETAGGER_INSTALL_PATH,new File(userDir,"treetagger"));
56
                Toolbox.setParam(Toolbox.TREETAGGER_MODELS_PATH,new File(userDir,"treetagger/models"));
57
                Toolbox.setParam(Toolbox.METADATA_ENCODING, "UTF-8");
58
                Toolbox.setParam(Toolbox.METADATA_COLSEPARATOR, ",");
59
                Toolbox.setParam(Toolbox.METADATA_TXTSEPARATOR, "\"");
60
                Toolbox.setParam(Toolbox.USER_TXM_HOME, new File(System.getProperty("user.home"), "TXM"));
61
        }
62
}
63
if (params == null) { println "no parameters. Aborting"; return; }
64

    
65
String corpusname = params.getCorpusName();
66
Element corpusElem = params.corpora.get(corpusname);
67
String basename = params.name;
68
String rootDir = params.rootDir;
69
String lang = corpusElem.getAttribute("lang");
70
String model = lang;
71
String encoding = corpusElem.getAttribute("encoding");
72
boolean annotate = "true" == corpusElem.getAttribute("annotate");
73
String xsl = params.getXsltElement(corpusElem).getAttribute("xsl")
74
def xslParams = params.getXsltParams(corpusElem);;
75
int wordsPerPage = params.getWordsPerPage("default")
76
String page_element = params.getPageElement("default")
77
boolean build_edition = params.getDoEdition("default")
78

    
79
File srcDir = new File(rootDir);
80
File binDir = new File(Toolbox.getParam(Toolbox.USER_TXM_HOME),"corpora/"+basename);
81
binDir.deleteDir();
82
binDir.mkdirs();
83
if (!binDir.exists()) {
84
        println "Could not create binDir "+binDir
85
        return;
86
}
87

    
88
File txmDir = new File(binDir, "txm");
89
txmDir.deleteDir();
90
txmDir.mkdirs();
91
HashMap<String, String> textLangs = [:];
92
def langGroups = [:];
93

    
94
println "-- IMPORTER - Reading source files"
95
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
96
if (MONITOR != null) MONITOR.worked(20, "IMPORTER - Reading source files");
97
def imp = new importer()
98
imp.run(srcDir, binDir, txmDir, textLangs, langGroups);
99
def corpusIDS = imp.getCorpusIDS()
100
println "-- ANNOTATE - Running NLP tools"
101
if (MONITOR != null) MONITOR.worked(20, "ANNOTATE - Running NLP tools");
102
new Annotate().run(binDir, txmDir, textLangs);
103

    
104
println "langs : "+textLangs
105
println "texts : "+langGroups
106

    
107
println "-- COMPILING - Building Search Engine indexes"
108
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
109
if (MONITOR != null) MONITOR.worked(20, "COMPILING - Building Search Engine indexes")
110
def c = new compiler();
111
if (debug) c.setDebug();
112
c.setAlignment("seg","id");
113
c.setLangs(textLangs);
114
c.setCorpusIDS(corpusIDS);
115
c.setLangGroups(langGroups);
116
if (!c.run(binDir, txmDir, basename)) {
117
        println "Compiler failed"
118
        return;
119
}
120

    
121
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
122
new File(binDir,"HTML").deleteDir();
123
new File(binDir,"HTML").mkdirs();
124
if (build_edition) {
125
        
126
        println "-- EDITION - Building edition"
127
        if (MONITOR != null) MONITOR.worked(20, "EDITION - Building edition")
128
        
129
        List<File> filelist = new File(binDir,"txm").listFiles();
130
        def second = 0
131

    
132
        def splitTUsPerText = [:]
133

    
134
        for (File txmCorpusDir : txmDir.listFiles()) {
135
                def txmFiles = txmCorpusDir.listFiles();
136
                txmFiles.sort() // same order
137
                //println "paging txmFiles : "+txmFiles
138

    
139
                File outdir = new File(binDir,"/HTML/"+txmCorpusDir.getName()+"/default/");
140
                outdir.mkdirs();
141
                //println "processing pages of corpus "+txmCorpusDir.getName()
142

    
143
                Element paraCorpusElem = corpusElem.cloneNode(true);
144
                paraCorpusElem.setAttribute("name", txmCorpusDir.getName());
145

    
146
                for (File srcfile : txmFiles) {
147
                        print "."
148
                        String txtname = srcfile.getName();
149
                        int i = txtname.lastIndexOf("_");
150
                        if(i > 0) txtname = txtname.substring(0, i);
151

    
152
                        //println ""+srcfile.getName()+" -> "+splitTUsPerText[txtname]
153
                        def splitTUs = splitTUsPerText[txtname];
154
                        if (splitTUs == null) {
155
                                splitTUs = [];
156
                                splitTUsPerText[txtname] = splitTUs;
157
                        }
158

    
159
                        String l = textLangs.get(srcfile.getName())
160
                        if (l == null) l = "fr";
161
                        List<String> NoSpaceBefore = LangFormater.getNoSpaceBefore(l);
162
                        List<String> NoSpaceAfter = LangFormater.getNoSpaceAfter(l);
163

    
164
                        Element text = params.addText(paraCorpusElem, txtname, srcfile);
165

    
166
                        def ed = new pager(srcfile, outdir, txtname, NoSpaceBefore, NoSpaceAfter, wordsPerPage, basename, page_element,splitTUs);
167
                        Element edition = params.addEdition(text, "default", outdir.getAbsolutePath(), "html");
168

    
169
                        for (i = 0 ; i < ed.getPageFiles().size();) {
170
                                File f = ed.getPageFiles().get(i);
171
                                String wordid = ed.getIdx().get(i);
172
                                params.addPage(edition, ""+(++i), wordid);
173
                        }
174
                }
175

    
176
                params.corporaElement.appendChild(paraCorpusElem);
177

    
178
                File cssfile = new File(Toolbox.getParam(Toolbox.USER_TXM_HOME), "css/tmx.css")
179
                if (cssfile.exists()) {
180
                        FileCopy.copy(cssfile, new File(outdir, "tmx.css"));
181
                }
182
                File stylefile = new File(binDir, "style.css")
183
                if (stylefile.exists()) {
184
                        FileCopy.copy(stylefile, new File(outdir, "style.css"));
185
                }
186
                println ""
187
        }
188
}
189
params.corporaElement.removeChild(corpusElem);
190

    
191
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
192
if (MONITOR != null) MONITOR.worked(20, "FINALIZING")
193
File paramFile = new File(binDir, "import.xml");
194
DomUtils.save(params.root.getOwnerDocument(), paramFile);readyToLoad = true;