Statistics
| Revision:

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

History | View | Annotate | Download (2.8 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

    
9
import groovy.transform.Field
10

    
11
import org.txm.rcp.swt.widget.parameters.*
12
import org.txm.annotation.urs.*
13
import org.txm.searchengine.cqp.corpus.*
14

    
15
import visuAnalec.donnees.Structure;
16
import visuAnalec.elements.Relation;
17
import visuAnalec.elements.Schema
18
import visuAnalec.elements.Unite;
19
import visuAnalec.vue.Vue
20

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

    
26
// BEGINNING OF PARAMETERS
27
@Field @Option(name="unit_type",usage="", widget="String", required=true, def="MENTION")
28
                String unit_type
29
@Field @Option(name="schema_type",usage="", widget="String", required=true, def="CHAINE")
30
                String schema_type
31
if (!ParametersDialog.open(this)) return;
32

    
33
int nCreated = 0 // count the number of created RELATION
34

    
35
MainCorpus corpus = corpusViewSelection
36
def analecCorpus = URSCorpora.getCorpus(corpus); // analec corpus has the same name has the TXM corpus
37
Structure structure = analecCorpus.getStructure()
38
if (!structure.getUnites().contains(unit_type)) { // check if the structure contains the unit_type units
39
        println "Error: corpus structure does not contains unit with name=$unit_type"
40
        return
41
}
42
if (!structure.getSchemas().contains(schema_type)) { // check if the structure contains the unit_type units
43
        println "Error: corpus structure does not contains schema with name=$schema_type"
44
        return
45
}
46
if (!structure.getRelations().contains("ANAPHORE")) { // update the structure if needed
47
        println "Creating the 'ANAPHORE' relation in the structure"
48
        structure.ajouterType(Relation.class, "ANAPHORE")
49
        analecCorpus.ajouterProp(Relation.class, "ANAPHORE", "TYPE")
50
        analecCorpus.ajouterVal(Relation.class, "ANAPHORE", "TYPE", "COREFERENTE")
51
        analecCorpus.ajouterVal(Relation.class, "ANAPHORE", "TYPE", "ASSOCIATIVE")
52
}
53
if (analecCorpus.getRelations("ANAPHORE").size() > 0) {
54
        println "Error: This macro can't update existing Relations"
55
        return
56
}
57

    
58
for (Schema schema : analecCorpus.getSchemas(schema_type)) { // parse all CHAINE
59
        def units = []
60
        for (Unite unit : schema.getUnitesSousjacentes()) { // keep only the 'unit_type' units
61
                if (unit.type.equals(unit_type)) units << unit
62
        }
63
        units.sort() { a, b -> a.getDeb() <=> b.getDeb() ?: a.getFin() <=> b.getFin() } // sort them
64
        
65
        for (int i = 0 ; i < units.size() - 1 ; i++) { // build RELATIONS and don't process the last unit
66
                println "creating "+units[i+1]+", "+units[i]
67
                Relation relation = new Relation("ANAPHORE", units[i+1], units[i])
68
                relation.getProps().put("TYPE", "COREFERENTE")
69
                analecCorpus.addRelationLue(relation)  // add the new relation
70
                nCreated++;
71
        }
72
}
73

    
74
println "nCreated=$nCreated"