Statistics
| Revision:

root / tmp / org.txm.analec.rcp / src / org / txm / macro / urs / PremierMaillonMacro.groovy @ 1217

History | View | Annotate | Download (2.6 kB)

1 481 mdecorde
// Copyright © 2016 ENS de Lyon, CNRS, University of Franche-Comté
2 481 mdecorde
// Licensed under the terms of the GNU General Public License (http://www.gnu.org/licenses)
3 481 mdecorde
// @author mdecorde
4 481 mdecorde
// @author sheiden
5 481 mdecorde
// STANDARD DECLARATIONS
6 1217 mdecorde
package org.txm.macro.urs
7 481 mdecorde
8 481 mdecorde
import org.kohsuke.args4j.*
9 481 mdecorde
10 481 mdecorde
import groovy.transform.Field
11 481 mdecorde
12 671 mdecorde
import org.txm.rcp.swt.widget.parameters.*
13 1217 mdecorde
import org.txm.annotation.urs.*
14 481 mdecorde
import org.txm.searchengine.cqp.corpus.*
15 481 mdecorde
16 481 mdecorde
import visuAnalec.donnees.Structure;
17 481 mdecorde
import visuAnalec.elements.Unite;
18 481 mdecorde
import visuAnalec.vue.Vue
19 481 mdecorde
20 481 mdecorde
if (!(corpusViewSelection instanceof MainCorpus)) {
21 481 mdecorde
        println "Corpora selection is not a Corpus"
22 481 mdecorde
        return;
23 481 mdecorde
}
24 481 mdecorde
25 481 mdecorde
// BEGINNING OF PARAMETERS
26 481 mdecorde
@Field @Option(name="unit_type",usage="", widget="String", required=true, def="Maillon")
27 481 mdecorde
String unit_type
28 481 mdecorde
@Field @Option(name="reset",usage="", widget="Boolean", required=true, def="true")
29 481 mdecorde
boolean reset
30 481 mdecorde
31 481 mdecorde
if (!ParametersDialog.open(this)) return;
32 481 mdecorde
33 481 mdecorde
MainCorpus corpus = corpusViewSelection
34 1217 mdecorde
def analecCorpus = URSCorpora.getCorpus(corpus);
35 481 mdecorde
Structure structure = analecCorpus.getStructure()
36 481 mdecorde
if (!structure.getUnites().contains(unit_type)) { // check if the structure contains the unit_type units
37 481 mdecorde
        println "Error: corpus structure does not contains unit with name=$unit_type"
38 481 mdecorde
        return
39 481 mdecorde
}
40 481 mdecorde
def props = structure.getUniteProperties(unit_type)
41 481 mdecorde
42 481 mdecorde
String NEW = "NEW"
43 481 mdecorde
String REF = "REF"
44 481 mdecorde
String YES = "YES"
45 481 mdecorde
String NO = "NO"
46 481 mdecorde
if (!props.contains(NEW)) { // update the structure if needed
47 481 mdecorde
        analecCorpus.ajouterProp(Unite.class, unit_type, NEW);
48 481 mdecorde
        analecCorpus.ajouterVal(Unite.class, unit_type, NEW, "YES");
49 481 mdecorde
        analecCorpus.ajouterVal(Unite.class, unit_type, NEW, "NO");
50 481 mdecorde
}
51 481 mdecorde
52 481 mdecorde
if (!props.contains(REF)) { // check the unit_type units have the REF property
53 481 mdecorde
        println "Error: $unit_type units have no proprerty named 'REF'"
54 481 mdecorde
        return
55 481 mdecorde
}
56 481 mdecorde
57 481 mdecorde
int nIgnored = 0; // number of units ignored
58 481 mdecorde
int nYes = 0 // number of "YES" unit set
59 481 mdecorde
int nNo = 0 // number of "NO" unit set
60 481 mdecorde
61 481 mdecorde
def allRefs = new HashSet<String>() // store the references already seen, allow to set the 'NEW' property to 'YES' or 'NO'
62 481 mdecorde
def units = analecCorpus.getUnites(unit_type)
63 481 mdecorde
units.sort() { a, b -> a.getDeb() <=> b.getDeb() ?: a.getFin() <=> b.getFin() }
64 481 mdecorde
for (Unite unit : units) {
65 481 mdecorde
        def prop = unit.getProp(NEW);
66 481 mdecorde
        def ref = unit.getProp(REF);
67 481 mdecorde
        if (reset || prop == null || prop.length() == 0) {
68 481 mdecorde
                if (allRefs.contains(ref)) {
69 481 mdecorde
                        unit.getProps().put(NEW, NO)
70 481 mdecorde
                        nNo++
71 481 mdecorde
                } else { // this is the first MENTION of the CHAINE
72 481 mdecorde
                        allRefs << ref
73 481 mdecorde
                        unit.getProps().put(NEW, YES)
74 481 mdecorde
                        nYes++
75 481 mdecorde
                }
76 481 mdecorde
        } else {
77 481 mdecorde
                // nothing to do "NEW" already exists
78 481 mdecorde
                nIgnored++
79 481 mdecorde
        }
80 481 mdecorde
}
81 481 mdecorde
82 481 mdecorde
println "nIgnored=$nIgnored"
83 481 mdecorde
println "nYes=$nYes"
84 481 mdecorde
println "nNo=$nNo"