Statistics
| Revision:

root / tmp / org.txm.analec.rcp / src / org / txm / macro / analec / misc / UnitsProgressionMacro.groovy @ 1094

History | View | Annotate | Download (7.2 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.analec.misc
7

    
8
import java.util.ArrayList;
9
import java.util.List;
10

    
11
import org.apache.commons.lang.StringUtils
12
import org.jfree.chart.JFreeChart
13
import org.jfree.chart.plot.XYPlot
14
import org.kohsuke.args4j.*
15

    
16
import groovy.transform.Field
17

    
18
import org.txm.Toolbox
19
import org.txm.macro.analec.AnalecUtils;
20
import org.txm.progression.core.chartsengine.jfreechart.themes.highcharts.renderers.ProgressionItemSelectionRenderer
21
import org.txm.progression.core.functions.Progression
22
import org.txm.rcp.swt.widget.parameters.*
23
import org.txm.analec.*
24
import org.txm.chartsengine.rcp.editors.ChartEditor
25
import org.txm.searchengine.cqp.AbstractCqiClient
26
import org.txm.searchengine.cqp.corpus.*
27
import org.txm.searchengine.cqp.corpus.query.Match;
28
import org.txm.searchengine.cqp.corpus.query.CQLQuery
29
import org.txm.rcp.Application
30
import org.txm.rcp.IImageKeys
31

    
32
import visuAnalec.donnees.Structure
33
import visuAnalec.elements.*
34

    
35
def scriptName = this.class.getSimpleName()
36
def parent
37
def selection = []
38
if (!(corpusViewSelection instanceof CQPCorpus)) {
39
        println "** $scriptName please select a Corpus to run the macro"
40
}
41
selection << corpusViewSelection
42
parent = corpusViewSelection
43

    
44
// BEGINNING OF PARAMETERS
45
@Field @Option(name="schema_ursql", usage="TYPE@PROP=VALUE", widget="String", required=false, 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="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")
54
                int limit_distance_in_schema
55
@Field @Option(name="limit_cql", usage="CQL to build structure limits", widget="Query", required=true, def="<div> [] expand to div")
56
                limit_cql
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="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")
60
                int limit_distance
61
@Field @Option(name="unit_property_display", usage="Unit property to count", widget="String", required=true, def="CATEGORIE")
62
                String unit_property_display
63
@Field @Option(name="struct_name", usage="Structure to display", widget="String", required=true, def="div")
64
                String struct_name
65
@Field @Option(name="struct_prop", usage="Structure property to display", widget="String", required=true, def="n")
66
                String struct_prop
67
@Field @Option(name="line_width", usage="line width", widget="Integer", required=true, def="1")
68
                int line_width = 2
69
@Field @Option(name="bande_width", usage="bande width", widget="Float", required=true, def="1.0f")
70
                float bande_width = 1.0f
71
@Field @Option(name="debug", usage="Show internal variable content", widget="StringArray", metaVar="OFF        ON        ALL        REALLY ALL", required=true, def="OFF")
72
                debug
73
if (!ParametersDialog.open(this)) return
74
if (debug == "OFF") debug = 0; else if (debug == "ON") debug = 1; else if (debug == "ALL") debug = 2 else if (debug == "REALLY ALL") debug = 3
75

    
76

    
77
        def CQI = CQPSearchEngine.getCqiClient()
