Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / importer / hyperbase / hyperbaseLoader.groovy @ 479

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 (mer. 03 juin 2015) $
25
// $LastChangedRevision: 2984 $
26
// $LastChangedBy: mdecorde $
27
//
28
package org.txm.importer.hyperbase;
29

    
30

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

    
42
String userDir = System.getProperty("user.home");
43

    
44
def MONITOR;
45
boolean debug = org.txm.utils.logger.Log.isPrintingErrors();
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/TESTS/hyperbase/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
boolean build_edition = params.getDoEdition("default")
77

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

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

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

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

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

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

    
120
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
121

    
122
new File(binDir,"HTML/$corpusname").deleteDir();
123
new File(binDir,"HTML/$corpusname").mkdirs();
124
if (build_edition) {
125

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

    
135
        println "Paginating texts: "
136

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

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

    
146
                Element text = params.addText(params.corpora.get(corpusname), txtname, srcfile);
147

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

    
151
                for (i = 0 ; i < ed.getPageFiles().size();) {
152
                        File f = ed.getPageFiles().get(i);
153
                        String wordid = ed.getIdx().get(i);
154
                        params.addPage(edition, ""+(++i), wordid);
155
                }
156
        }
157
}
158
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
159
if (MONITOR != null) MONITOR.worked(20, "FINALIZING")
160
File paramFile = new File(binDir, "import.xml");
161
DomUtils.save(params.root.getOwnerDocument(), paramFile);readyToLoad = true;