Statistics
| Revision:

root / tmp / org.txm.analec.rcp / src / org / txm / macroprototypes / urs / cqp / URSUnits2CQPWordsMacro.groovy @ 2087

History | View | Annotate | Download (2.7 kB)

1
package org.txm.macroprototypes.urs.cqp
2

    
3
import org.kohsuke.args4j.*
4
import groovy.transform.Field
5
import org.txm.annotation.urs.*
6
import org.txm.importer.ValidateXml
7
import org.txm.rcp.swt.widget.parameters.*
8
import org.txm.searchengine.cqp.corpus.*
9
import visuAnalec.elements.*
10

    
11
// BEGINNING OF PARAMETERS
12

    
13
// Open the parameters input dialog box
14
//if (!ParametersDialog.open(this)) return;
15

    
16
if (!(corpusViewSelection instanceof MainCorpus)) {
17
        println "Selection must be a Corpus"
18
        return
19
}
20

    
21
MainCorpus corpus = corpusViewSelection
22
def analecCorpus = URSCorpora.getCorpus(corpus)
23

    
24
def texts = corpus.getCorpusTextIdsList();
25
def texts_startlimits = corpus.getTextStartLimits()
26
def texts_endlimits = corpus.getTextEndLimits()
27

    
28
for (int i = 0 ; i < texts.size() ; i++) {
29
        
30
        println "Processing annotations of "+texts[i]+"..."
31

    
32
        def text_id = texts[i]
33
        def text_start = texts_startlimits[i]
34
        def text_end = texts_endlimits[i]
35

    
36
        File xmltxmFile = new File(corpus.getProject().getProjectDirectory(), "txm/"+corpus.getID()+"/"+text_id+".xml")
37
        File xmltxmFileCopy = new File(corpus.getProject().getProjectDirectory(), text_id+"_copy.xml")
38

    
39
        if (!xmltxmFile.exists()) {
40
                println "Warning: no text file found: "+xmltxmFile
41
                continue
42
        }
43
        
44
        for (String unit_type : analecCorpus.getStructure().getTypes(Unite.class)) {
45
                def corpus_units = []
46
                ArrayList<Unite> all_units = analecCorpus.getUnites(unit_type)
47
                corpus_units = all_units.findAll() {
48
                        "word".equals(it.getProp("type")) && text_start <= it.getDeb() && it.getFin() < text_end && !("true".equals(it.getProp("written")))
49
                }
50
                
51
                if (corpus_units.size() == 0) continue;
52
                
53
                try {
54
                        println "processing word Units ${text_id} and its units "+corpus_units.size()
55
                        WordUnitsInserter inserter = new WordUnitsInserter(corpus, xmltxmFile, corpus_units);
56
                        if (inserter.process(xmltxmFileCopy) && ValidateXml.test(xmltxmFileCopy)) {
57
                                xmltxmFile.delete()
58
                                xmltxmFileCopy.renameTo(xmltxmFile)
59

    
60
                                for (Unite unit : corpus_units) {
61
                                        unit.getProps()["written"] = "true"
62
                                }
63
                                URSCorpora.saveCorpus(corpus);
64
                                println "Done, "+corpus_units.size()+ " units written"
65

    
66
                        } else {
67
                                println "Error while processing milestones $xmltxmFile file"
68
                                File error = new File(corpus.getProject().getProjectDirectory(), "error/"+xmltxmFile.getName())
69
                                error.getParentFile().mkdirs()
70
                                println "        moving created file to $error"
71
                                error.delete()
72
                                xmltxmFileCopy.renameTo(error)
73
                        }
74
                } catch(Exception e) {
75
                        println "Error while processing milestones $xmltxmFile file: "+e
76
                        e.printStackTrace();
77
                        File error = new File(corpus.getProject().getProjectDirectory(), "error/"+xmltxmFile.getName())
78
                        error.getParentFile().mkdirs()
79
                        println "        moving created file to $error"
80
                        error.delete()
81
                        xmltxmFileCopy.renameTo(error)
82
                }
83
        }
84
}