Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / scripts / importer / xtz / XTZImport.groovy @ 1000

History | View | Annotate | Download (5.2 kB)

1
package org.txm.scripts.importer.xtz;
2

    
3
import java.io.File;
4
import java.io.FileFilter;
5
import java.util.ArrayList;
6
import java.util.Arrays;
7
import java.util.Collections;
8

    
9
import org.eclipse.core.runtime.IProgressMonitor;
10
import org.txm.utils.xml.DomUtils;
11
import org.txm.metadatas.Metadatas
12
import org.txm.objects.BaseParameters
13
import org.txm.utils.io.FileCopy;
14
import org.txm.*
15
import org.w3c.dom.Element
16
import org.txm.importer.xtz.*
17

    
18
public class XTZImport extends ImportModule {
19

    
20
        public XTZImport(File importParametersFile) {
21
                super(importParametersFile);
22
        }
23

    
24
        public XTZImport(BaseParameters importParameters) {
25
                super(importParameters);
26
        }
27

    
28
        @Override
29
        public void init(BaseParameters p) {
30
                super.init(p);
31
                
32
                importer = new XTZImporter(this)
33
                compiler = new XTZCompiler(this)
34
                annotater = new TTAnnotater(this);
35
                pager = new XTZPager(this)
36
        }
37
        
38
        @Override
39
        protected ArrayList<File> getTXMFilesOrder() {
40
                //System.out.println("XTZ FILES ORDER");
41
                if (importer == null) {
42
                        println "no importer step, using default text order"
43
                        return super.getTXMFilesOrder();
44
                }
45
                Metadatas metadata = importer.getMetadata();
46
                if (metadata == null) {
47
                        println "no metadata, using default text order"
48
                        return super.getTXMFilesOrder();
49
                }
50
                File txmDirectory = new File(binaryDirectory, "txm/"+corpusName);
51
                ArrayList<File> files = new ArrayList<File>(Arrays.asList(txmDirectory.listFiles(new FileFilter() {
52
                        @Override
53
                        public boolean accept(File file) {
54
                                return file.isFile() && file.getName().endsWith(".xml");
55
                        }
56
                })));
57
        
58
                final HashMap<File, String> textorder = new HashMap<File, String>();
59
                for (File f : files) {
60
                        HashMap<String, String> m = metadata.getTextMetadata(f);
61
                        if (m != null && m.containsKey("textorder")) textorder[f] = m["textorder"];
62
                }
63
                println "Sorting texts using 'textorder' metadata values: "+textorder
64
                Collections.sort(files, new Comparator<File>() {
65
                        public int compare(File f1, File f2) {
66
                                String o1 = textorder[f1];
67
                                String o2 = textorder[f2];
68
                                if (o1 == null && o2 == null) {
69
                                        return f1.getName().compareTo(f2.getName());
70
                                } else if (o1 == null) {
71
                                        return 1
72
                                } else if (o2 == null) {
73
                                        return -1
74
                                } else {
75
                                        int c = o1.compareTo(o2);
76
                                        if (c == 0) return f1.getName().compareTo(f2.getName());
77
                                        else return c;
78
                                }
79
                        }
80
                });
81
                //println files
82
                return files;
83
        }
84
        
85
        public void start() throws InterruptedException {
86
                super.start();
87

    
88
                if (isSuccessful) {
89
                        
90
                        //declare a local KR
91
                        //TODO find out how the annotation plugin may hook the import steps
92
//                        List<String> krnames = importParameters.getKnowledgeRepositoryNames();
93
//                        if (krnames.size() == 0) {
94
//                                importParameters.createKnowledgeRepositoryElement("DEFAULT"); // set a default KR shared by all XTZ corpus
95
//                        } else if (krnames.size() == 1 && krnames.get(0).equals("DEFAULT")) {
96
//                                // nothing to do
97
//                        } else {
98
//                                println("Corpus is using custom Knowledge repositories: "+importParameters.getKnowledgeRepositoryNames());
99
//                        }
100
                        
101
                        //copy sub directories
102
                        if (isUpdatingCorpus()) {
103

    
104
                        } else {
105
                                def dirToCopy = ["xsl", "css", "dtd"]
106
                                println "--- Copying subdirectories $dirToCopy"
107
                                for (String dir : dirToCopy) {
108
                                        File origDirectory = new File(this.sourceDirectory, dir)
109
                                        if (origDirectory.exists()) {
110
                                                print "."
111
                                                File copyDirectory = new File(this.binaryDirectory, dir)
112
                                                FileCopy.copyFiles(origDirectory, copyDirectory)
113
                                        }
114
                                }
115
                                println ""
116
                        }
117
                }
118
        }
119

    
120
        public static void main(String[] args) {
121

    
122
                long start = System.currentTimeMillis()
123

    
124
                String userDir = System.getProperty("user.home")
125
                Toolbox.setParam(Toolbox.INSTALL_DIR,new File("/usr/lib/TXM"));
126
                //Toolbox.setParam(Toolbox.INSTALL_DIR,new File("C:\\Program Files\\TXM"));//For Windows
127
                Toolbox.setParam(Toolbox.METADATA_ENCODING, "UTF-8");
128
                Toolbox.setParam(Toolbox.METADATA_COLSEPARATOR, ",");
129
                Toolbox.setParam(Toolbox.METADATA_TXTSEPARATOR, "\"");
130
                //Toolbox.setParam(Toolbox.TREETAGGER_MODELS_PATH,new File("C:\\Program Files\\treetagger\\models"));//for Windows
131
                Toolbox.setParam(Toolbox.USER_TXM_HOME, new File(userDir, "TXM"));
132

    
133
                //File importParametersFile = new File("/home/mdecorde/xml/brown-for-xtz/import.xml");
134
                //                File importParametersFile = new File("/home/mdecorde/xml/qgraalc/qgraal_cw/import.xml"); // new import
135
                //File importParametersFile = new File("/home/mdecorde/TXM/corpora/QGRAALXTZ/import.xml"); // corpus update
136
                //                File importParametersFile = new File("/home/mdecorde/xml/annotation/import.xml"); // new import
137
                //File importParametersFile = new File("/home/mdecorde/xml/baiptest/import.xml"); // new import
138
                //File importParametersFile = new File("/home/mdecorde/TXM/corpora/BAIP/import.xml"); // corpus update
139
                File importParametersFile = new File("/home/mdecorde/xml/xtzsmall/import.xml");
140
                BaseParameters b = new BaseParameters(importParametersFile);
141
                b.getKeyValueParameters().put(ImportKeys.CLEAN, "false")
142
                b.getKeyValueParameters().put(ImportKeys.MULTITHREAD, "false")
143
                b.getKeyValueParameters().put(ImportKeys.DEBUG, "true")
144
                b.getKeyValueParameters().put(ImportKeys.UPDATECORPUS, "false")
145

    
146
                XTZImport i = new XTZImport(b);
147
                i.start();
148
                
149
                println "Done: "+i.isSuccessful
150

    
151
                long end = System.currentTimeMillis()
152
                println "TIME: "+(end-start)
153
        }
154
}