Statistics
| Revision:

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

History | View | Annotate | Download (4.9 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
// STANDARD DECLARATIONS
5 481 mdecorde
package org.txm.macro.analec
6 481 mdecorde
7 481 mdecorde
import org.kohsuke.args4j.*
8 481 mdecorde
import groovy.transform.Field
9 671 mdecorde
import org.txm.rcp.swt.widget.parameters.*
10 481 mdecorde
import org.txm.analec.*
11 481 mdecorde
import org.txm.searchengine.cqp.corpus.*
12 481 mdecorde
import visuAnalec.donnees.Structure;
13 481 mdecorde
import visuAnalec.elements.Schema
14 481 mdecorde
import visuAnalec.elements.Unite;
15 481 mdecorde
import visuAnalec.vue.Vue
16 481 mdecorde
17 481 mdecorde
if (!(corpusViewSelection instanceof MainCorpus)) {
18 481 mdecorde
        println "Corpora selection is not a Corpus"
19 481 mdecorde
        return;
20 481 mdecorde
}
21 481 mdecorde
22 481 mdecorde
// BEGINNING OF PARAMETERS
23 481 mdecorde
@Field @Option(name="unit_type",usage="", widget="String", required=true, def="MENTION")
24 481 mdecorde
String unit_type
25 481 mdecorde
if (!ParametersDialog.open(this)) return;
26 481 mdecorde
27 481 mdecorde
int nCreated = 0 // count the number of created CHAINE
28 481 mdecorde
int nUpdated = 0 // count the number of updated CHAINE
29 481 mdecorde
30 481 mdecorde
MainCorpus corpus = corpusViewSelection
31 1166 mdecorde
def analecCorpus = AnalecCorpora.getCorpus(corpus); // analec corpus has the same name has the TXM corpus
32 481 mdecorde
Structure structure = analecCorpus.getStructure()
33 481 mdecorde
if (!structure.getUnites().contains(unit_type)) { // check if the structure contains the unit_type units
34 481 mdecorde
        println "Error: corpus structure does not contains unit with name=$unit_type"
35 481 mdecorde
        return
36 481 mdecorde
}
37 481 mdecorde
if (!structure.getSchemas().contains("CHAINE")) { // update the structure if needed
38 481 mdecorde
        println "Creating the 'CHAINE' schema in the structure"
39 481 mdecorde
        analecCorpus.ajouterType(Schema.class, "CHAINE")
40 481 mdecorde
        analecCorpus.ajouterProp(Schema.class, "CHAINE", "REF")
41 481 mdecorde
        analecCorpus.ajouterProp(Schema.class, "CHAINE", "GENRE")
42 481 mdecorde
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "GENRE", "INDETERMINABLE")
43 481 mdecorde
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "GENRE", "FEMININ")
44 481 mdecorde
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "GENRE", "MASCULIN")
45 481 mdecorde
        analecCorpus.ajouterProp(Schema.class, "CHAINE", "NOMBRE")
46 481 mdecorde
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "NOMBRE", "GROUPE_FLOU")
47 481 mdecorde
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "NOMBRE", "GROUPE_STRICT")
48 481 mdecorde
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "NOMBRE", "SINGULIER")
49 481 mdecorde
        analecCorpus.ajouterProp(Schema.class, "CHAINE", "NB MAILLONS")
50 481 mdecorde
        analecCorpus.ajouterProp(Schema.class, "CHAINE", "TYPE REFERENT")
