Statistics
| Revision:

root / tmp / org.txm.analec.rcp / src / org / txm / macro / urs / exploit / UnitsListMacro.groovy @ 2105

History | View | Annotate | Download (6.7 kB)

1
// Copyright © 2016 ENS de Lyon, CNRS, University of Franche-Comté
2
// Licensed under the terms of the GNU General Public License (http://www.gnu.org/licenses)
3
// @author mdecorde
4
// @author sheiden
5
// STANDARD DECLARATIONS
6
package org.txm.macro.urs.exploit
7

    
8
import org.apache.commons.lang.StringUtils
9
import org.kohsuke.args4j.*
10

    
11
import groovy.transform.Field
12

    
13
import org.txm.Toolbox
14
import org.txm.rcp.swt.widget.parameters.*
15
import org.txm.annotation.urs.*
16
import org.txm.concordance.core.functions.Concordance
17
import org.txm.concordance.rcp.editors.ConcordanceEditor
18
import org.txm.concordance.rcp.handlers.*
19
import org.txm.macro.urs.AnalecUtils
20
import org.txm.searchengine.cqp.AbstractCqiClient
21
import org.txm.searchengine.cqp.ReferencePattern
22
import org.txm.searchengine.cqp.corpus.*
23
import org.txm.searchengine.cqp.corpus.query.CQLQuery
24
import org.txm.searchengine.cqp.CQPSearchEngine
25

    
26
import visuAnalec.donnees.Structure
27
import visuAnalec.elements.*
28

    
29
def scriptName = this.class.getSimpleName()
30

    
31
def selection = []
32
for (def s : corpusViewSelections) {
33
        if (s instanceof CQPCorpus) selection << s
34
        else if (s instanceof Partition) selection.addAll(s.getParts())
35
}
36

    
37
if (selection.size() == 0) {
38
        println "** $scriptName: please select a Corpus or a Partition in the Corpus view: "+corpusViewSelections
39
        return false
40
} else {
41
        for (def c : selection) c.compute(false)
42
}
43

    
44
// BEGINNING OF PARAMETERS
45
@Field @Option(name="schema_ursql", usage="TYPE@PROP=VALUE", widget="String", required=true, def="CHAINE")
46
                String schema_ursql
47
@Field @Option(name="minimum_schema_size", usage="Minimum size needed to consider a schema", widget="Integer", required=true, def="3")
48
                int minimum_schema_size
49
@Field @Option(name="maximum_schema_size", usage="Maximum size needed to consider a schema", widget="Integer", required=true, def="9999999")
50
                int maximum_schema_size
51
@Field @Option(name="unit_ursql", usage="TYPE@PROP=VALUE", widget="String", required=false, def="MENTION")
52
                String unit_ursql
53
@Field @Option(name="position_in_schema", usage="Unit distance in schema (0 = no selection, 1 = first after limit, -1 = last before limit, etc.)", widget="Integer", required=true, def="0")
54
                int position_in_schema
55
@Field @Option(name="cql_limit", usage="CQL to build structure limits", widget="Query", required=false, def="")
56
                cql_limit
57
@Field @Option(name="strict_inclusion", usage="Units must be strictly included into corpus matches", widget="Boolean", required=true, def="true")
58
                boolean strict_inclusion
59
@Field @Option(name="position_in_matches", usage="Unit distance to structure limit (0 = no selection, 1 = first after limit, -1 = last before limit, etc.)", widget="Integer", required=true, def="0")
60
                int position_in_matches
61
@Field @Option(name="output_mode", usage="If selected units properties and words are shown", widget="StringArray", metaVar="FORMATED        TABULATED        COUNT        CQL        CONCORDANCE", required=true, def="FORMATED")
62
                output_mode
63
@Field @Option(name="debug", usage="Show internal variable content", widget="StringArray", metaVar="OFF        ON        ALL        REALLY ALL", required=true, def="OFF")
64
                debug
