Statistics
| Revision:

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

History | View | Annotate | Download (6.5 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.macro.urs.AnalecUtils
19
import org.txm.searchengine.cqp.AbstractCqiClient
20
import org.txm.searchengine.cqp.ReferencePattern
21
import org.txm.searchengine.cqp.corpus.*
22
import org.txm.searchengine.cqp.corpus.query.CQLQuery
23

    
24
import visuAnalec.donnees.Structure
25
import visuAnalec.elements.*
26

    
27
def scriptName = this.class.getSimpleName()
28

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

    
35
if (selection.size() == 0) {
36
        println "** $scriptName: please select a Corpus or a Partition in the Corpus view: "+corpusViewSelections
37
        return false
38
}
39

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

    
64

    
65
def CQI = CQPSearchEngine.getCqiClient()
66

    
67
//corpus = corpusViewSelection
68
def allResults = [:]
69
for (def corpus : selection) {
70

    
71
        mainCorpus = corpus.getMainCorpus()
72

    
73
        def word = mainCorpus.getWordProperty()
74
        def analecCorpus = URSCorpora.getCorpus(mainCorpus.getName())
75

    
76
        def selectedUnits = AnalecUtils.selectUnitsInSchema(debug, analecCorpus, corpus, schema_ursql, minimum_schema_size, maximum_schema_size,
77
                        unit_ursql, limit_distance_in_schema, limit_cql, strict_inclusion, limit_distance);
78

    
79
        def n = 1
80

    
81
        allResults[corpus] = selectedUnits
82
}
83

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

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

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

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

    
146
                Concordance concordance = new Concordance(corpus);
147
                concordance.setParameters(new CQLQuery(""), [word],[word],[word],
148
                                [word],[word],[word],
149
                                new ReferencePattern(text_id), new ReferencePattern(text_id), 7, 10)
150

    
151
                if (monitor == null) {
152
                        println "Error: cannot open concordance editor: null monitor"
153
                        return
154
                }
155
                monitor.syncExec(new Runnable() {
156
                                        public void run() {
157
                                                try {
158
                                                        concordance.compute(fqr); // skip the CQI.query step
159
                                                        
160
                                                        ConcordanceEditor editor = ComputeConcordance.openEditor(concordance);
161
                                                        
162
                                                } catch(Throwable error) {
163
                                                        println "Error: "+error;
164
                                                        error.printStackTrace();
165
                                                }
166
                                        }
167
                                });
168
        }
169
}
170

    
171
return allResults