Statistics
| Revision:

root / tmp / org.txm.analec.rcp / src / org / txm / macro / urs / exploit / UnitsReferentialStabilityMacro.groovy @ 2105

History | View | Annotate | Download (5.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.exploit
7

    
8
import org.kohsuke.args4j.*
9
import groovy.transform.Field
10
import org.txm.*
11
import org.txm.rcp.swt.widget.parameters.*
12
import org.txm.annotation.urs.*
13
import org.txm.searchengine.cqp.corpus.*
14
import org.apache.commons.lang.StringUtils;
15
import org.txm.macro.urs.AnalecUtils
16
import visuAnalec.elements.*
17
import org.txm.searchengine.cqp.CQPSearchEngine
18

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

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

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

    
35
@Field @Option(name="minimum_schema_size", usage="minimal schema size", widget="Integer", required=true, def="3")
36
int minimum_schema_size
37
@Field @Option(name="maximum_schema_size", usage="Maximum size needed to consider a schema", widget="Integer", required=true, def="9999999")
38
int maximum_schema_size
39
@Field @Option(name="schema_display_property_name",usage="", widget="String", required=false, def="REF")
40
String schema_display_property_name
41

    
42
@Field @Option(name="unit_ursql", usage="TYPE@PROP=REGEX", widget="String", required=false, def="MENTION")
43
String unit_ursql
44

    
45
@Field @Option(name="word_property", usage="", widget="String", required=false, def="word")
46
String word_property
47

    
48
@Field @Option(name="show_values", usage="", widget="Boolean", required=false, def="false")
49
boolean show_values
50

    
51
@Field @Option(name="debug", usage="Show internal variable content", widget="StringArray", metaVar="OFF        ON        ALL        REALLY ALL", required=true, def="OFF")
52
debug
53

    
54
if (!ParametersDialog.open(this)) return;
55
if (debug == "OFF") debug = 0; else if (debug == "ON") debug = 1; else if (debug == "ALL") debug = 2 else if (debug == "REALLY ALL") debug = 3
56

    
57

    
58
for (def corpus : selection) {
59
        def analecCorpus = URSCorpora.getCorpus(corpus)
60
        
61
        def errors = AnalecUtils.isPropertyDefined(Schema.class, analecCorpus, schema_ursql)
62
        if (errors.size() > 0) {
63
                println "** The $schema_ursql schema URSQL cannot be computed in the corpus with types: $errors."
64
                return;
65
        }
66
        
67
        errors=AnalecUtils.isPropertyDefined(Unite.class, analecCorpus, unit_ursql)
68
        if (errors.size() > 0) {
69
                println "** $unit_ursql unit URSQL cannot be computed in the corpus with types: $errors."
70
                return;
71
        }
72
        def CQI = CQPSearchEngine.getCqiClient()
73
        
74
        def prop = corpus.getProperty(word_property)
75
        if (prop == null) { // no CQP property called $word_property
76
                errors=AnalecUtils.isPropertyDefined(Unite.class, analecCorpus, AnalecUtils.getFilterParameters(unit_ursql)[0], word_property)
77
                if (errors.size() > 0) {
78
                        println "** $word_property unit property cannot be computed in the corpus with types: $errors."
79
                        return;
80
                }
81
        }
82
        def schemas = AnalecUtils.selectSchemasInCorpus(debug, analecCorpus, corpus, schema_ursql, minimum_schema_size, maximum_schema_size);
83
        allFormesSet = new HashSet();
84
        def coefs = []
85
        int n = 1
86
        
87
        int nUnitesAllSchemas = 0
88
        int nUnitesTotalSchemas = 0
89
        
90
        println "** $corpus schemas: "
91
        for (def schema : schemas) {
92
                def formesSet = new HashSet(); // contient toutes les formes du CR courant
93
                nUnitesTotal = 0;
94
                
95
                def allUnites = schema.getUnitesSousjacentesNonTriees()
96
        
97
                def units = AnalecUtils.filterElements(debug, allUnites, unit_ursql)
98
                def nUnites = schema.getUnitesSousjacentes().size()
99
                def nUnitesTotal = units.size()
100
                for (def unit : units) {
101
                
102
                        String forme =  null;
103
                        if (prop == null) { // word_property is the analec unit property to use
104
                                forme = unit.getProp(word_property)
105
                        } else {
106
                                int[] pos = null;
107
                                if (unit.getDeb() == unit.getFin()) pos = [unit.getDeb()]
108
                                else pos = unit.getDeb()..unit.getFin()
109
                                        
110
                                forme = StringUtils.join(CQI.cpos2Str(prop.getQualifiedName(), pos), " ") // ids is enough
111
                        }
112
                        
113
                        formesSet.add(forme)
114
                }
115
                
116
                if (formesSet.size() == 0 || nUnitesTotal == 0) {
117
                        coef = "NA"
118
                } else {
119
                        coef = (nUnitesTotal/formesSet.size())
120
                }
121
                coefs << coef
122
                allFormesSet.addAll(formesSet)
123
                
124
                if (schema_display_property_name != null && schema_display_property_name.length() > 0) {
125
                        print schema.getProp(schema_display_property_name)
126
                } else {
127
                        print schema_ursql+"-"+n+" : "
128
                }
129
                
130
                println " ($nUnites units) : $nUnitesTotal selected units / ${formesSet.size()} ${word_property}s = $coef"
131
                if (show_values) {
132
                        println "\t${word_property}s="+formesSet
133
                }
134
                n++
135
                
136
                nUnitesAllSchemas += nUnites
137
                nUnitesTotalSchemas += nUnitesTotal
138
        }
139
        
140
        coef = nUnitesTotalSchemas/allFormesSet.size()
141
        //println "ALL : ($nUnitesAllSchemas units) : $nUnitesTotalSchemas selected units / ${allFormesSet.size()} ${word_property}s = $coef"
142
        
143
//        return ["result":coefs, "data":["nUnitesTotal":nUnitesTotalSchemas, "allFormesSet":allFormesSet], "coef":(coef)]
144
}