Statistics
| Revision:

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

History | View | Annotate | Download (2.8 kB)

1 671 mdecorde
// Copyright © 2016 ENS de Lyon, CNRS, University of Franche-Comté
2 671 mdecorde
// Licensed under the terms of the GNU General Public License (http://www.gnu.org/licenses)
3 671 mdecorde
// @author mdecorde
4 671 mdecorde
// STANDARD DECLARATIONS
5 1217 mdecorde
package org.txm.macro.urs.edit
6 671 mdecorde
7 671 mdecorde
import org.kohsuke.args4j.*
8 671 mdecorde
9 671 mdecorde
import groovy.transform.Field
10 671 mdecorde
11 671 mdecorde
import org.txm.rcp.swt.widget.parameters.*
12 1217 mdecorde
import org.txm.annotation.urs.*
13 671 mdecorde
import org.txm.searchengine.cqp.corpus.*
14 671 mdecorde
15 671 mdecorde
import visuAnalec.donnees.Structure;
16 671 mdecorde
import visuAnalec.elements.Relation;
17 671 mdecorde
import visuAnalec.elements.Schema
18 671 mdecorde
import visuAnalec.elements.Unite;
19 671 mdecorde
import visuAnalec.vue.Vue
20 671 mdecorde
21 671 mdecorde
if (!(corpusViewSelection instanceof MainCorpus)) {
22 671 mdecorde
        println "Corpora selection is not a Corpus"
23 671 mdecorde
        return;
24 671 mdecorde
}
25 671 mdecorde
26 671 mdecorde
// BEGINNING OF PARAMETERS
27 671 mdecorde
@Field @Option(name="unit_type",usage="", widget="String", required=true, def="MENTION")
28 671 mdecorde
                String unit_type
29 671 mdecorde
@Field @Option(name="schema_type",usage="", widget="String", required=true, def="CHAINE")
30 671 mdecorde
                String schema_type
31 671 mdecorde
if (!ParametersDialog.open(this)) return;
32 671 mdecorde
33 671 mdecorde
int nCreated = 0 // count the number of created RELATION
34 671 mdecorde
35 671 mdecorde
MainCorpus corpus = corpusViewSelection
36 1217 mdecorde
def analecCorpus = URSCorpora.getCorpus(corpus); // analec corpus has the same name has the TXM corpus
37 671 mdecorde
Structure structure = analecCorpus.getStructure()
38 671 mdecorde
if (!structure.getUnites().contains(unit_type)) { // check if the structure contains the unit_type units
39 671 mdecorde
        println "Error: corpus structure does not contains unit with name=$unit_type"
40 671 mdecorde
        return
41 671 mdecorde
}
42 671 mdecorde
if (!structure.getSchemas().contains(schema_type)) { // check if the structure contains the unit_type units
43 671 mdecorde
        println "Error: corpus structure does not contains schema with name=$schema_type"
44 671 mdecorde
        return
45 671 mdecorde
}
46 671 mdecorde
if (!structure.getRelations().contains("ANAPHORE")) { // update the structure if needed
47 671 mdecorde
        println "Creating the 'ANAPHORE' relation in the structure"
48 671 mdecorde
        structure.ajouterType(Relation.class, "ANAPHORE")
49 671 mdecorde
        analecCorpus.ajouterProp(Relation.class, "ANAPHORE", "TYPE")
50 671 mdecorde
        analecCorpus.ajouterVal(Relation.class, "ANAPHORE", "TYPE", "COREFERENTE")
51 671 mdecorde
        analecCorpus.ajouterVal(Relation.class, "ANAPHORE", "TYPE", "ASSOCIATIVE")
52 671 mdecorde
}
53 671 mdecorde
if (analecCorpus.getRelations("ANAPHORE").size() > 0) {
54 671 mdecorde
        println "Error: This macro can't update existing Relations"
55 671 mdecorde
        return
56 671 mdecorde
}
57 671 mdecorde
58 671 mdecorde
for (Schema schema : analecCorpus.getSchemas(schema_type)) { // parse all CHAINE
59 671 mdecorde
        def units = []
60 671 mdecorde
        for (Unite unit : schema.getUnitesSousjacentes()) { // keep only the 'unit_type' units
61 671 mdecorde
                if (unit.type.equals(unit_type)) units << unit
62 671 mdecorde
        }
63 671 mdecorde
        units.sort() { a, b -> a.getDeb() <=> b.getDeb() ?: a.getFin() <=> b.getFin() } // sort them
64 671 mdecorde
65 671 mdecorde
        for (int i = 0 ; i < units.size() - 1 ; i++) { // build RELATIONS and don't process the last unit
66 671 mdecorde
                println "creating "+units[i+1]+", "+units[i]
67 671 mdecorde
                Relation relation = new Relation("ANAPHORE", units[i+1], units[i])
68 671 mdecorde
                relation.getProps().put("TYPE", "COREFERENTE")
69 671 mdecorde
                analecCorpus.addRelationLue(relation)  // add the new relation
70 671 mdecorde
                nCreated++;
71 671 mdecorde
        }
72 671 mdecorde
}
73 671 mdecorde
74 671 mdecorde
println "nCreated=$nCreated"