65
if (!ParametersDialog.open(this)) return;
66
if (debug == "OFF") debug = 0; else if (debug == "ON") debug = 1; else if (debug == "ALL") debug = 2 else if (debug == "REALLY ALL") debug = 3
67

    
68

    
69
def CQI = CQPSearchEngine.getCqiClient()
70

    
71
//corpus = corpusViewSelection
72
def allResults = [:]
73
for (def corpus : selection) {
74

    
75
        mainCorpus = corpus.getMainCorpus()
76

    
77
        def word = mainCorpus.getWordProperty()
78
        def analecCorpus = URSCorpora.getCorpus(mainCorpus)
79

    
80
        def selectedUnits = AnalecUtils.selectUnitsInSchema(debug, analecCorpus, corpus, schema_ursql, minimum_schema_size, maximum_schema_size,
81
                        unit_ursql, position_in_schema, cql_limit, strict_inclusion, position_in_matches);
82

    
83
        def n = 1
84

    
85
        allResults[corpus] = selectedUnits
86
}
87

    
88
if (output_mode == "FORMATED") {
89
        for (def corpus : allResults.keySet()) {
90
                def mainCorpus = corpus.getMainCorpus()
91
                def word = mainCorpus.getWordProperty()
92
                def selectedUnits = allResults[corpus]
93
                println "$corpus units: "+selectedUnits.size()
94
                def n = 1
95
                for (def unit : selectedUnits) {
96
                        int[] pos = null
97
                        if (unit.getDeb() == unit.getFin()) pos = [unit.getDeb()]
98
                        else pos = (unit.getDeb()..unit.getFin())
99
                        def form = StringUtils.join(CQI.cpos2Str(word.getQualifiedName(), pos), " ")
100

    
101
                        def props = unit.getProps().sort()
102
                        println sprintf("#%4d, %d-%d, \"%s\" %s", n, unit.getDeb(), unit.getFin(), form, props)
103
                        n++
104
                }
105
        }
106
} else if (output_mode == "TABULATED") {
107
        for (def corpus : allResults.keySet()) {
108
                def mainCorpus = corpus.getMainCorpus()
109
                def word = mainCorpus.getWordProperty()
110
                def selectedUnits = allResults[corpus]
111
                println "$corpus units: "+selectedUnits.size()
112
                def n = 1
113
                for (def unit : selectedUnits) {
114
                        int[] pos = null
115
                        if (unit.getDeb() == unit.getFin()) pos = [unit.getDeb()]
116
                        else pos = (unit.getDeb()..unit.getFin())
117
                        def form = StringUtils.join(CQI.cpos2Str(word.getQualifiedName(), pos), " ")
118

    
119
                        def props = unit.getProps().sort()
120
                        println sprintf("#%4d\t%d\t%d\t%s\t%s", n, unit.getDeb(), unit.getFin(), form, props)
121
                        n++
122
                }
123
        }
124
} else if (output_mode == "COUNT") {
125
        for (def corpus : allResults.keySet()) {
126
                def selectedUnits = allResults[corpus]
127
                println "$corpus units: "+selectedUnits.size()
128
        }
129
} else if (output_mode == "CQL") {
130
        for (def corpus : allResults.keySet()) {
131
                def selectedUnits = allResults[corpus]
132
                println "$corpus units: "+selectedUnits.size()
133
                println AnalecUtils.getCQL(corpus.getID(), selectedUnits)
134
        }
135
} else if (output_mode == "CONCORDANCE") {
136

    
137
        for (def corpus : allResults.keySet()) {
138
                def word = corpus.getProperty("word")
139
                def text_id = corpus.getStructuralUnit("text").getProperty("id")
140
                def selectedUnits = allResults[corpus]
141
                def query = ""
142
                if (schema_ursql != null) query+= " $schema_ursql"
143
                if (unit_ursql != null) query+= " $unit_ursql"
144
                def ret  = AnalecUtils.getStartsEndsTargetsArrays(selectedUnits)
145
                def starts = ret[0]
146
                def ends = ret[1]
147
                def targets = ret[2]
148
                FakeQueryResult fqr = new FakeQueryResult("Fake", corpus, new CQLQuery(query), starts, ends, targets)
149

    
150
                Concordance concordance = new Concordance(corpus);
151
                concordance.setParameters(new CQLQuery(""), [word],[word],[word],
152
                [word],[word],[word],
153
                new ReferencePattern(text_id), new ReferencePattern(text_id), 7, 10)
154
                concordance.setQueryResult(fqr)
155

    
156
                if (monitor == null) {
157
                        println "Error: cannot open concordance editor: null monitor"
158
                        return
159
                }
160
                monitor.syncExec(new Runnable() {
161
                                        public void run() {
162
                                                try {
163
                                                        concordance.compute(); // skip the CQI.query step
164

    
165
                                                        ConcordanceEditor editor = ComputeConcordance.openEditor(concordance);
166

    
167
                                                } catch(Throwable error) {
168
                                                        println "Error: "+error;
169
                                                        error.printStackTrace();
170
                                                }
171
                                        }
172
                                });
173
        }
174
}
175

    
176
return allResults