Statistics
| Revision:

root / tmp / org.txm.analec.rcp / src / org / txm / macro / urs / exploit / SchemaLengthsMacro.groovy @ 2143

History | View | Annotate | Download (6.4 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

    
6
// STANDARD DECLARATIONS
7
package org.txm.macro.urs.exploit
8

    
9
import org.kohsuke.args4j.*
10
import groovy.transform.Field
11
import org.txm.rcp.swt.widget.parameters.*
12
import org.txm.annotation.urs.*
13
import org.txm.macro.urs.AnalecUtils
14
import visuAnalec.elements.*
15
import org.txm.searchengine.cqp.corpus.*
16
import org.txm.Toolbox
17
import org.txm.rcp.commands.*
18
import org.txm.statsengine.r.core.RWorkspace
19

    
20
def selection = []
21
for (def s : corpusViewSelections) {
22
        if (s instanceof CQPCorpus) selection << s
23
        else if (s instanceof Partition) selection.addAll(s.getParts())
24
}
25

    
26
if (selection.size() == 0) {
27
        println "** $scriptName: please select a Corpus or a Partition in the Corpus view: "+corpusViewSelections
28
        return false
29
} else {
30
        for (def c : selection) c.compute(false)
31
}
32

    
33
@Field @Option(name="schema_ursql", usage="TYPE@PROP=REGEX", widget="String", required=true, def="CHAINE")
34
                String schema_ursql
35

    
36
@Field @Option(name="minimum_schema_size", usage="minimal schema size", widget="Integer", required=true, def="3")
37
                int minimum_schema_size
38
@Field @Option(name="maximum_schema_size", usage="Maximum size needed to consider a schema", widget="Integer", required=true, def="9999999")
39
                int maximum_schema_size
40
@Field @Option(name="schema_property_display", usage="schema property to show", widget="String", required=true, def="REF")
41
                String schema_property_display
42
@Field @Option(name="unit_ursql", usage="TYPE@PROP=REGEX", widget="String", required=false, def="MENTION")
43
                String unit_ursql
44
@Field @Option(name="strict_inclusion", usage="Units must be strictly included into corpus matches", widget="Boolean", required=true, def="true")
45
                boolean strict_inclusion
46
@Field @Option(name="sep", usage="choose between the len or freq columns", widget="Separator", metaVar="freq        len", required=true, def="")
47
                def sep
48
@Field @Option(name="sort_column", usage="choose between the len or freq columns", widget="StringArray", metaVar="freq        len", required=true, def="freq")
49
                String sort_column
50
@Field @Option(name="max_lines", usage="cut the number of lines shown. '0' means no cut", widget="Integer", required=false, def="0")
51
                int max_lines
52
//@Field @Option(name="output_graph", usage="Show chart", widget="Boolean", required=true, def="false")
53
                output_graph = false
54
@Field @Option(name="debug", usage="Show internal variable content", widget="StringArray", metaVar="OFF        ON        ALL        REALLY ALL", required=true, def="OFF")
55
                debug
56

    
57
if (!ParametersDialog.open(this)) return;
58
if (debug == "OFF") debug = 0; else if (debug == "ON") debug = 1; else if (debug == "ALL") debug = 2 else if (debug == "REALLY ALL") debug = 3
59

    
60
for (def corpus : selection) {
61
        def analecCorpus = URSCorpora.getCorpus(corpus)
62

    
63
        def errors = AnalecUtils.isPropertyDefined(Schema.class, analecCorpus, schema_ursql)
64
        if (errors.size() > 0) {
65
                println "** The $schema_ursql schema URSQL cannot be computed in the corpus with types: $errors."
66
                return;
67
        }
68

    
69
        errors=AnalecUtils.isPropertyDefined(Unite.class, analecCorpus, unit_ursql)
70
        if (errors.size() > 0) {
71
                println "** $unit_ursql unit URSQL cannot be computed in the corpus with types: $errors."
72
                return;
73
        }
74

    
75
        def schemas = AnalecUtils.selectSchemasInCorpus(debug, analecCorpus, corpus, schema_ursql, minimum_schema_size, maximum_schema_size);
76

    
77
        int nSchemas = 0;
78

    
79
        def lens = [:]
80
        def lensnames = [:]
81
        for (def schema : schemas) {
82

    
83
                def allUnites = schema.getUnitesSousjacentesNonTriees()
84
                
85
                def units = AnalecUtils.filterElements(debug, allUnites, unit_ursql)
86
                
87
                selectedUnits = AnalecUtils.filterUniteByInclusion(debug, units, corpus.getMatches(), strict_inclusion, 0)
88
                
89
                int nUnites = selectedUnits.size();
90

    
91
                if (!lens.containsKey(nUnites)) {
92
                        lens[nUnites] = 0;
93
                        lensnames[nUnites] = [];
94
                }
95

    
96
                lens[nUnites] = lens[nUnites] + 1;
97
                String value = schema.getProp(schema_property_display);
98
                if (value.contains(",")) value = "\"$value\""
99
                lensnames[nUnites] << value
100
                nSchemas++;
101
        }
102

    
103
        //println "nSchemas=$nSchemas"
104
        def freqs = lens.keySet();
105
        freqs.sort();
106
        int t = 0;
107
        int n = 0;
108
        //println "Fréquences ("+freqs.size()+")"
109
        for (def f : freqs) {
110
                t += f * lens[f]
111
                n += lens[f]
112
        }
113

    
114
        if (n == 0) {
115
                println "No units selected for schemas=$schema_ursql and units=$unit_ursql"
116
                return false;
117
        }
118
        
119
        coef = (t/n)
120
        
121
        def slens = null
122
        if ("freq".equals(sort_column)) {
123
                slens = lens.sort { a, b -> -a.value <=> -b.value ?: -a.key <=> -b.key }
124
        } else {
125
                slens = lens.sort { a, b -> -a.key <=> -b.key ?: -a.value <=> -b.value }
126
        }
127
        
128
        def flens = []
129
        slens.each { key, value -> value.times { flens << key } }
130
        def nbins = flens.size()*2
131

    
132
        def cfreq = 0
133
        println "Schema lengths in '$corpus'. The average schema length is $t/$n = "+coef
134
        println "Index:\nlen\tfreq\tcfreq"
135
        int nShown = 0;
136
        for( def it : slens) {
137
                println it.key+"        "+it.value+"        "+(cfreq+=it.value)+"        "+lensnames[it.key].join(", ")
138
                nShown++
139
                if (max_lines > 0 && nShown >= max_lines) {
140
                        println "... (${slens.size() - max_lines})"
141
                        break;
142
                }
143
        }
144

    
145
        def slens2 = slens.sort { a, b -> -a.key <=> -b.key }
146
        if (output_graph) {
147
                def r = RWorkspace.getRWorkspaceInstance()
148

    
149
                r.addVectorToWorkspace("len", slens2.keySet() as int[])
150
                r.addVectorToWorkspace("freq", slens2.values() as int[])
151
                r.addVectorToWorkspace("flen", flens as int[])
152

    
153
                def corpusName = corpus.getName()
154
                println "corpusName=$corpusName"
155
                def PNGFile = File.createTempFile("txm_"+corpus.getID()+"_", ".png", new File(Toolbox.getTxmHomePath(), "results"))
156
                def PNGFilePath = PNGFile.getAbsolutePath()
157
                println "PNG file: "+PNGFilePath
158

    
159
                def SVGFile = File.createTempFile("txm_"+corpus.getID()+"_", ".svg", new File(Toolbox.getTxmHomePath(), "results"))
160
                def SVGFilePath = SVGFile.getAbsolutePath()
161
                println "SVG file: "+SVGFilePath
162

    
163
                /// BEGINNING OF R SCRIPT
164
                def script ="""
165
hist(flen, xaxt='n', col="gray", xlab="Length", breaks=$nbins, main="$corpusName Longueur des chaînes ($nbins bins)")
166
axis(side=1, at=len)
167
dev.off()
168
"""
169
                /// END OF R SCRIPT
170

    
171
                // execute R script
172
                r.plot(PNGFile, "png(file = \"${PNGFilePath}\"); "+script)
173
                r.plot(SVGFile, "svg(file = \"${SVGFilePath}\"); "+script)
174

    
175
                //display the SVG results graphic
176
                monitor.syncExec(new Runnable() {
177
                                        @Override
178
                                        public void run() { OpenBrowser.openfile(SVGFile.getAbsolutePath(), corpusName+" Longueur des chaînes") }
179
                                })
180
        }
181
        //return ["result":coef, "data":lens]
182
}
183

    
184