78

    
79
def queries = []
80
def queryResults = []
81
def informations = []
82
for (def corpus : selection) {
83
        
84
        mainCorpus = corpus.getMainCorpus()
85

    
86
        def word = mainCorpus.getWordProperty()
87
        def analecCorpus = AnalecCorpora.getCorpus(mainCorpus.getName())
88

    
89
        def selectedUnits = AnalecUtils.selectUnitsInSchema(debug, analecCorpus, corpus, schema_ursql, minimum_schema_size, maximum_schema_size, 
90
        unit_ursql, limit_distance_in_schema, limit_cql, strict_inclusion, limit_distance);
91

    
92
        def query = ""
93
        if (limit_cql != null && !limit_cql.getQueryString().equals("\"\"")) query += limit_cql
94
        if (schema_ursql != null && schema_ursql.length() > 0) { if (query.length() > 0) query += " & "; query += ""+schema_ursql+ " >"}
95
        if (unit_ursql != null && unit_ursql.length() > 0) query += " "+unit_ursql
96
        if (query == "") query = corpus.getName()
97
        query = new CQLQuery(query)
98
        int[] starts = new int[selectedUnits.size()];
99
        int[] ends = new int[selectedUnits.size()];
100
        def unitsinformations = []
101
        int n = 0;
102
        for (Unite unite : selectedUnits) {
103
                starts[n] = unite.getDeb()
104
                ends[n] = unite.getFin()
105
                unitsinformations << AnalecUtils.toString(CQI, word, unite);
106
                n++
107
        }
108
        def queryResult = new FakeQueryResult(corpus.getName(), corpus, query, starts, ends, null)
109
        queries << query
110
        queryResults << queryResult
111
        informations << unitsinformations
112

    
113
        if (unit_property_display != null && unit_property_display.length() > 0) {
114
                def propvalues = [:]
115
                for (def unit : selectedUnits) {
116
                        def v = unit.getProp(unit_property_display)
117
                        if (v == null) v = "<null>"
118
                        else if (v.length() == 0) v = "<empty>"
119
                        
120
                        if (!propvalues.containsKey(v))propvalues[v] = []
121
                        propvalues[v] << unit
122
                }
123
                
124
                for (def v : propvalues.keySet().sort()) {
125
                        selectedUnits = propvalues[v]
126
                        query = corpus.getName()+" "+limit_cql
127
                        query = new CQLQuery(v)
128
                        starts = new int[selectedUnits.size()];
129
                        ends = new int[selectedUnits.size()];
130
                        unitsinformations = []
131
                        n = 0;
132
                        for (Unite unite : selectedUnits) {
133
                                starts[n] = unite.getDeb()
134
                                ends[n] = unite.getFin()
135
                                unitsinformations << AnalecUtils.toString(CQI, word, unite);
136
                                n++
137
                        }
138
                        queryResult = new FakeQueryResult(corpus.getName(), corpus, query, starts, ends, null)
139
                        queries << query
140
                        queryResults << queryResult
141
                        informations << unitsinformations
142
                }
143
        }
144
}
145

    
146
corpus = parent
147
try {
148
        def struct = corpus.getStructuralUnit(struct_name)
149
        def struct_p = struct.getProperty(struct_prop)
150

    
151
        Progression progression = new Progression(corpus, queries,
152
                        struct, struct_p,        ".*",
153
                        true, false, false,
154
                        line_width, false, bande_width)
155

    
156
        progression.stepQueries(queryResults); // new
157

    
158
        if (!progression.stepStructuralUnits() || monitor.isCanceled())        return
159
                monitor.worked(20)
160
        if (!progression.stepFinalize() || monitor.isCanceled()) return
161
                monitor.worked(20)
162

    
163
        monitor.syncExec(new Runnable() {
164
                                @Override
165
                                public void run() {
166
                                        try {
167
                                                ChartEditor charteditorpart = SWTChartsComponentProvider.openEditor(Application.swtComponentProvider.createProgressionChartEditorPart(IImageKeys.getImage(IImageKeys.ACTION_PROGRESSION), progression, progression.isMonochrome(), progression.isMonostyle(), progression.isDoCumulative()))
168
                                                JFreeChart chart = charteditorpart.getChart()
169
                                                def plot = chart.getXYPlot()
170
                                                ProgressionItemSelectionRenderer renderer = plot.getRenderer();
171
                                                renderer.setAdditionalLabelInformation(informations)
172
                                        } catch(Exception e) {e.printStackTrace()}
173
                                }
174
                        })
175

    
176
} catch(Exception e) {
177
        e.printStackTrace()
178
        return false
179
}