Statistics
| Revision:

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

History | View | Annotate | Download (4.9 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.urs
6

    
7
import org.kohsuke.args4j.*
8
import groovy.transform.Field
9
import org.txm.rcp.swt.widget.parameters.*
10
import org.txm.annotation.urs.*
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
if (!ParametersDialog.open(this)) return;
26

    
27
int nCreated = 0 // count the number of created CHAINE
28
int nUpdated = 0 // count the number of updated CHAINE
29

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

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

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

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

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

    
117
println "nUpdated=$nUpdated"
118
println "nCreated=$nCreated"