Statistics
| Revision:

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

History | View | Annotate | Download (7.1 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.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.progression.core.chartsengine.jfreechart.themes.highcharts.renderers.ProgressionItemSelectionRenderer
20
import org.txm.progression.core.functions.Progression
21
import org.txm.rcp.swt.widget.parameters.*
22
import org.txm.annotation.urs.*
23
import org.txm.chartsengine.rcp.editors.ChartEditor
24
import org.txm.macro.urs.AnalecUtils
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 = URSCorpora.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
        query = new CQLQuery(query)
97
        int[] starts = new int[selectedUnits.size()];
98
        int[] ends = new int[selectedUnits.size()];
99
        def unitsinformations = []
100
        int n = 0;
101
        for (Unite unite : selectedUnits) {
102
                starts[n] = unite.getDeb()
103
                ends[n] = unite.getFin()
104
                unitsinformations << AnalecUtils.toString(CQI, word, unite);
105
                n++
106
        }
107
        def queryResult = new FakeQueryResult(corpus.getID(), corpus, query, starts, ends, null)
108
        queries << query
109
        queryResults << queryResult
110
        informations << unitsinformations
111

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

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

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

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

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

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

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