Statistics
| Revision:

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

History | View | Annotate | Download (5.5 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.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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
134
        println "Paginating texts: "
135

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

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

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

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

    
150
                for (i = 0 ; i < ed.getPageFiles().size();) {
151
                        File f = ed.getPageFiles().get(i);
152
                        String wordid = ed.getIdx().get(i);
153
                        params.addPage(edition, ""+(++i), wordid);
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;