Statistics
| Revision:

root / tmp / org.txm.core / src / groovy / org / txm / importer / alceste / alcesteLoader.groovy @ 148

History | View | Annotate | Download (5.6 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-06-03 15:04:53 +0200 (Wed, 03 Jun 2015) $
25
// $LastChangedRevision: 2984 $
26
// $LastChangedBy: mdecorde $
27
//
28
package org.txm.importer.alceste;
29

    
30
import org.txm.importer.alceste.importer;
31
import org.txm.importer.alceste.compiler;
32
import org.txm.importer.xml.pager_old;
33
import org.txm.objects.*;
34
import org.txm.utils.*;
35
import org.txm.*;
36
import org.txm.scripts.teitxm.*;
37
import org.txm.utils.i18n.*;
38
import org.w3c.dom.Element
39

    
40
String userDir = System.getProperty("user.home");
41

    
42
def MONITOR;
43
boolean debug = org.txm.utils.logger.Log.isPrintingErrors();
44
BaseParameters params;
45
try {params = paramsBinding;MONITOR=monitor} catch (Exception)
46
{        println "DEV MODE";//exception means we debug
47
        debug = true
48
        params = new BaseParameters(new File(userDir, "xml/TESTS/alceste2/import.xml"))
49
        params.load()
50
        if (!org.txm.Toolbox.isInitialized()) {
51
                Toolbox.setParam(Toolbox.INSTALL_DIR,new File("/usr/lib/TXM"));
52
                Toolbox.setParam(Toolbox.TREETAGGER_INSTALL_PATH,new File(userDir,"treetagger"));
53
                Toolbox.setParam(Toolbox.TREETAGGER_MODELS_PATH,new File(userDir,"treetagger/models"));
54
                Toolbox.setParam(Toolbox.METADATA_ENCODING, "UTF-8");
55
                Toolbox.setParam(Toolbox.METADATA_COLSEPARATOR, ",");
56
                Toolbox.setParam(Toolbox.METADATA_TXTSEPARATOR, "\"");
57
                Toolbox.setParam(Toolbox.USER_TXM_HOME, new File(System.getProperty("user.home"), "TXM"));
58
        }
59
}
60
if (params == null) { println "no parameters. Aborting"; return; }
61

    
62
String corpusname = params.getCorpusName();
63

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

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

    
85
File txmDir = new File(binDir,"txm/$corpusname");
86
txmDir.deleteDir();
87
txmDir.mkdirs();
88

    
89
if (MONITOR != null) MONITOR.worked(1, "IMPORTER")
90
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
91
println "-- IMPORTER - Reading source files"
92

    
93
if (!(new importer().run(srcDir, binDir, txmDir, encoding, basename, lang))) {
94
        println "import process stopped";
95
        return;
96
}
97

    
98
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
99
if (MONITOR != null) MONITOR.worked(20, "ANNOTATE")
100
println "-- ANNOTATE - Running NLP tools"
101
boolean annotationSuccess = false;
102
if (annotate && new Annotate().run(binDir, txmDir, model+".par")) {
103
        annotationSuccess = true;
104
}
105

    
106
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
107
if (MONITOR != null) MONITOR.worked(25, "COMPILING")
108
println "-- COMPILING - Building Search Engine indexes"
109
def c = new compiler();
110
c.setAnnotationSuccess(annotationSuccess)
111
if (debug) c.setDebug();
112
c.setLang(lang);
113
if (!c.run(binDir, txmDir, corpusname)) {
114
        println "import process stopped";
115
        return;
116
}
117

    
118
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
119

    
120
new File(binDir,"HTML/$corpusname").deleteDir();
121
new File(binDir,"HTML/$corpusname").mkdirs();
122
if (build_edition) {
123
        
124
        if (MONITOR != null) MONITOR.worked(20, "EDITION")
125

    
126
        println "-- EDITION - Building edition"
127
        File outdir = new File(binDir,"/HTML/$corpusname/default/");
128
        outdir.mkdirs();
129
        List<File> filelist = txmDir.listFiles();
130
        Collections.sort(filelist);
131
        def second = 0
132

    
133
        println "Paginating texts: "
134

    
135
        for (File srcfile : filelist) {
136
                print "."
137
                String txtname = srcfile.getName();
138
                int i = txtname.lastIndexOf(".");
139
                if(i > 0) txtname = txtname.substring(0, i);
140

    
141
                List<String> NoSpaceBefore = LangFormater.getNoSpaceBefore(lang);
142
                List<String> NoSpaceAfter = LangFormater.getNoSpaceAfter(lang);
143

    
144
                Element text = params.addText(params.corpora.get(corpusname), txtname, srcfile);
145

    
146
                def ed = new pager_old(srcfile, outdir, txtname, NoSpaceBefore, NoSpaceAfter, wordsPerPage, basename, "pb");
147
                Element edition = params.addEdition(text, "default", outdir.getAbsolutePath(), "html");
148

    
149
                for (i = 0 ; i < ed.getPageFiles().size();) {
150
                        File f = ed.getPageFiles().get(i);
151
                        String wordid = ed.getIdx().get(i);
152
                        params.addPage(edition, ""+(++i), wordid);
153
                }
154
        }
155
}
156

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