Statistics
| Revision:

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

History | View | Annotate | Download (3.6 kB)

1
// STANDARD DECLARATIONS
2
package org.txm.macro.analec.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.analec.*
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.getName()
51
if (!AnalecCorpora.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 = AnalecCorpora.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
int iUnit = 0
83
int n = 0
84
for (int iLine = 0 ; iLine < lines.size() ; iLine++) {
85
        def line = lines[iLine]
86
        def m = line.getMatch()
87
        if (create_only_if_new && iUnit < units.size()) { // test only if create_only_if_new == true
88
                def unit = null
89
                while (iUnit < units.size() && units[iUnit].getDeb() < m.getStart()) {
90

    
91
                        if (iUnit < units.size()) {
92
                                unit = units[iUnit++]
93
                                if (unit.getDeb() == m.getStart() && unit.getFin() == m.getEnd()) { // skip and print the line
94
                                        println "skiping line: "+line.getViewRef().toString()+" "+line.keywordToString()
95
                                        continue
96
                                }
97
                        }
98
                }
99
        }
100
        n++
101
        def props = [:]
102
        props[prop] = value
103
        Unite u = analecCorpus.addUniteSaisie(unit_type, m.getStart(), m.getEnd(), props)
104
        //println "$props -> "+u.getProps()
105
}
106
println "$n $unit_type created."
107
if (n > 0) corpus.setIsModified(true);