Statistics
| Revision:

root / tmp / org.txm.analec.rcp / src / org / txm / macro / analec / edit / PremierMaillonMacro.groovy @ 1166

History | View | Annotate | Download (2.6 kB)

1
// Copyright © 2016 ENS de Lyon, CNRS, University of Franche-Comté
2
// Licensed under the terms of the GNU General Public License (http://www.gnu.org/licenses)
3
// @author mdecorde
4
// @author sheiden
5
// STANDARD DECLARATIONS
6
package org.txm.macro.analec.edit
7

    
8
import org.kohsuke.args4j.*
9

    
10
import groovy.transform.Field
11

    
12
import org.txm.rcp.swt.widget.parameters.*
13
import org.txm.analec.*
14
import org.txm.searchengine.cqp.corpus.*
15

    
16
import visuAnalec.donnees.Structure;
17
import visuAnalec.elements.Unite;
18
import visuAnalec.vue.Vue
19

    
20
if (!(corpusViewSelection instanceof MainCorpus)) {
21
        println "Corpora selection is not a Corpus"
22
        return;
23
}
24

    
25
// BEGINNING OF PARAMETERS
26
@Field @Option(name="unit_type",usage="", widget="String", required=true, def="Maillon")
27
String unit_type
28
@Field @Option(name="reset",usage="", widget="Boolean", required=true, def="true")
29
boolean reset
30

    
31
if (!ParametersDialog.open(this)) return;
32

    
33
MainCorpus corpus = corpusViewSelection
34
def analecCorpus = AnalecCorpora.getCorpus(corpus);
35
Structure structure = analecCorpus.getStructure()
36
if (!structure.getUnites().contains(unit_type)) { // check if the structure contains the unit_type units
37
        println "Error: corpus structure does not contains unit with name=$unit_type"
38
        return
39
}
40
def props = structure.getUniteProperties(unit_type)
41

    
42
String NEW = "NEW"
43
String REF = "REF"
44
String YES = "YES"
45
String NO = "NO"
46
if (!props.contains(NEW)) { // update the structure if needed
47
        analecCorpus.ajouterProp(Unite.class, unit_type, NEW);
48
        analecCorpus.ajouterVal(Unite.class, unit_type, NEW, "YES");
49
        analecCorpus.ajouterVal(Unite.class, unit_type, NEW, "NO");
50
}
51

    
52
if (!props.contains(REF)) { // check the unit_type units have the REF property
53
        println "Error: $unit_type units have no proprerty named 'REF'"
54
        return
55
}
56

    
57
int nIgnored = 0; // number of units ignored
58
int nYes = 0 // number of "YES" unit set
59
int nNo = 0 // number of "NO" unit set
60

    
61
def allRefs = new HashSet<String>() // store the references already seen, allow to set the 'NEW' property to 'YES' or 'NO'
62
def units = analecCorpus.getUnites(unit_type)
63
units.sort() { a, b -> a.getDeb() <=> b.getDeb() ?: a.getFin() <=> b.getFin() }
64
for (Unite unit : units) {
65
        def prop = unit.getProp(NEW);
66
        def ref = unit.getProp(REF);
67
        if (reset || prop == null || prop.length() == 0) {
68
                if (allRefs.contains(ref)) {
69
                        unit.getProps().put(NEW, NO)
70
                        nNo++
71
                } else { // this is the first MENTION of the CHAINE
72
                        allRefs << ref
73
                        unit.getProps().put(NEW, YES)
74
                        nYes++
75
                }
76
        } else {
77
                // nothing to do "NEW" already exists
78
                nIgnored++
79
        }
80
}
81

    
82
println "nIgnored=$nIgnored"
83
println "nYes=$nYes"
84
println "nNo=$nNo"