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 671 mdecorde
// Copyright © 2016 ENS de Lyon, CNRS, University of Franche-Comté
2 671 mdecorde
// Licensed under the terms of the GNU General Public License (http://www.gnu.org/licenses)
3 671 mdecorde
// @author mdecorde
4 671 mdecorde
// @author sheiden
5 671 mdecorde
6 671 mdecorde
// STANDARD DECLARATIONS
7 2082 mdecorde
package org.txm.macro.urs.exploit
8 671 mdecorde
9 671 mdecorde
import org.kohsuke.args4j.*
10 671 mdecorde
import groovy.transform.Field
11 671 mdecorde
import org.txm.rcp.swt.widget.parameters.*
12 1217 mdecorde
import org.txm.annotation.urs.*
13 1968 mdecorde
import org.txm.macro.urs.AnalecUtils
14 1968 mdecorde
import visuAnalec.elements.*
15 671 mdecorde
import org.txm.searchengine.cqp.corpus.*
16 671 mdecorde
import org.txm.Toolbox
17 671 mdecorde
import org.txm.rcp.commands.*
18 1968 mdecorde
import org.txm.statsengine.r.core.RWorkspace
19 671 mdecorde
20 2105 mdecorde
def selection = []
21 2105 mdecorde
for (def s : corpusViewSelections) {
22 2105 mdecorde
        if (s instanceof CQPCorpus) selection << s
23 2105 mdecorde
        else if (s instanceof Partition) selection.addAll(s.getParts())
24 1968 mdecorde
}
25 1968 mdecorde
26 2105 mdecorde
if (selection.size() == 0) {
27 2105 mdecorde
        println "** $scriptName: please select a Corpus or a Partition in the Corpus view: "+corpusViewSelections
28 2105 mdecorde
        return false
29 2105 mdecorde
} else {
30 2105 mdecorde
        for (def c : selection) c.compute(false)
31 2105 mdecorde
}
32 2105 mdecorde
33 1962 mdecorde
@Field @Option(name="schema_ursql", usage="TYPE@PROP=REGEX", widget="String", required=true, def="CHAINE")
34 2105 mdecorde
                String schema_ursql
35 671 mdecorde
36 1962 mdecorde
@Field @Option(name="minimum_schema_size", usage="minimal schema size", widget="Integer", required=true, def="3")
37 2105 mdecorde
                int minimum_schema_size
38 2099 mdecorde
@Field @Option(name="maximum_schema_size", usage="Maximum size needed to consider a schema", widget="Integer", required=true, def="9999999")
39 2105 mdecorde
                int maximum_schema_size
40 1962 mdecorde
@Field @Option(name="schema_property_display", usage="schema property to show", widget="String", required=true, def="REF")
41 2105 mdecorde
                String schema_property_display
42 1962 mdecorde
@Field @Option(name="unit_ursql", usage="TYPE@PROP=REGEX", widget="String", required=false, def="MENTION")
43 2105 mdecorde
                String unit_ursql
44 2143 mdecorde
@Field @Option(name="strict_inclusion", usage="Units must be strictly included into corpus matches", widget="Boolean", required=true, def="true")
45 2143 mdecorde
                boolean strict_inclusion
46 2143 mdecorde
@Field @Option(name="sep", usage="choose between the len or freq columns", widget="Separator", metaVar="freq        len", required=true, def="")
47 2143 mdecorde
                def sep
48 2143 mdecorde
@Field @Option(name="sort_column", usage="choose between the len or freq columns", widget="StringArray", metaVar="freq        len", required=true, def="freq")
49 2143 mdecorde
                String sort_column
50 2143 mdecorde
@Field @Option(name="max_lines", usage="cut the number of lines shown. '0' means no cut", widget="Integer", required=false, def="0")
51 2143 mdecorde
                int max_lines
52 2105 mdecorde
//@Field @Option(name="output_graph", usage="Show chart", widget="Boolean", required=true, def="false")
53 2105 mdecorde
                output_graph = false
54 1968 mdecorde
@Field @Option(name="debug", usage="Show internal variable content", widget="StringArray", metaVar="OFF        ON        ALL        REALLY ALL", required=true, def="OFF")
55 2105 mdecorde
                debug