51 481 mdecorde
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "TYPE REFERENT", "UNKNOWN")
52 481 mdecorde
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "TYPE REFERENT", "CONCRET_OBJECT")
53 481 mdecorde
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "TYPE REFERENT", "ABSTRACT_OBJECT")
54 481 mdecorde
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "TYPE REFERENT", "TIME")
55 481 mdecorde
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "TYPE REFERENT", "PRODUCT")
56 481 mdecorde
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "TYPE REFERENT", "AMOUNT")
57 481 mdecorde
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "TYPE REFERENT", "EVENT")
58 481 mdecorde
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "TYPE REFERENT", "GPE")
59 481 mdecorde
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "TYPE REFERENT", "ORG")
60 481 mdecorde
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "TYPE REFERENT", "PERSON")
61 481 mdecorde
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "TYPE REFERENT", "LIEU")
62 481 mdecorde
}
63 481 mdecorde
64 481 mdecorde
def props = structure.getUniteProperties(unit_type)
65 481 mdecorde
String REF = "REF"
66 481 mdecorde
if (!props.contains(REF)) { // check the unit_type units have the REF property
67 481 mdecorde
        println "Error: $unit_type units have no proprerty named 'REF'"
68 481 mdecorde
        return
69 481 mdecorde
}
70 481 mdecorde
71 481 mdecorde
// parse the units to build CHAINES
72 481 mdecorde
def chaines = [:]
73 481 mdecorde
def units = analecCorpus.getUnites(unit_type)
74 481 mdecorde
units.sort() { a, b -> a.getDeb() <=> b.getDeb() ?: a.getFin() <=> b.getFin() }
75 481 mdecorde
for (Unite unit : units) {
76 481 mdecorde
        def ref = unit.getProp(REF)
77 481 mdecorde
        if (!chaines.containsKey(ref)) {
78 481 mdecorde
                chaines[ref] = []
79 481 mdecorde
        }
80 481 mdecorde
        chaines[ref] << unit
81 481 mdecorde
}
82 481 mdecorde
83 481 mdecorde
// update the already existing CHAINES schemas
84 481 mdecorde
for (Schema schema : analecCorpus.getSchemas("CHAINE")) {
85 481 mdecorde
        String ref = schema.getProp(REF)
86 481 mdecorde
        if (chaines.containsKey(ref)) { // the CHAINE exists
87 481 mdecorde
                // maj des unités de la chaine existante
88 481 mdecorde
                int size_before = schema.getContenu().size()
89 481 mdecorde
                for (def unit : chaines[ref]) schema.ajouter(unit) // insert the new units in the hashset
90 481 mdecorde
91 481 mdecorde
                // Update the CHAINE size
92 481 mdecorde
                schema.props.put("NB MAILLONS", Integer.toString(schema.contenu.size()))
93 481 mdecorde
94 481 mdecorde
                // remove the inserted CHAINE from 'chaines'
95 481 mdecorde
                chaines.remove(ref)
96 481 mdecorde
                if (size_before < schema.getContenu().size()) // if the size changed, then the CHAIEN have been updated
97 481 mdecorde
                        nUpdated++
98 481 mdecorde
        }
99 481 mdecorde
}
100 481 mdecorde
101 481 mdecorde
// create the remaining CHAINES schemas
102 481 mdecorde
for (def ref : chaines.keySet()) { // process the remaining CHAINE of 'chaines'
103 481 mdecorde
        nCreated++;
104 481 mdecorde
        Schema schema = new Schema()
105 481 mdecorde
        schema.type = "CHAINE"
106 481 mdecorde
        schema.props.put("REF", ref)
107 481 mdecorde
        schema.props.put("GENRE", "") // set default values
108 481 mdecorde
        schema.props.put("NOMBRE", "") // set default values
109 481 mdecorde
        schema.props.put("NB MAILLONS", Integer.toString(chaines[ref].size()))
110 481 mdecorde
        schema.props.put("TYPE REFERENT", "") // set default values
111 481 mdecorde
112 481 mdecorde
        for (def unit : chaines[ref]) schema.ajouter(unit) // insert the new units in the hashset
113 481 mdecorde
114 481 mdecorde
        analecCorpus.addSchemaLu(schema)  // add the new schema
115 481 mdecorde
}
116 481 mdecorde
117 481 mdecorde
println "nUpdated=$nUpdated"
118 481 mdecorde
println "nCreated=$nCreated"