Statistics
| Revision:

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

History | View | Annotate | Download (6.1 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.scripts.importer.tmx;
29
import org.txm.searchengine.cqp.corpus.MainCorpus;
30

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

    
45
String userDir = System.getProperty("user.home");
46
def MONITOR;
47
Project project;
48

    
49
try {project=projectBinding;MONITOR=monitor} catch (Exception)
50
{        }
51
if (project == null) { println "no project set. Aborting"; return; }
52

    
53
String corpusname = project.getName();
54
String basename = corpusname
55
String rootDir = project.getSrcdir();
56
String lang = project.getLang()
57
String model = lang
58
String encoding = project.getEncoding()
59
boolean annotate = project.getAnnotate()
60
String xsl = project.getFrontXSL();
61
def xslParams = project.getXsltParameters();
62
int wordsPerPage = project.getEditionDefinition("default").getWordsPerPage()
63
String page_element = project.getEditionDefinition("default").getPageElement()
64
boolean build_edition = project.getEditionDefinition("default").getBuildEdition()
65

    
66
File srcDir = new File(rootDir);
67
File binDir = project.getProjectDirectory();
68
binDir.mkdirs();
69
if (!binDir.exists()) {
70
        println "Could not create binDir "+binDir
71
        return;
72
}
73

    
74
File txmDir = new File(binDir, "txm");
75
txmDir.deleteDir();
76
txmDir.mkdirs();
77
HashMap<String, String> textLangs = [:];
78
def langGroups = [:];
79

    
80
println "-- IMPORTER - Reading source files"
81
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
82
if (MONITOR != null) MONITOR.worked(20, "IMPORTER - Reading source files");
83
def imp = new importer()
84
imp.run(srcDir, binDir, txmDir, textLangs, langGroups);
85
def corpusIDS = imp.getCorpusIDS()
86

    
87
if (MONITOR != null) MONITOR.worked(20, "ANNOTATE - Running NLP tools");
88
if (annotate) {
89
        println "-- ANNOTATE - Running NLP tools"
90
        def engine = Toolbox.getEngineManager(EngineType.ANNOTATION).getEngine("TreeTagger")
91
        if (engine.processDirectory(txmDir, binDir, ["lang":model])) {
92
                annotationSuccess = true;
93
        }
94
}
95
println "langs : "+textLangs
96
println "texts : "+langGroups
97

    
98
println "-- COMPILING - Building Search Engine indexes"
99
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
100
if (MONITOR != null) MONITOR.worked(20, "COMPILING - Building Search Engine indexes")
101
def c = new compiler();
102
if (debug) c.setDebug();
103
c.setAlignment("seg","id");
104
c.setLangs(textLangs);
105
c.setCorpusIDS(corpusIDS);
106
c.setLangGroups(langGroups);
107
if (!c.run(project, binDir, txmDir, basename)) {
108
        println "Compiler failed"
109
        return;
110
}
111

    
112
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
113
new File(binDir,"HTML").deleteDir();
114
new File(binDir,"HTML").mkdirs();
115
if (build_edition) {
116

    
117
        println "-- EDITION - Building edition"
118
        if (MONITOR != null) MONITOR.worked(20, "EDITION - Building edition")
119

    
120
        List<File> filelist = new File(binDir,"txm").listFiles();
121
        def second = 0
122

    
123
        def splitTUsPerText = [:]
124

    
125
        for (File txmCorpusDir : txmDir.listFiles()) {
126
                def txmFiles = txmCorpusDir.listFiles();
127
                txmFiles.sort() // same order
128
                //println "paging txmFiles : "+txmFiles
129

    
130
                File outdir = new File(binDir,"/HTML/"+txmCorpusDir.getName()+"/default/");
131
                outdir.mkdirs();
132
                //println "processing pages of corpus "+txmCorpusDir.getName()
133

    
134
                //                Element paraCorpusElem = corpusElem.cloneNode(true);
135
                //                paraCorpusElem.setAttribute("name", txmCorpusDir.getName());
136

    
137
                for (File txmFile : txmFiles) {
138
                        print "."
139
                        String txtname = txmFile.getName();
140
                        int i = txtname.lastIndexOf(".xml");
141
                        if(i > 0) txtname = txtname.substring(0, i);
142

    
143
                        //println ""+srcfile.getName()+" -> "+splitTUsPerText[txtname]
144
                        def splitTUs = splitTUsPerText[txtname];
145
                        if (splitTUs == null) {
146
                                splitTUs = [];
147
                                splitTUsPerText[txtname] = splitTUs;
148
                        }
149

    
150
                        String l = textLangs.get(txmFile.getName()).toLowerCase()
151
                        if (l == null) l = "fr";
152
                        List<String> NoSpaceBefore = LangFormater.getNoSpaceBefore(l);
153
                        List<String> NoSpaceAfter = LangFormater.getNoSpaceAfter(l);
154

    
155
                        Text t = new Text(project);
156
                        t.setName(txtname);
157
                        t.setSourceFile(txmFile)
158
                        t.setTXMFile(txmFile)
159

    
160
                        def ed = new pager(txmFile, outdir, txtname, NoSpaceBefore, NoSpaceAfter, wordsPerPage, basename, page_element,splitTUs);
161
                        Edition edition = new Edition(t);
162
                        edition.setName("default");
163
                        edition.setIndex(outdir.getAbsolutePath());
164
                        for (i = 0 ; i < ed.getPageFiles().size();) {
165
                                File f = ed.getPageFiles().get(i);
166
                                String wordid = "w_0";
167
                                if (i < ed.getIdx().size()) wordid = ed.getIdx().get(i);
168
                                edition.addPage(""+(++i), wordid);
169
                        }
170
                }
171

    
172
                File cssfile = new File(Toolbox.getTxmHomePath(), "css/tmx.css")
173
                if (cssfile.exists()) {
174
                        FileCopy.copy(cssfile, new File(outdir, "tmx.css"));
175
                }
176
                File stylefile = new File(binDir, "style.css")
177
                if (stylefile.exists()) {
178
                        FileCopy.copy(stylefile, new File(outdir, "style.css"));
179
                }
180
                println ""
181
        }
182
}
183

    
184
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
185
if (MONITOR != null) MONITOR.worked(20, "FINALIZING")
186

    
187
readyToLoad = project.save();