Statistics
| Revision:

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

History | View | Annotate | Download (4.6 kB)

1
// STANDARD DECLARATIONS
2
package org.txm.macroprototypes.urs.cqp
3

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

    
12
// BEGINNING OF PARAMETERS
13

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

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

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

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

    
29
for (int i = 0 ; i < texts.size() ; i++) {
30

    
31
        println "Processing annotations of "+texts[i]+"..."
32

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

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

    
40
        if (!xmltxmFile.exists()) {
41
                println "Warning: no text file found: "+xmltxmFile
42
                continue
43
        }
44

    
45
        // WRITE MILESTONES UNITS
46

    
47
        println "-> MILESTONES UNITS"
48

    
49
        def units = []
50
        for (String unit_type : analecCorpus.getStructure().getTypes(Unite.class)) {
51
                def corpus_units = []
52
                ArrayList<Unite> all_units = analecCorpus.getUnites(unit_type)
53
                corpus_units = all_units.findAll() {
54
                        it.getProp("type") != null && it.getProp("type").trim().length() > 0 && "yes".equals(it.getProp("milestone")) && text_start <= it.getDeb() && it.getFin() < text_end && !("true".equals(it.getProp("written")))
55
                }
56
                units.addAll(corpus_units)
57
        }
58

    
59
        if (units.size()== 0) {
60
                println "No milestones to write"
61
        } else {
62
                try {
63
                        println "processing milestones Units ${text_id} and its units "+units.size()
64
                        MileStoneInserter inserter = new MileStoneInserter(corpus, xmltxmFile, units);
65
                        if (inserter.process(xmltxmFileCopy) && ValidateXml.test(xmltxmFileCopy)) {
66
                                xmltxmFile.delete()
67
                                xmltxmFileCopy.renameTo(xmltxmFile)
68

    
69
                                for (Unite unit : units) {
70
                                        unit.getProps()["written"] = "true"
71
                                }
72
                                URSCorpora.saveCorpus(corpus);
73
                                println "Done, "+units.size()+ " milestones written"
74

    
75
                        } else {
76
                                println "Error while processing milestones $xmltxmFile file"
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
                } catch(Exception e) {
84
                        println "Error while processing milestones $xmltxmFile file: "+e
85
                        File error = new File(corpus.getProject().getProjectDirectory(), "error/"+xmltxmFile.getName())
86
                        error.getParentFile().mkdirs()
87
                        println "        moving created file to $error"
88
                        error.delete()
89
                        xmltxmFileCopy.renameTo(error)
90
                }
91
        }
92
        // WRITE NON MILESTONES UNITS
93
        println "-> OTHER UNITS"
94

    
95
        for (String unit_type : analecCorpus.getStructure().getTypes(Unite.class)) {
96
                
97
                def corpus_units = []
98
                ArrayList<Unite> all_units = analecCorpus.getUnites(unit_type)
99
                corpus_units = all_units.findAll() {
100
                        it.getProp("type") != null && it.getProp("type").trim().length() > 0 && !"no".equals(it.getProp("milestone")) && text_start <= it.getDeb() && it.getFin() < text_end && !("true".equals(it.getProp("written")))
101
                }
102

    
103
                if (corpus_units.size() == 0) continue;
104
                
105
                println "processing Units ${text_id} and its $unit_type units "+corpus_units.size()
106
                UnitsInserter inserter2 = new UnitsInserter(corpus, xmltxmFile, corpus_units, unit_type);
107
                try {
108
                        if (inserter2.process(xmltxmFileCopy) && ValidateXml.test(xmltxmFileCopy)) {
109
                                xmltxmFile.delete()
110
                                xmltxmFileCopy.renameTo(xmltxmFile)
111

    
112
                                for (Unite unit : corpus_units) {
113
                                        unit.getProps()["written"] = "true"
114
                                }
115
                                URSCorpora.saveCorpus(corpus);
116
                                println "Done, "+corpus_units.size()+ " units written"
117

    
118
                        } else {
119
                                println "Error while processing milestones $xmltxmFile file"
120
                                File error = new File(corpus.getProject().getProjectDirectory(), "error/"+xmltxmFile.getName())
121
                                error.getParentFile().mkdirs()
122
                                println "        moving created file to $error"
123
                                error.delete()
124
                                xmltxmFileCopy.renameTo(error)
125
                        }
126
                } catch(Exception e) {
127
                        println "Error while processing milestones $xmltxmFile file: "+e
128
                        File error = new File(corpus.getProject().getProjectDirectory(), "error/"+xmltxmFile.getName())
129
                        error.getParentFile().mkdirs()
130
                        println "        moving created file to $error"
131
                        error.delete()
132
                        xmltxmFileCopy.renameTo(error)
133
                }
134
        }
135
}