Statistics
| Revision:

root / tmp / org.txm.analec.rcp / src / org / txm / macro / urs / edit / ConcordanceToUnitMacro.groovy @ 1726

History | View | Annotate | Download (3.7 kB)

1
// STANDARD DECLARATIONS
2
package org.txm.macro.urs.edit
3

    
4
import org.kohsuke.args4j.*
5
import groovy.transform.Field
6
import org.txm.rcp.swt.widget.parameters.*
7
import org.txm.rcp.editors.concordances.*
8
import org.txm.searchengine.cqp.corpus.CQPCorpus
9
import org.txm.searchengine.cqp.corpus.MainCorpus
10
import org.txm.functions.concordances.*
11
import org.txm.annotation.urs.*
12
import org.txm.concordance.core.functions.Concordance
13
import org.txm.concordance.rcp.editors.ConcordanceEditor
14
import visuAnalec.elements.Unite
15

    
16
// BEGINNING OF PARAMETERS
17

    
18
@Field @Option(name="unit_type", usage="The unit type to create", widget="String", required=true, def="MENTION")
19
def unit_type
20

    
21
@Field @Option(name="create_only_if_new", usage="Create the unit if not already annotated", widget="Boolean", required=true, def="true")
22
def create_only_if_new
23

    
24
@Field @Option(name="prop", usage="prop", widget="String", required=true, def="REF")
25
def prop
26

    
27
@Field @Option(name="value", usage="default value", widget="String", required=true, def="NAME")
28
def value
29

    
30
// END OF PARAMETERS
31

    
32
// get a Concordance from 1) current Concordance editor or 2) CorporaView selection
33
Concordance concordance
34
if (editor instanceof ConcordanceEditor) {
35
        concordance = editor.getConcordance()
36
} else if (corpusViewSelection instanceof Concordance) {
37
        concordance = corpusViewSelection
38
} else {
39
        println "You must select a concordance or open a concordance result to run this macro."
40
        return false
41
}
42

    
43
if (concordance == null) {
44
        println "You must compute a concordance before."
45
        return
46
}
47

    
48
// check the analec corpus is ready
49
MainCorpus corpus = concordance.getCorpus().getMainCorpus();
50
String name = corpus.getID()
51
if (!URSCorpora.isAnnotationStructureReady(name)) {
52
        println "Annotation structure is not ready."
53
        return
54
}
55

    
56
// Open the parameters input dialog box
57
if (!ParametersDialog.open(this)) return;
58

    
59
// check the corpus structure has the unit_type provided
60
def analecCorpus = URSCorpora.getCorpus(name)
61
if (!analecCorpus.getStructure().getUnites().contains(unit_type)) {
62
        //println "The corpus structure does not contains unit with type=$unit_type"
63
        //return;
64
        analecCorpus.getStructure().ajouterType(Unite.class, unit_type);
65
}
66

    
67
if (!analecCorpus.getStructure().getNomsProps(Unite.class, unit_type).contains(prop)) {
68
        //println "The corpus structure does not contains unit with type=$unit_type"
69
        //return;
70
        analecCorpus.getStructure().ajouterProp(Unite.class, unit_type, prop)
71
}
72

    
73
if (!analecCorpus.getStructure().getValeursProp(Unite.class, unit_type, prop).contains(value)) {
74
        //println "The corpus structure does not contains unit with type=$unit_type"
75
        //return;
76
        analecCorpus.getStructure().ajouterVal(Unite.class, unit_type, prop, value)
77
}
78

    
79
// browse lines and check
80
def units = analecCorpus.getUnites(unit_type)
81
def lines = concordance.getLines()
82

    
83
int n = 0
84
for (int iLine = 0 ; iLine < lines.size() ; iLine++) {
85
        int iUnit = 0
86
        def line = lines[iLine]
87
        def m = line.getMatch()
88
        def do_create = true
89
        if (create_only_if_new && iUnit < units.size()) { // test only if create_only_if_new == true
90
                def unit = null
91
                //TODO don't iterates over all units
92
                while (iUnit < units.size() ) { //&& units[iUnit].getDeb() < m.getStart()) {
93
                        if (iUnit < units.size()) {
94
                                unit = units[iUnit++]
95
                                if (unit.getDeb() == m.getStart() && unit.getFin() == m.getEnd()) { // skip and print the line
96
                                        println "skiping line: "+line.getViewRef().toString()+" "+line.keywordToString()
97
                                        do_create = false
98
                                        continue
99
                                }
100
                        }
101
                }
102
        }
103
        if (do_create) {
104
                n++
105
                def props = [:]
106
                props[prop] = value
107
                Unite u = analecCorpus.addUniteSaisie(unit_type, m.getStart(), m.getEnd(), props)
108
                //        println "$props -> "+u.getProps()
109
        }
110
}
111
println "$n $unit_type created."
112
if (n > 0) corpus.setIsModified(true);