Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / importer / txt / txtLoader.groovy @ 945

History | View | Annotate | Download (7.4 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: 2017-04-11 15:30:35 +0200 (mar. 11 avril 2017) $
25
// $LastChangedRevision: 3426 $
26
// $LastChangedBy: mdecorde $
27
//
28
package org.txm.importer.txt;
29

    
30
import org.txm.importer.txt.importer;
31
import org.txm.importer.txt.compiler;
32
import org.txm.importer.xml.pager;
33
import org.txm.objects.*;
34
import org.txm.importer.xmltxm.*;
35
import org.txm.*;
36
import org.txm.core.engines.*;
37
import org.txm.utils.i18n.*;
38
import org.txm.metadatas.*;
39
import org.txm.utils.io.FileCopy;
40
import org.w3c.dom.Element
41
import org.txm.utils.xml.DomUtils;
42

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

    
63
String corpusname = params.getCorpusName();
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.getTxmHomePath(), "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
//get metadata values from CSV
90
Metadatas metadatas; // text metadata
91
File allmetadatasfile = new File(srcDir, "metadata.csv");
92

    
93
if (allmetadatasfile.exists()) {
94
        println "Trying to read metadata from: "+allmetadatasfile
95
        File copy = new File(binDir, "metadata.csv")
96
        if (!FileCopy.copy(allmetadatasfile, copy)) {
97
                println "Error: could not create a copy of metadata file "+allmetadatasfile.getAbsoluteFile();
98
                return;
99
        }
100
        metadatas = new Metadatas(copy, Toolbox.getMetadataEncoding(), 
101
                Toolbox.getMetadataColumnSeparator(), 
102
                Toolbox.getMetadataTextSeparator(), 1)
103
} else {
104
        println "No metadata file: "+allmetadatasfile
105
}
106

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

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

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

    
138
println "-- ANNOTATE - Running NLP tools"
139
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
140
if (MONITOR != null) MONITOR.worked(20, "ANNOTATE - Running NLP tools")
141
boolean annotationSuccess = true;
142
if (annotate) {
143
        def engine = Toolbox.getEngineManager(EngineType.ANNOTATION).getEngine("TreeTagger")
144
        if (engine.processDirectory(txmDir, binDir, ["lang":model])) {
145
                annotationSuccess = true;
146
        }
147
}
148

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

    
169
        println "-- EDITION - Building edition"
170
        if (MONITOR != null) MONITOR.worked(20, "EDITION - Building edition")
171
        
172
        File outdir = new File(binDir, "HTML/$corpusname/default/");
173
        outdir.mkdirs();
174
        List<File> filelist = txmDir.listFiles();
175
        Collections.sort(filelist);
176
        def second = 0
177

    
178

    
179
        for (File srcfile : filelist) {
180
                print "."
181
                String txtname = srcfile.getName();
182
                int i = txtname.lastIndexOf(".");
183
                if (i > 0) txtname = txtname.substring(0, i);
184

    
185
                List<String> NoSpaceBefore = LangFormater.getNoSpaceBefore(lang);
186
                List<String> NoSpaceAfter = LangFormater.getNoSpaceAfter(lang);
187

    
188
                Element text = params.addText(params.corpora.get(corpusname), txtname, srcfile);
189

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

    
193
                for (i = 0 ; i < ed.getPageFiles().size();) {
194
                        File f = ed.getPageFiles().get(i);
195
                        String wordid = ed.getIdx().get(i);
196
                        params.addPage(edition, ""+(++i), wordid);
197
                }
198
        }
199
}
200
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
201
if (MONITOR != null) MONITOR.worked(20, "FINALIZING")
202
File paramFile = new File(binDir, "import.xml");
203
DomUtils.save(params.root.getOwnerDocument(), paramFile);readyToLoad = true;