Statistics
| Revision:

root / tmp / org.txm.core / src / groovy / org / txm / importer / discours / discoursLoader.groovy @ 187

History | View | Annotate | Download (5.3 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: 2014-11-20 11:30:43 +0100 (Thu, 20 Nov 2014) $
25
// $LastChangedRevision: 2904 $
26
// $LastChangedBy: mdecorde $
27
//
28
package org.txm.importer.discours;
29

    
30
import org.txm.importer.discours.compiler;
31
import org.txm.importer.xml.pager;
32
import org.txm.objects.*;
33
import org.txm.utils.*;
34
import org.txm.*;
35
import org.txm.utils.i18n.*;
36
import org.w3c.dom.Element
37

    
38
String userDir = System.getProperty("user.home");
39

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

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

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

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

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

    
87
if (MONITOR != null) MONITOR.worked(1, "IMPORTER")
88
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
89
println "-- IMPORTER - Reading source files"
90
def imp = new importer();
91
if (!imp.run(srcDir, binDir, txmDir, encoding, basename)) {
92
        println "import process stopped";
93
        return;
94
}
95

    
96
println "-- ANNOTATE - Running NLP tools"
97
println "No annotation to do"
98

    
99
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
100
if (MONITOR != null) MONITOR.worked(25, "COMPILING")
101
println("-- COMPILING - Building Search Engine indexes")
102
List<File> files = txmDir.listFiles();
103
def c = new compiler()
104
if (debug) c.setDebug();
105
// no annotation
106
c.setLang(lang);
107
if (!c.run(files, binDir, txmDir, corpusname)) {
108
        println "import process stopped";
109
        return;
110
}
111

    
112
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
113

    
114
new File(binDir,"HTML/$corpusname").deleteDir();
115
new File(binDir,"HTML/$corpusname").mkdirs();
116
if (build_edition) {
117

    
118
        println "-- EDITION - Building edition"
119
        if (MONITOR != null) MONITOR.worked(20, "EDITION")
120
        
121
        File outdir = new File(binDir,"/HTML/$corpusname/default/");
122
        outdir.mkdirs();
123
        List<File> filelist = txmDir.listFiles();
124
        Collections.sort(filelist);
125

    
126
        println "Paginating texts: "
127

    
128
        for (File srcfile : filelist) {
129
                print "."
130
                String txtname = srcfile.getName();
131
                int i = txtname.lastIndexOf(".");
132
                if(i > 0) txtname = txtname.substring(0, i);
133

    
134
                List<String> NoSpaceBefore = LangFormater.getNoSpaceBefore(lang);
135
                List<String> NoSpaceAfter = LangFormater.getNoSpaceAfter(lang);
136

    
137
                Element text = params.addText(params.corpora.get(corpusname), txtname, srcfile);
138

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

    
142
                for (i = 0 ; i < ed.getPageFiles().size();) {
143
                        File f = ed.getPageFiles().get(i);
144
                        String wordid = ed.getIdx().get(i);
145
                        params.addPage(edition, ""+(++i), wordid);
146
                }
147
        }
148
}
149
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
150
if (MONITOR != null) MONITOR.worked(20, "FINALIZING")
151
File paramFile = new File(binDir, "import.xml");
152
DomUtils.save(params.root.getOwnerDocument(), paramFile);readyToLoad = true;