Statistics
| Revision:

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

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

    
6
// STANDARD DECLARATIONS
7
package org.txm.macro.urs.misc
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.searchengine.cqp.corpus.*
14
import org.txm.Toolbox
15
import org.txm.rcp.commands.*
16
import org.apache.commons.lang.StringUtils
17

    
18
// BEGINNING OF PARAMETERS
19
@Field @Option(name="schema_type",usage="", widget="String", required=true, def="Coréférence")
20
String schema_type
21

    
22
@Field @Option(name="minimum_schema_size",usage="", widget="Integer", required=true, def="3")
23
int minimum_schema_size
24

    
25
@Field @Option(name="schema_property_name",usage="", widget="String", required=false, def="")
26
String schema_property_name
27

    
28
@Field @Option(name="schema_property_value",usage="", widget="String", required=false, def=".*")
29
String schema_property_value
30

    
31
@Field @Option(name="unit_type",usage="", widget="String", required=false, def="Maillon")
32
String unit_type
33

    
34
@Field @Option(name="unit_property_name", usage="", widget="String", required=false, def="")
35
String unit_property_name
36

    
37
@Field @Option(name="unit_property_value", usage="", widget="String", required=false, def=".*")
38
String unit_property_value
39

    
40
@Field @Option(name="word_property", usage="", widget="StringArray", metaVar="word        lemma        frlemma        frolemma        #forme#        id", required=false, def="word")
41
String word_property
42

    
43
@Field @Option(name="separator", usage="", widget="String", required=true, def=", ")
44
String separator
45

    
46
@Field @Option(name="buildCQL", usage="générer la requête des unités", widget="Boolean", required=true, def='false')
47
def buildCQL
48

    
49

    
50
if (!(corpusViewSelection instanceof MainCorpus)) {
51
        println "Corpus view selection is not a Corpus"
52
        return;
53
}
54

    
55
if (!ParametersDialog.open(this)) return;
56
// END OF PARAMETERS
57

    
58
MainCorpus corpus = corpusViewSelection
59
def analecCorpus = URSCorpora.getCorpus(corpus)
60

    
61
// check Schema parameters
62
if (!analecCorpus.getStructure().getSchemas().contains(schema_type)) {
63
        println "No schema with name=$schema_type"
64
        return;
65
} else {
66
        if (schema_property_name.length() > 0 && schema_property_value.length() > 0) {
67
                // test property existance
68
                def props = analecCorpus.getStructure().getSchemaProperties(schema_type);
69
                if (!props.contains(schema_property_name)) {
70
                        println "Schema $schema_type has no property named $schema_property_name"
71
                        return;
72
                }
73
        }
74
}
75

    
76
// check unit parameters
77
if (!analecCorpus.getStructure().getUnites().contains(unit_type)) {
78
        println "No unit with name=$unit_type"
79
        return;
80
} else {
81
        if (unit_property_name.length() > 0 && unit_property_value.length() > 0) {
82
                // test property existance
83
                def props = analecCorpus.getStructure().getUniteProperties(unit_type);
84
                if (!props.contains(unit_property_name)) {
85
                        println "Unit $unit_type has no property named $unit_property_name"
86
                        return;
87
                }
88
        }
89
}
90

    
91
def CQI = CQPSearchEngine.getCqiClient()
92

    
93
if (buildCQL) {
94
        word_prop = corpus.getProperty("id")
95
} else {
96
        word_prop = corpus.getProperty(word_property)
97
}
98

    
99
def schemas = analecCorpus.getSchemas(schema_type)
100
schemas.sort() {it.getProps()}
101
def nSchemas = 0
102

    
103
def lens = [:]
104
for (def schema : schemas) {
105

    
106
        if (schema_property_name.length() > 0 && schema_property_value.length() > 0) {
107
                if (!schema.getProp(schema_property_name).matches(schema_property_value)) {
108
                        // ignoring this schema
109
                        continue
110
                }
111
        }
112
        
113
        def nUnites = 0
114
        for (def unit : schema.getUnitesSousjacentes()) {
115
                if (unit_type.length() > 0) {
116
                        if (!unit.getType().equals(unit_type)) {
117
                                continue
118
                        }
119
                }
120
                
121
                if (unit_property_name.length() > 0 && unit_property_value.length() > 0) {
122
                        if (!unit.getProp(unit_property_name).matches(unit_property_value)) {
123
                                // ignoring this schema
124
                                continue
125
                        }
126
                }
127

    
128
                nUnites++
129
        }
130

    
131
        if (nUnites < minimum_schema_size) continue
132

    
133
        print schema.getProps().toString()+ ": "
134
        def first = true
135
        for (def unit : schema.getUnitesSousjacentes()) {
136
                if (unit_type.length() > 0) {
137
                        if (!unit.getType().equals(unit_type)) {
138
                                continue
139
                        }
140
                }
141
                
142
                if (unit_property_name.length() > 0 && unit_property_value.length() > 0) {
143
                        if (!unit.getProp(unit_property_name).matches(unit_property_value)) {
144
                                // ignoring this schema
145
                                continue
146
                        }
147
                }
148

    
149
                String forme =  null;
150

    
151
                if (buildCQL) {
152
                        int[] pos = null
153
                        if (unit.getDeb() == unit.getFin()) pos = [unit.getDeb()]
154
                        else pos = (unit.getDeb()..unit.getFin())
155
                        def first2= true
156
                        q = ""
157
                        pos.each {
158
                                if (first2) { first2 = false } else { q = q+" " }
159
                                int[] pos2 = [it]
160
                                q = q+"["+word_prop+"=\""+CQI.cpos2Str(word_prop.getQualifiedName(), pos2)[0]+"\"]"
161
                        }
162
                        if (first) { first = false } else { print "|" }
163
                        print "("+q+")"
164
                } else {
165
                        if (word_prop == null) { // word_property is the analec unit property to use
166
                        forme = unit.getProp(word_property)
167
                        } else {
168
                        int[] pos = null
169
                        if (unit.getDeb() == unit.getFin()) pos = [unit.getDeb()]
170
                        else pos = (unit.getDeb()..unit.getFin())
171
                                
172
                        forme = StringUtils.join(CQI.cpos2Str(word_prop.getQualifiedName(), pos), " ") // ids is enough
173
                        }
174

    
175
                        if (first) { first = false } else { print separator }
176
                        print forme
177
                }
178
        }
179
        println ""
180

    
181
        nSchemas++
182
}