Statistics
| Revision:

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

History | View | Annotate | Download (5.1 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
// STANDARD DECLARATIONS
5
package org.txm.macro.analec.edit
6

    
7
import org.kohsuke.args4j.*
8
import groovy.transform.Field
9
import org.txm.rcp.swt.widget.parameters.*
10
import org.txm.analec.*
11
import org.txm.searchengine.cqp.corpus.*
12
import visuAnalec.donnees.Structure;
13
import visuAnalec.elements.Schema
14
import visuAnalec.elements.Unite;
15
import visuAnalec.vue.Vue
16

    
17
if (!(corpusViewSelection instanceof MainCorpus)) {
18
        println "Corpora selection is not a Corpus"
19
        return;
20
}
21

    
22
// BEGINNING OF PARAMETERS
23
@Field @Option(name="unit_type",usage="", widget="String", required=true, def="MENTION")
24
String unit_type
25
@Field @Option(name="ref_property",usage="", widget="String", required=true, def="REF")
26
String ref_property
27
if (!ParametersDialog.open(this)) return;
28

    
29
int nCreated = 0 // count the number of created CHAINE
30
int nUpdated = 0 // count the number of updated CHAINE
31

    
32
MainCorpus corpus = corpusViewSelection
33
def analecCorpus = AnalecCorpora.getCorpus(corpus); // analec corpus has the same name has the TXM corpus
34
Structure structure = analecCorpus.getStructure()
35
if (!structure.getUnites().contains(unit_type)) { // check if the structure contains the unit_type units
36
        println "Error: corpus structure does not contains unit with name=$unit_type"
37
        return
38
}
39
if (!structure.getSchemas().contains("CHAINE")) { // update the structure if needed
40
        println "Creating the 'CHAINE' schema in the structure"
41
        analecCorpus.ajouterType(Schema.class, "CHAINE")
42
        analecCorpus.ajouterProp(Schema.class, "CHAINE", "REF")
43
        analecCorpus.ajouterProp(Schema.class, "CHAINE", "GENRE")
44
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "GENRE", "INDETERMINABLE")
45
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "GENRE", "FEMININ")
46
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "GENRE", "MASCULIN")
47
        analecCorpus.ajouterProp(Schema.class, "CHAINE", "NOMBRE")
48
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "NOMBRE", "GROUPE_FLOU")
49
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "NOMBRE", "GROUPE_STRICT")
50
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "NOMBRE", "SINGULIER")
51
        analecCorpus.ajouterProp(Schema.class, "CHAINE", "NB MAILLONS")
52
        analecCorpus.ajouterProp(Schema.class, "CHAINE", "TYPE REFERENT")
53
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "TYPE REFERENT", "UNKNOWN")
54
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "TYPE REFERENT", "CONCRET_OBJECT")
55
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "TYPE REFERENT", "ABSTRACT_OBJECT")
56
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "TYPE REFERENT", "TIME")
57
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "TYPE REFERENT", "PRODUCT")
58
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "TYPE REFERENT", "AMOUNT")
59
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "TYPE REFERENT", "EVENT")
60
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "TYPE REFERENT", "GPE")
61
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "TYPE REFERENT", "ORG")
62
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "TYPE REFERENT", "PERSON")
63
        analecCorpus.ajouterVal(Schema.class, "CHAINE", "TYPE REFERENT", "LIEU")
64
}
65

    
66
def props = structure.getUniteProperties(unit_type)
67
if (!props.contains(ref_property)) { // check the unit_type units have the REF property
68
        println "Error: $unit_type units have no proprerty named '$ref_property'"
69
        return
70
}
71

    
72
// parse the units to build CHAINES
73
def chaines = [:]
74
def units = analecCorpus.getUnites(unit_type)
75
units.sort() { a, b -> a.getDeb() <=> b.getDeb() ?: a.getFin() <=> b.getFin() }
76
for (Unite unit : units) {
77
        def ref = unit.getProp(ref_property)
78
        if (!chaines.containsKey(ref)) {
79
                chaines[ref] = []
80
        }
81
        chaines[ref] << unit
82
}
83

    
84
// update the already existing CHAINES schemas
85
for (Schema schema : analecCorpus.getSchemas("CHAINE")) {
86
        String ref = schema.getProp(ref_property)
87
        if (chaines.containsKey(ref)) { // the CHAINE exists 
88
                // maj des unités de la chaine existante
89
                int size_before = schema.getContenu().size()
90
                for (def unit : chaines[ref]) schema.ajouter(unit) // insert the new units in the hashset
91
                
92
                // Update the CHAINE size
93
                schema.props.put("NB MAILLONS", Integer.toString(schema.contenu.size())) 
94
                
95
                // remove the inserted CHAINE from 'chaines'
96
                chaines.remove(ref)
97
                if (size_before < schema.getContenu().size()) // if the size changed, then the CHAIEN have been updated
98
                        nUpdated++
99
        }
100
}
101

    
102
// create the remaining CHAINES schemas
103
for (def ref : chaines.keySet()) { // process the remaining CHAINE of 'chaines'
104
        nCreated++;
105
        Schema schema = new Schema()
106
        schema.type = "CHAINE"
107
        schema.props.put("REF", ref) 
108
        schema.props.put("GENRE", "") // set default values
109
        schema.props.put("NOMBRE", "") // set default values
110
        schema.props.put("NB MAILLONS", Integer.toString(chaines[ref].size())) 
111
        schema.props.put("TYPE REFERENT", "") // set default values
112
        
113
        for (def unit : chaines[ref]) schema.ajouter(unit) // insert the new units in the hashset
114
        
115
        analecCorpus.addSchemaLu(schema)  // add the new schema
116
}
117

    
118
if (nUpdated > 0 || nCreated > 0) corpus.setIsModified(true);
119
println "nUpdated=$nUpdated"
120
println "nCreated=$nCreated"