Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / macro / annotation / CQL2AnnotationsMacro.groovy @ 2656

History | View | Annotate | Download (2 kB)

1

    
2
package org.txm.macro.annotation
3

    
4
// STANDARD DECLARATIONS
5
import org.txm.rcpapplication.swt.widget.parameters.*
6
import org.txm.searchengine.cqp.corpus.*
7
import org.txm.searchengine.cqp.corpus.query.*
8
import org.txm.rcp.views.corpora.CorporaView
9
import org.txm.annotation.kr.core.KRAnnotationEngine
10
import org.txm.annotation.kr.core.repository.*
11

    
12
def scriptName = this.class.getSimpleName()
13

    
14
if (!(corpusViewSelection instanceof CQPCorpus)) {
15
        println "** $scriptName: please select a Corpus in the Corpus view."
16
        return 0
17
}
18

    
19
CQPCorpus corpus = corpusViewSelection
20

    
21
MainCorpus mcorpus = corpus.getMainCorpus();
22
KnowledgeRepository kr = KnowledgeRepositoryManager.createSimpleKnowledgeRepository(mcorpus.getName());
23

    
24
@Field @Option(name="query", usage="The CQL query to select tokens", widget="String", required=true, def="")
25
String query
26

    
27
@Field @Option(name="property", usage="The property to annotate", widget="String", required=true, def="property")
28
String property
29

    
30
@Field @Option(name="value", usage="the annotation value", widget="String", required=true, def="value")
31
String value
32

    
33
// Open the parameters input dialog box
34
if (!ParametersDialog.open(this)) return
35

    
36
if (query == null || query.trim().length() == 0) {
37
        println "Error: no query set."
38
        return 0
39
}
40

    
41
annotManager = KRAnnotationEngine.getAnnotationManager(corpus);
42

    
43
AnnotationType type = kr.getType(property);
44
if (type == null) {
45
        AnnotationType t = kr.addType(property, property);
46
        t.setEffect(AnnotationEffect.TOKEN);
47
        type = t
48
} else {
49
        type.setEffect(AnnotationEffect.TOKEN);
50
}
51

    
52
value_to_add = kr.getValue(type, value);
53
if (value_to_add == null) { // create or not the annotation is simple mode
54
        value_to_add = kr.addValue(value, value, type.getId());
55
}
56

    
57
qr = corpus.query(new CQLQuery(query), "TMP", false)
58
if (qr.getNMatch() == 0) {
59
        println "No match to annotate"
60
        return 0
61
}
62

    
63
annotManager.createAnnotations(type, value_to_add, qr.getMatches(), null);
64

    
65
println query+" created "+qr.getNMatch()+" annotations."
66

    
67
return qr.getNMatch()