Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / macroproto / ZoneConcordancesMacro.groovy @ 822

History | View | Annotate | Download (2.7 kB)

1
// STANDARD DECLARATIONS
2
package org.txm.macroproto
3

    
4
import org.kohsuke.args4j.*
5
import groovy.transform.Field
6
import org.txm.rcpapplication.swt.widget.parameters.*
7
import org.txm.Toolbox
8
import org.txm.searchengine.cqp.corpus.*
9
import org.txm.searchengine.cqp.corpus.query.*
10
import org.apache.commons.lang.StringUtils
11

    
12
// BEGINNING OF PARAMETERS
13

    
14
if (!(corpusViewSelection instanceof Corpus)) {
15
        println "please select a corpus: $corpusViewSelection"
16
        return
17
}
18

    
19
def CQI = Toolbox.getCqiClient();
20

    
21
@Field @Option(name="query", usage="an example query", widget="Query", required=true, def='[pos="V.*"]')
22
def query
23

    
24
@Field @Option(name="property", usage="an example query", widget="String", required=true, def='word')
25
def property
26

    
27
@Field @Option(name="outputFile", usage="an example file", widget="FileSave", required=true, def="C:/Temp/foo.txt")
28
def outputFile
29

    
30
// Open the parameters input dialog box
31
if (!ParametersDialog.open(this)) return;
32

    
33
def writer = outputFile.newWriter("UTF-8")
34

    
35
// END OF PARAMETERS
36

    
37
String query_string = query.toString();
38
if (query_string.startsWith("@")) query_string = query_string.substring(1)
39

    
40
def split = query_string.split("@")
41
def queries = []
42
// @ A @ B @ C @ D
43
println "requête de zones: "+query_string
44
//println split
45
String current_q = ""
46
for (int i = 0 ; i < split.length ;i++) {
47
        def reconstructed_query = ""
48
        def q = split[i]
49
        
50
        //println "q=$q"
51
        
52
        //println "current_q=$current_q"
53
        
54
        if (i > 0 && q.length() > 0) {
55
                reconstructed_query = current_q+"@"+q
56
                if (i < (split.length-1)) reconstructed_query += split[i+1..split.length-1].join("")
57
                queries << reconstructed_query
58
        }
59
        
60
        current_q += q
61
        //println "Q="+current_q
62
}
63

    
64
if (queries.size() == 0) {
65
        println "no target @ found in query: $query"
66
        return
67
}
68

    
69
def corpus = corpusViewSelection
70
def wordProperty = corpus.getProperty(property)
71
def qrs = []
72
for (def q : queries) {
73
        qrs << corpus.query(new Query(q), "TMP", false).getMatches()
74
}
75
def qr = qrs[0]
76
println ""+qr.size()+" matches pour "+(queries.size()+1)+" zones"
77

    
78
writer.print "N\tposition\t"
79
for (int j = 0 ; j < queries.size()+1 ; j++) {
80
        writer.print "\t"
81
        writer.print "Z"+(j+1)
82
}
83
writer.println ""
84

    
85
for (int i = 0 ; i < qr.size() ; i++) {
86
        int[] positions = qr[i].getStart() .. qr[i].getEnd()
87
        def forms = CQI.cpos2Str(wordProperty.getQualifiedName(), positions)
88
        
89
        def targets = []
90
        for (int j = 0 ; j < queries.size() ; j++) {
91
                targets <<  (qrs[j][i]).getTarget()
92
        }
93
        
94
        writer.print ""+(i+1)+"\t"+positions[0]+"\t"
95
        boolean first = true
96
        for (int j = 0 ; j < forms.size() ; j++) {
97
                if (targets.contains(positions[j])) {
98
                        writer.print "\t"
99
                        first = true
100
                }
101
                
102
                if (!first) { writer.print " "}
103
                writer.print forms[j]
104
                first=false
105
        }
106
        writer.println ""
107
        
108
}
109

    
110
writer.close()
111
println "Result written in "+outputFile.getAbsolutePath()