Statistics
| Revision:

root / tmp / org.txm.core / src / groovy / org / txm / importer / factiva / factivamailLoader.groovy @ 148

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: 2012-07-20 17:33:42 +0200 (ven., 20 juil. 2012) $
25
// $LastChangedRevision: 2221 $
26
// $LastChangedBy: mdecorde $
27
//
28
package org.txm.importer.factiva;
29

    
30
import org.txm.importer.alceste.importer;
31
import org.txm.importer.alceste.compiler;
32
import org.txm.importer.xml.pager_old;
33
import org.txm.objects.*;
34
import org.txm.utils.*;
35
import org.txm.*;
36
import org.txm.scripts.teitxm.*;
37
import org.txm.utils.i18n.*;
38
import org.w3c.dom.Element
39

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

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

    
52
                Toolbox.setParam(Toolbox.INSTALL_DIR,new File("/usr/lib/TXM"));
53
                Toolbox.setParam(Toolbox.TREETAGGER_INSTALL_PATH,new File(userDir,"treetagger"));
54
                Toolbox.setParam(Toolbox.TREETAGGER_MODELS_PATH,new File(userDir,"treetagger/models"));
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(System.getProperty("user.home"), "TXM"));
59
        }
60
}
61
if (params == null) { println "no parameters. Aborting"; return; }
62

    
63
String corpusname = params.getCorpusName();
64

    
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

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

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

    
88
// TRANSFORM FACTIVA TO ALCESTE HERE
89
File newSrcDir = new File(binDir, "src")
90
newSrcDir.mkdir();
91
File alcesteFile = new File(newSrcDir, srcDir.getName()+".txt");
92
FactivaMail2Alceste converter = new FactivaMail2Alceste(srcDir, alcesteFile, "UTF-8", "UTF-8")
93
if (!converter.doparse()) {
94
        println "Failed to convert files in $srcDir to one Alceste file"
95
        return false;
96
}
97
if (MONITOR != null) MONITOR.worked(1, "IMPORTER")
98
if (MONITOR != null && MONITOR.isCanceled()) { return MONITOR.done(); }
99
println "-- IMPORTER - Reading source files"
100

    
101
if (!(new importer().run(newSrcDir, binDir, txmDir, encoding, basename, lang))) {
102
        println "import process stopped";
103
        return;
104
}
105

    
106
if (MONITOR != null) MONITOR.worked(20, "ANNOTATE")
107
println "-- ANNOTATE - Running NLP tools"
108
boolean annotationSuccess = false;
109
if (annotate && new Annotate().run(binDir, txmDir, model+".par")) {
110
        annotationSuccess = true;
111
}
112

    
113
if (MONITOR != null) MONITOR.worked(25, "COMPILING")
114
println "-- COMPILING - Building Search Engine indexes"
115
def c = new compiler();
116
c.setAnnotationSuccess(annotationSuccess)
117
if (debug) c.setDebug();
118
c.setLang(lang);
119
if (!c.run(binDir, txmDir, corpusname)) {
120
        println "import process stopped";
121
        return;
122
}
123

    
124
if (MONITOR != null) MONITOR.worked(20, "EDITION")
125
println "-- EDITION - Building edition"
126
new File(binDir,"HTML/$corpusname").deleteDir();
127
new File(binDir,"HTML/$corpusname").mkdirs();
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
for (File srcfile : filelist) {
136
        print "."
137
        String txtname = srcfile.getName();
138
        int i = txtname.lastIndexOf(".");
139
        if(i > 0) txtname = txtname.substring(0, i);
140
        
141
        List<String> NoSpaceBefore = LangFormater.getNoSpaceBefore(lang);
142
        List<String> NoSpaceAfter = LangFormater.getNoSpaceAfter(lang);
143

    
144
        Element text = params.addText(params.corpora.get(corpusname), txtname, srcfile);
145
                
146
        def ed = new pager_old(srcfile, outdir, txtname, NoSpaceBefore, NoSpaceAfter, 500, basename, "pb");
147
        Element edition = params.addEdition(text, "default", outdir.getAbsolutePath(), "html");
148
        
149
        for (i = 0 ; i < ed.getPageFiles().size();) {
150
                File f = ed.getPageFiles().get(i);
151
                String wordid = ed.getIdx().get(i);
152
                params.addPage(edition, ""+(++i), wordid);
153
        }
154
}
155

    
156
if (MONITOR != null) MONITOR.worked(20, "FINALIZING")
157
File paramFile = new File(binDir, "import.xml");
158
DomUtils.save(params.root.getOwnerDocument(), paramFile);readyToLoad = true;