Statistics
| Revision:

root / tmp / org.txm.core / src / groovy / org / txm / importer / quick / quickLoader.groovy @ 187

History | View | Annotate | Download (7.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: 2016-05-11 08:38:47 +0200 (Wed, 11 May 2016) $
25
// $LastChangedRevision: 3211 $
26
// $LastChangedBy: mdecorde $
27
//
28
package org.txm.importer.quick;
29

    
30
import org.txm.*
31
import org.txm.importer.xml.pager
32
import org.txm.metadatas.*
33
import org.txm.objects.*
34
import org.txm.scripts.teitxm.*
35
import org.txm.utils.FileCopy
36
import org.txm.utils.i18n.*
37
import org.w3c.dom.Element
38

    
39
String userDir = System.getProperty("user.home");
40
boolean debug = org.txm.utils.logger.Log.isPrintingErrors();
41
def MONITOR;
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, "TXM/clipboard/clipboard2/import.xml"))
47
        params.load()
48
        if (!org.txm.Toolbox.isInitialized()) {
49
                Toolbox.setParam(Toolbox.INSTALL_DIR,new File("C:/Program Files/TXM"));
50
                Toolbox.setParam(Toolbox.TREETAGGER_INSTALL_PATH, new File("C:/Program Files/TreeTagger"));
51
                Toolbox.setParam(Toolbox.TREETAGGER_MODELS_PATH, new File("C:/Users/mdecorde/TXM/treetagger"));
52
                Toolbox.setParam(Toolbox.METADATA_ENCODING, "UTF-8");
53
                Toolbox.setParam(Toolbox.METADATA_COLSEPARATOR, ",");
54
                Toolbox.setParam(Toolbox.METADATA_TXTSEPARATOR, "\"");
55
                Toolbox.setParam(Toolbox.USER_TXM_HOME, new File(userDir, "TXM"));
56
        }
57
}
58
if (params == null) { println "No parameters file. Aborting"; return;}
59

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

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

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

    
86
//get metadata values from CSV
87
Metadatas metadatas; // text metadata
88
File allmetadatasfile = new File(srcDir, "metadata.csv");
89

    
90
if (allmetadatasfile.exists()) {
91
        println "Trying to read metadata from: "+allmetadatasfile
92
        File copy = new File(binDir, "metadata.csv")
93
        if (!FileCopy.copy(allmetadatasfile, copy)) {
94
                println "Error: could not create a copy of metadata file "+allmetadatasfile.getAbsoluteFile();
95
                return;
96
        }
97
        metadatas = new Metadatas(copy, Toolbox.getParam(Toolbox.METADATA_ENCODING), Toolbox.getParam(Toolbox.METADATA_COLSEPARATOR), Toolbox.getParam(Toolbox.METADATA_TXTSEPARATOR), 1)
98
} else {
99
        println "No metadata file: "+allmetadatasfile
100
}
101

    
102
def suffixes = ["txt", "TXT"]
103
println "-- IMPORTER - Reading source files with extension "+suffixes
104
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
105
if (MONITOR != null) MONITOR.worked(20, "IMPORTER - Reading source files with extension "+suffixes)
106
if (!new importer().run(srcDir, binDir, txmDir,encoding, suffixes, basename, lang)) {
107
        println "Import process stopped";
108
        return;
109
}
110

    
111
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
112
if (MONITOR != null) MONITOR.worked(20, "INJECTING METADATA - from csv file: "+allmetadatasfile)
113
if (metadatas != null) {
114
        println "-- INJECTING METADATA - from csv file: "+allmetadatasfile
115
        println("Injecting metadata: "+metadatas.getHeadersList()+" in texts of directory "+txmDir)
116
        for (File infile : txmDir.listFiles()) {
117
                print "."
118
                File outfile = File.createTempFile("temp", ".xml", infile.getParentFile());
119

    
120
                if (!metadatas.injectMetadatasInXml(infile, outfile, "text", null)) {
121
                        outfile.delete();
122
                } else {
123
                        if (!(infile.delete() && outfile.renameTo(infile))) println "Warning can't rename file "+outfile+" to "+infile
124
                        if (!infile.exists()) {
125
                                println "Error: could not replace $infile by $outfile"
126
                                return false;
127
                        }
128
                }
129
        }
130
        println ""
131
}
132

    
133
println "-- ANNOTATE - Running NLP tools"
134
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
135
if (MONITOR != null) MONITOR.worked(20, "ANNOTATE - Running NLP tools")
136
boolean annotationSuccess = true;
137
if (annotate && !new Annotate().run(binDir, txmDir, model+".par")) {
138
        annotationSuccess = false;
139
}
140

    
141
println "-- COMPILING - Building Search Engine indexes"
142
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
143
if (MONITOR != null) MONITOR.worked(20, "COMPILING - Building Search Engine indexes")
144
def c = new compiler(null, "", corpusname, "default");
145
//c.setCwbPath(userDir+"/TXM/cwb/bin/")// for developers
146
if (metadatas != null)
147
        c.setMetadataAttributes(metadatas.getSattributes())
148
c.setLang(lang);
149
c.setAnnotationSuccess(annotationSuccess)
150
if (debug) c.setDebug();
151
if (!c.run(binDir, txmDir, corpusname)) {
152
        println "Import process stopped";
153
        return;
154
}
155
//println "basename :"+basename;
156
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
157
new File(binDir,"HTML/$corpusname").deleteDir();
158
new File(binDir,"HTML/$corpusname").mkdirs();
159
if (build_edition) {
160

    
161
        println "-- EDITION - Building edition"
162
        if (MONITOR != null) MONITOR.worked(20, "EDITION - Building edition")
163
        
164
        File outdir = new File(binDir, "HTML/$corpusname/default/");
165
        outdir.mkdirs();
166
        List<File> filelist = txmDir.listFiles();
167
        Collections.sort(filelist);
168
        def second = 0
169

    
170

    
171
        for (File srcfile : filelist) {
172
                print "."
173
                String txtname = srcfile.getName();
174
                int i = txtname.lastIndexOf(".");
175
                if (i > 0) txtname = txtname.substring(0, i);
176

    
177
                List<String> NoSpaceBefore = LangFormater.getNoSpaceBefore(lang);
178
                List<String> NoSpaceAfter = LangFormater.getNoSpaceAfter(lang);
179

    
180
                Element text = params.addText(params.corpora.get(corpusname), txtname, srcfile);
181

    
182
                def ed = new pager(srcfile, outdir, txtname, NoSpaceBefore, NoSpaceAfter, wordsPerPage, basename, null);
183
                Element edition = params.addEdition(text, "default", outdir.getAbsolutePath(), "html");
184

    
185
                for (i = 0 ; i < ed.getPageFiles().size();) {
186
                        File f = ed.getPageFiles().get(i);
187
                        String wordid = ed.getIdx().get(i);
188
                        params.addPage(edition, ""+(++i), wordid);
189
                }
190
        }
191
}
192
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
193
if (MONITOR != null) MONITOR.worked(20, "FINALIZING")
194
File paramFile = new File(binDir, "import.xml");
195
DomUtils.save(params.root.getOwnerDocument(), paramFile);readyToLoad = true;