56 671 mdecorde
57 671 mdecorde
if (!ParametersDialog.open(this)) return;
58 1968 mdecorde
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 671 mdecorde
60 2105 mdecorde
for (def corpus : selection) {
61 2105 mdecorde
        def analecCorpus = URSCorpora.getCorpus(corpus)
62 671 mdecorde
63 2105 mdecorde
        def errors = AnalecUtils.isPropertyDefined(Schema.class, analecCorpus, schema_ursql)
64 2105 mdecorde
        if (errors.size() > 0) {
65 2105 mdecorde
                println "** The $schema_ursql schema URSQL cannot be computed in the corpus with types: $errors."
66 2105 mdecorde
                return;
67 2105 mdecorde
        }
68 671 mdecorde
69 2105 mdecorde
        errors=AnalecUtils.isPropertyDefined(Unite.class, analecCorpus, unit_ursql)
70 2105 mdecorde
        if (errors.size() > 0) {
71 2105 mdecorde
                println "** $unit_ursql unit URSQL cannot be computed in the corpus with types: $errors."
72 2105 mdecorde
                return;
73 2105 mdecorde
        }
74 671 mdecorde
75 2105 mdecorde
        def schemas = AnalecUtils.selectSchemasInCorpus(debug, analecCorpus, corpus, schema_ursql, minimum_schema_size, maximum_schema_size);
76 671 mdecorde
77 2105 mdecorde
        int nSchemas = 0;
78 671 mdecorde
79 2105 mdecorde
        def lens = [:]
80 2105 mdecorde
        def lensnames = [:]
81 2105 mdecorde
        for (def schema : schemas) {
82 671 mdecorde
83 2105 mdecorde
                def allUnites = schema.getUnitesSousjacentesNonTriees()
84 2143 mdecorde
85 2105 mdecorde
                def units = AnalecUtils.filterElements(debug, allUnites, unit_ursql)
86 2143 mdecorde
87 2143 mdecorde
                selectedUnits = AnalecUtils.filterUniteByInclusion(debug, units, corpus.getMatches(), strict_inclusion, 0)
88 2143 mdecorde
89 2143 mdecorde
                int nUnites = selectedUnits.size();
90 671 mdecorde
91 2105 mdecorde
                if (!lens.containsKey(nUnites)) {
92 2105 mdecorde
                        lens[nUnites] = 0;
93 2105 mdecorde
                        lensnames[nUnites] = [];
94 2105 mdecorde
                }
95 671 mdecorde
96 2105 mdecorde
                lens[nUnites] = lens[nUnites] + 1;
97 2143 mdecorde
                String value = schema.getProp(schema_property_display);
98 2143 mdecorde
                if (value.contains(",")) value = "\"$value\""
99 2143 mdecorde
                lensnames[nUnites] << value
100 2105 mdecorde
                nSchemas++;
101 2105 mdecorde
        }
102 671 mdecorde
103 2105 mdecorde
        //println "nSchemas=$nSchemas"
104 2105 mdecorde
        def freqs = lens.keySet();
105 2105 mdecorde
        freqs.sort();
106 2105 mdecorde
        int t = 0;
107 2105 mdecorde
        int n = 0;
108 2105 mdecorde
        //println "Fréquences ("+freqs.size()+")"
109 2105 mdecorde
        for (def f : freqs) {
110 2105 mdecorde
                t += f * lens[f]
111 2105 mdecorde
                n += lens[f]
112 2105 mdecorde
        }
113 671 mdecorde
114 2143 mdecorde
        if (n == 0) {
115 2143 mdecorde
                println "No units selected for schemas=$schema_ursql and units=$unit_ursql"
116 2143 mdecorde
                return false;
117 2143 mdecorde
        }
118 2143 mdecorde
119 2105 mdecorde
        coef = (t/n)
120 2143 mdecorde
121 2143 mdecorde
        def slens = null
122 2143 mdecorde
        if ("freq".equals(sort_column)) {
123 2143 mdecorde
                slens = lens.sort { a, b -> -a.value <=> -b.value ?: -a.key <=> -b.key }
124 2143 mdecorde
        } else {
125 2143 mdecorde
                slens = lens.sort { a, b -> -a.key <=> -b.key ?: -a.value <=> -b.value }
126 2143 mdecorde
        }
127 2143 mdecorde
128 2105 mdecorde
        def flens = []
129 2105 mdecorde
        slens.each { key, value -> value.times { flens << key } }
130 2105 mdecorde
        def nbins = flens.size()*2
131 671 mdecorde
132 2105 mdecorde
        def cfreq = 0
133 2143 mdecorde
        println "Schema lengths in '$corpus'. The average schema length is $t/$n = "+coef
134 2143 mdecorde
        println "Index:\nlen\tfreq\tcfreq"
135 2143 mdecorde
        int nShown = 0;
136 2143 mdecorde
        for( def it : slens) {
137 2143 mdecorde
                println it.key+"        "+it.value+"        "+(cfreq+=it.value)+"        "+lensnames[it.key].join(", ")
138 2143 mdecorde
                nShown++
139 2143 mdecorde
                if (max_lines > 0 && nShown >= max_lines) {
140 2143 mdecorde
                        println "... (${slens.size() - max_lines})"
141 2143 mdecorde
                        break;
142 2143 mdecorde
                }
143 2143 mdecorde
        }
144 671 mdecorde
145 2105 mdecorde
        def slens2 = slens.sort { a, b -> -a.key <=> -b.key }
146 2105 mdecorde
        if (output_graph) {
147 2105 mdecorde
                def r = RWorkspace.getRWorkspaceInstance()
148 671 mdecorde
149 2105 mdecorde
                r.addVectorToWorkspace("len", slens2.keySet() as int[])
150 2105 mdecorde
                r.addVectorToWorkspace("freq", slens2.values() as int[])
151 2105 mdecorde
                r.addVectorToWorkspace("flen", flens as int[])
152 671 mdecorde
153 2105 mdecorde
                def corpusName = corpus.getName()
154 2105 mdecorde
                println "corpusName=$corpusName"
155 2105 mdecorde
                def PNGFile = File.createTempFile("txm_"+corpus.getID()+"_", ".png", new File(Toolbox.getTxmHomePath(), "results"))
156 2105 mdecorde
                def PNGFilePath = PNGFile.getAbsolutePath()
157 2105 mdecorde
                println "PNG file: "+PNGFilePath
158 2105 mdecorde
159 2105 mdecorde
                def SVGFile = File.createTempFile("txm_"+corpus.getID()+"_", ".svg", new File(Toolbox.getTxmHomePath(), "results"))
160 2105 mdecorde
                def SVGFilePath = SVGFile.getAbsolutePath()
161 2105 mdecorde
                println "SVG file: "+SVGFilePath
162 2105 mdecorde
163 2105 mdecorde
                /// BEGINNING OF R SCRIPT
164 2105 mdecorde
                def script ="""
165 671 mdecorde
hist(flen, xaxt='n', col="gray", xlab="Length", breaks=$nbins, main="$corpusName Longueur des chaînes ($nbins bins)")
166 671 mdecorde
axis(side=1, at=len)
167 671 mdecorde
dev.off()
168 671 mdecorde
"""
169 2105 mdecorde
                /// END OF R SCRIPT
170 671 mdecorde
171 2105 mdecorde
                // execute R script
172 2105 mdecorde
                r.plot(PNGFile, "png(file = \"${PNGFilePath}\"); "+script)
173 2105 mdecorde
                r.plot(SVGFile, "svg(file = \"${SVGFilePath}\"); "+script)
174 671 mdecorde
175 2105 mdecorde
                //display the SVG results graphic
176 2105 mdecorde
                monitor.syncExec(new Runnable() {
177 2105 mdecorde
                                        @Override
178 2105 mdecorde
                                        public void run() { OpenBrowser.openfile(SVGFile.getAbsolutePath(), corpusName+" Longueur des chaînes") }
179 2105 mdecorde
                                })
180 2105 mdecorde
        }
181 2105 mdecorde
        //return ["result":coef, "data":lens]
182 2105 mdecorde
}
183 671 mdecorde