Statistics
| Revision:

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

History | View | Annotate | Download (2.8 kB)

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

    
4
import org.kohsuke.args4j.*
5

    
6
import groovy.transform.Field
7

    
8
import org.txm.rcp.swt.widget.parameters.*
9
import org.txm.annotation.urs.*
10
import org.txm.concordance.core.functions.*
11
import org.txm.concordance.rcp.editors.ConcordanceEditor;
12

    
13
// BEGINNING OF PARAMETERS
14

    
15
@Field @Option(name="unit_type", usage="The unit type to create", widget="String", required=true, def="MENTION")
16
def unit_type
17

    
18
@Field @Option(name="create_only_if_new", usage="Create the unit if not already annotated", widget="Boolean", required=true, def="true")
19
def create_only_if_new
20

    
21
@Field @Option(name="type", usage="type", widget="String", required=true, def="true")
22
def type
23

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

    
27
// END OF PARAMETERS
28

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

    
40
if (concordance == null) {
41
        println "You must compute a concordance before."
42
        return
43
}
44

    
45
// check the analec corpus is ready
46
def corpus = concordance.getCorpus();
47
String name = corpus.getMainCorpus().getName()
48
if (!URSCorpora.isAnnotationStructureReady(name)) {
49
        println "Annotation structure is not ready."
50
        return
51
}
52

    
53
// Open the parameters input dialog box
54
if (!ParametersDialog.open(this)) return
55

    
56
// check the corpus structure has the unit_type provided
57
def analecCorpus = URSCorpora.getCorpus(name)
58
if (!analecCorpus.getStructure().getUnites().contains(unit_type)) {
59
        println "The corpus structure does not contains unit with type=$unit_type"
60
        return;
61
}
62

    
63
// browse lines and check 
64
def units = analecCorpus.getUnites(unit_type)
65
def lines = concordance.getLines()
66
int iUnit = 0
67
int n = 0
68
for (int iLine = 0 ; iLine < lines.size() ; iLine++) { 
69
        def line = lines[iLine]
70
        def m = line.getMatch()
71
        if (create_only_if_new && iUnit < units.size()) { // test only if create_only_if_new == true
72
                def unit = null
73
                while (iUnit < units.size() && units[iUnit].getDeb() < m.getStart()) {
74
                        
75
                
76
                if (iUnit < units.size()) {
77
                        unit = units[iUnit++]
78
                        if (unit.getDeb() == m.getStart() && unit.getFin() == m.getEnd()) { // skip and print the line
79
                                println "skiping line: "+line.getViewRef().toString()+" "+line.keywordToString()
80
                                // line.getViewRef().toString()
81
                                // line.leftContextToString()
82
                                // line.keywordToString()
83
                                // line.rightContextToString()
84
                                continue
85
                        }
86
                }
87
                }
88
        }
89
        n++
90
        analecCorpus.addUniteSaisie(unit_type, m.getStart(), m.getEnd(), [type:value])
91
}
92
println "$n $unit_type created."