Statistics
| Revision:

root / tmp / org.txm.core / src / groovy / org / txm / macro / annotation / InjectWordPropTableMacro.groovy @ 187

History | View | Annotate | Download (3.3 kB)

1
package org.txm.macro.annotation;
2

    
3
import org.txm.utils.*
4

    
5
import java.nio.charset.Charset
6

    
7
import org.kohsuke.args4j.*
8

    
9
import groovy.transform.Field
10

    
11
import org.txm.rcpapplication.swt.widget.parameters.*
12

    
13
@Field @Option(name="inputDirectory", usage="The XML-TXM folder", widget="Folder", required=true, def="")
14
def inputDirectory = new File(System.getProperty("user.home"), "xml/anapovoas/HELPTXM-02-131124/ite02")
15

    
16
@Field @Option(name="outputDirectory", usage="The result folder", widget="Folder", required=true, def="")
17
def outputDirectory = new File(System.getProperty("user.home"), "xml/anapovoas/HELPTXM-02-131124/ite03")
18

    
19
@Field @Option(name="tsvFile", usage="The annotation TSV file", widget="File", required=true, def="")
20
def tsvFile = new File(System.getProperty("user.home"), "xml/anapovoas/HELPTXM-02-131124/TSV-ITE02.csv")
21

    
22
@Field @Option(name="properties", usage="columns to inject separated by commas", widget="String", required=true, def="p1, p2, ... , pn")
23
def properties = "verificado"
24

    
25
@Field @Option(name="debug", usage="Debug de the macro", widget="Boolean", required=true, def="false")
26
def debug = true
27

    
28
if (!ParametersDialog.open(this)) return;
29

    
30
def split = properties.split(",")
31
if (split.length == 0) {
32
        println "ERROR: no property given"
33
        return false
34
}
35
properties = []
36
for (def p : split) properties << p.trim()
37

    
38
println "Injecting annotations with the following parameters:"
39
println "- inputDirectory: $inputDirectory"
40
println "- outputDirectory: $outputDirectory"
41
println "- tsvFile: $tsvFile"
42
println "- properties: $properties"
43

    
44
outputDirectory.mkdir()
45
if (!outputDirectory.exists()) {
46
        println "ERROR: could not create or read $outputDirectory directory"
47
        return false
48
}
49

    
50
CsvReader records = new CsvReader(tsvFile.getAbsolutePath(), "\t".charAt(0), Charset.forName("UTF-8"))
51
records.setTextQualifier((char)0)
52
records.readHeaders();
53

    
54
def header = Arrays.asList(records.getHeaders())
55
if (debug) println "CSV header: $header"
56
for (def s : properties) if (!header.contains(s)) {
57
        println "Missing annotation property : $s"
58
        return;
59
}
60
for (def s : ["text_id", "id"]) if (!header.contains(s)) {
61
        println "Missing annotation property : $s"
62
        return;
63
}
64

    
65
def injections = [:]
66
int no = 1;
67
while (records.readRecord()) {
68
        String textid = records.get("text_id")
69
        if (!injections.containsKey(textid)) injections[textid] = [:]
70
        
71
        def props = [:]
72
        for (def s : properties) props[s] = records.get(s)
73
        if (records.get("id") == "") {
74
                 print "ERROR in record "+no
75
        } else {
76
                injections[textid][records.get("id")] = props
77
        }
78
        no++;
79
}
80

    
81
println "Injecting..."
82
//for (String textid : injections.keySet()) {
83
for (File currentXMLFile : inputDirectory.listFiles()) {
84
        String textid = currentXMLFile.getName();
85
        int idx = textid.indexOf(".")
86
        if (idx > 0) textid = textid.substring(0, idx);
87
        
88
        if (injections.containsKey(textid)) {        
89
                if (debug) println "Injections of "+injections[textid].size()+" lines."
90
                
91
                def builder = new AnnotationInjectionFilter(currentXMLFile, injections[textid], properties, debug)
92
                println "injecting in $currentXMLFile $properties"
93
                builder.process(new File(outputDirectory, currentXMLFile.getName()));
94
                println ""+builder.n+" lines injected."
95
        } else {
96
                println "copying file $currentXMLFile"
97
                FileCopy.copy(currentXMLFile, new File(outputDirectory, currentXMLFile.getName()))
98
        }
99
}
100

    
101
println "New XML-TXM files are saved in ${outputDirectory}."