Statistics
| Revision:

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

History | View | Annotate | Download (3.6 kB)

1 671 mdecorde
// STANDARD DECLARATIONS
2 1217 mdecorde
package org.txm.macro.urs.edit
3 671 mdecorde
4 671 mdecorde
import org.kohsuke.args4j.*
5 671 mdecorde
import groovy.transform.Field
6 671 mdecorde
import org.txm.rcp.swt.widget.parameters.*
7 671 mdecorde
import org.txm.rcp.editors.concordances.*
8 1094 mdecorde
import org.txm.searchengine.cqp.corpus.CQPCorpus
9 671 mdecorde
import org.txm.searchengine.cqp.corpus.MainCorpus
10 671 mdecorde
import org.txm.functions.concordances.*
11 1217 mdecorde
import org.txm.annotation.urs.*
12 671 mdecorde
import org.txm.concordance.core.functions.Concordance
13 671 mdecorde
import org.txm.concordance.rcp.editors.ConcordanceEditor
14 671 mdecorde
import visuAnalec.elements.Unite
15 671 mdecorde
16 671 mdecorde
// BEGINNING OF PARAMETERS
17 671 mdecorde
18 671 mdecorde
@Field @Option(name="unit_type", usage="The unit type to create", widget="String", required=true, def="MENTION")
19 671 mdecorde
def unit_type
20 671 mdecorde
21 671 mdecorde
@Field @Option(name="create_only_if_new", usage="Create the unit if not already annotated", widget="Boolean", required=true, def="true")
22 671 mdecorde
def create_only_if_new
23 671 mdecorde
24 671 mdecorde
@Field @Option(name="prop", usage="prop", widget="String", required=true, def="REF")
25 671 mdecorde
def prop
26 671 mdecorde
27 671 mdecorde
@Field @Option(name="value", usage="default value", widget="String", required=true, def="NAME")
28 671 mdecorde
def value
29 671 mdecorde
30 671 mdecorde
// END OF PARAMETERS
31 671 mdecorde
32 671 mdecorde
// get a Concordance from 1) current Concordance editor or 2) CorporaView selection
33 671 mdecorde
Concordance concordance
34 671 mdecorde
if (editor instanceof ConcordanceEditor) {
35 671 mdecorde
        concordance = editor.getConcordance()
36 671 mdecorde
} else if (corpusViewSelection instanceof Concordance) {
37 671 mdecorde
        concordance = corpusViewSelection
38 671 mdecorde
} else {
39 671 mdecorde
        println "You must select a concordance or open a concordance result to run this macro."
40 671 mdecorde
        return false
41 671 mdecorde
}
42 671 mdecorde
43 671 mdecorde
if (concordance == null) {
44 671 mdecorde
        println "You must compute a concordance before."
45 671 mdecorde
        return
46 671 mdecorde
}
47 671 mdecorde
48 671 mdecorde
// check the analec corpus is ready
49 671 mdecorde
MainCorpus corpus = concordance.getCorpus().getMainCorpus();
50 1162 mdecorde
String name = corpus.getID()
51 1217 mdecorde
if (!URSCorpora.isAnnotationStructureReady(name)) {
52 671 mdecorde
        println "Annotation structure is not ready."
53 671 mdecorde
        return
54 671 mdecorde
}
55 671 mdecorde
56 671 mdecorde
// Open the parameters input dialog box
57 671 mdecorde
if (!ParametersDialog.open(this)) return;
58 671 mdecorde
59 671 mdecorde
// check the corpus structure has the unit_type provided
60 1217 mdecorde
def analecCorpus = URSCorpora.getCorpus(name)
61 671 mdecorde
if (!analecCorpus.getStructure().getUnites().contains(unit_type)) {
62 671 mdecorde
        //println "The corpus structure does not contains unit with type=$unit_type"
63 671 mdecorde
        //return;
64 671 mdecorde
        analecCorpus.getStructure().ajouterType(Unite.class, unit_type);
65 671 mdecorde
}
66 671 mdecorde
67 671 mdecorde
if (!analecCorpus.getStructure().getNomsProps(Unite.class, unit_type).contains(prop)) {
68 671 mdecorde
        //println "The corpus structure does not contains unit with type=$unit_type"
69 671 mdecorde
        //return;
70 671 mdecorde
        analecCorpus.getStructure().ajouterProp(Unite.class, unit_type, prop)
71 671 mdecorde
}
72 671 mdecorde
73 671 mdecorde
if (!analecCorpus.getStructure().getValeursProp(Unite.class, unit_type, prop).contains(value)) {
74 671 mdecorde
        //println "The corpus structure does not contains unit with type=$unit_type"
75 671 mdecorde
        //return;
76 671 mdecorde
        analecCorpus.getStructure().ajouterVal(Unite.class, unit_type, prop, value)
77 671 mdecorde
}
78 671 mdecorde
79 671 mdecorde
// browse lines and check
80 671 mdecorde
def units = analecCorpus.getUnites(unit_type)
81 671 mdecorde
def lines = concordance.getLines()
82 671 mdecorde
int iUnit = 0
83 671 mdecorde
int n = 0
84 671 mdecorde
for (int iLine = 0 ; iLine < lines.size() ; iLine++) {
85 671 mdecorde
        def line = lines[iLine]
86 671 mdecorde
        def m = line.getMatch()
87 671 mdecorde
        if (create_only_if_new && iUnit < units.size()) { // test only if create_only_if_new == true
88 671 mdecorde
                def unit = null
89 671 mdecorde
                while (iUnit < units.size() && units[iUnit].getDeb() < m.getStart()) {
90 671 mdecorde
91 671 mdecorde
                        if (iUnit < units.size()) {
92 671 mdecorde
                                unit = units[iUnit++]
93 671 mdecorde
                                if (unit.getDeb() == m.getStart() && unit.getFin() == m.getEnd()) { // skip and print the line
94 671 mdecorde
                                        println "skiping line: "+line.getViewRef().toString()+" "+line.keywordToString()
95 671 mdecorde
                                        continue
96 671 mdecorde
                                }
97 671 mdecorde
                        }
98 671 mdecorde
                }
99 671 mdecorde
        }
100 671 mdecorde
        n++
101 671 mdecorde
        def props = [:]
102 671 mdecorde
        props[prop] = value
103 671 mdecorde
        Unite u = analecCorpus.addUniteSaisie(unit_type, m.getStart(), m.getEnd(), props)
104 671 mdecorde
        //println "$props -> "+u.getProps()
105 671 mdecorde
}
106 671 mdecorde
println "$n $unit_type created."
107 671 mdecorde
if (n > 0) corpus.setIsModified(true);