Statistics
| Revision:

root / tmp / org.txm.oriflamms.rcp / groovy / org / txm / macro / oriflamms / prepare / CoordsProjection.groovy @ 1094

History | View | Annotate | Download (5.6 kB)

1
package org.txm.macro.oriflamms.prepare
2

    
3
import org.txm.importer.StaxIdentityParser;
4
import org.txm.scripts.importer.StaxParser;
5

    
6
class CoordsProjection extends StaxIdentityParser {
7

    
8
        File xmlFile
9
        File img_links_directory
10
        File zones_directory
11

    
12
        String wordTag
13
        String textname
14
        String milestone
15

    
16
        String current_img_file = ""
17
        String current_zone_file = ""
18

    
19
        String xmlType;
20
        String group
21

    
22
        HashMap<String, String> zones = [:]
23
        HashMap<String, String> links = [:]
24

    
25
        public CoordsProjection(File xmlFile, File img_links_directory, File zones_directory, String wordTag) {
26
                super(xmlFile)
27

    
28
                this.xmlFile = xmlFile
29
                this.img_links_directory = img_links_directory
30
                this.zones_directory = zones_directory
31
                this.wordTag = wordTag;
32

    
33
                textname = xmlFile.getName()
34
                int idx = textname.indexOf(".xml")
35
                if (idx > 0) textname = textname.substring(0, idx)
36

    
37
                idx = textname.indexOf("-w")
38
                if (idx > 0) {
39
                        textname = textname.substring(0, idx)
40
                        xmlType = "word"
41
                }
42

    
43
                idx = textname.indexOf("-c")
44
                if (idx > 0) {
45
                        textname = textname.substring(0, idx)
46
                        xmlType = "character"
47
                }
48
        }
49

    
50
        public void processStartElement() {
51
                super.processStartElement();
52
                if (localname.equals("milestone")) {
53
                        String id = "";
54
                        String unit= "";
55
                        for (int i = 0 ; i < parser.getAttributeCount() ; i++) {
56
                                if (parser.getAttributeLocalName(i).equals("id")) {
57
                                        id = parser.getAttributeValue(i)
58
                                } else if (parser.getAttributeLocalName(i).equals("unit")) {
59
                                        unit = parser.getAttributeValue(i)
60
                                }
61
                        }
62

    
63
                        if (unit.equals("surface")) {
64
                                milestone = id;
65
                        }
66
                } else if (localname.equals(wordTag)) {
67
                        String id = "";
68
                        for (int i = 0 ; i < parser.getAttributeCount() ; i++) {
69
                                if (parser.getAttributeLocalName(i).equals("id")) {
70
                                        id = parser.getAttributeValue(i)
71
                                        break;
72
                                }
73
                        }
74

    
75
                        // load next data if needed
76
                        String img_file_name = textname+"_"+milestone+"-links.xml"
77
                        if (!current_img_file.equals(img_file_name)) { // rebuild hashmaps
78
                                String zone_file_name = textname+"_"+milestone+"-zones.xml"
79
                                loadNextData(img_file_name, zone_file_name);
80
                        }
81

    
82
                        //                        println "Find coords for word_id="+id+" in "+img_file_name+" and "+zone_file_name
83
                        //                        println "zone: "+links[id]
84
                        //                        println "coords: "+zones[links[id]]
85
                        if (zones.size() > 0 && links.size() > 0) {
86
                                def coords = zones[links[id]]
87
                                if (coords != null) {
88
                                        if (coords[0] == null || coords[1] == null || coords[2] == null || coords[3] == null) {
89
                                                println "WARNING one of coordinates is missing: $coords"
90
                                        } else {
91
                                                writer.writeAttribute("x1", coords[0])
92
                                                writer.writeAttribute("y1", coords[1])
93
                                                writer.writeAttribute("x2", coords[2])
94
                                                writer.writeAttribute("y2", coords[3])
95
                                        }
96
                                } else {
97
                                        println "WARNING No group for word id="+id+" and link id="+links[id]+" in text "+textname
98
                                }
99
                        }
100
                }
101
        }
102

    
103
        protected void loadNextData(String img_file_name, String zone_file_name) {
104
                File img_link_file = new File(img_links_directory, img_file_name)
105
                File zone_file = new File(zones_directory, zone_file_name)
106

    
107
                zones.clear()
108
                links.clear()
109
                if (zone_file.exists()) {
110
                        StaxParser pZones = new StaxParser(zone_file) {
111
                                                void processStartElement() {
112
                                                        if (localname.equals("zone")) {
113
                                                                String type = "";
114
                                                                String idZone = "";
115
                                                                String ulx, uly, lrx, lry;
116

    
117
                                                                for (int i = 0 ; i < parser.getAttributeCount() ; i++) {
118
                                                                        if (parser.getAttributeLocalName(i).equals("id")) {
119
                                                                                idZone = parser.getAttributeValue(i)
120
                                                                        } else if (parser.getAttributeLocalName(i).equals("type")) {
121
                                                                                type = parser.getAttributeValue(i)
122
                                                                        } else if (parser.getAttributeLocalName(i).equals("ulx")) {
123
                                                                                ulx = parser.getAttributeValue(i)
124
                                                                        } else if (parser.getAttributeLocalName(i).equals("uly")) {
125
                                                                                uly = parser.getAttributeValue(i)
126
                                                                        } else if (parser.getAttributeLocalName(i).equals("lrx")) {
127
                                                                                lrx = parser.getAttributeValue(i)
128
                                                                        } else if (parser.getAttributeLocalName(i).equals("lry")) {
129
                                                                                lry = parser.getAttributeValue(i)
130
                                                                        }
131
                                                                }
132

    
133
                                                                if (type.equals(xmlType)) {
134
                                                                        zones[idZone] = [ulx, uly, lrx, lry]
135
                                                                }
136

    
137
                                                        }
138
                                                }
139
                                        };
140
                        pZones.process();
141
                }
142
                if (img_link_file.exists()) {
143
                        StaxParser pLinks = new StaxParser(img_link_file) {
144
                                                void processStartElement() {
145
                                                        if (localname.equals("linkGrp")) {
146
                                                                for (int i = 0 ; i < parser.getAttributeCount() ; i++) {
147
                                                                        if (parser.getAttributeLocalName(i).equals("type")) {
148
                                                                                group = parser.getAttributeValue(i)
149
                                                                                break
150
                                                                        }
151
                                                                }
152
                                                        } else if (localname.equals("link") && group.startsWith(xmlType)) {
153
                                                                String target = "";
154

    
155
                                                                for (int i = 0 ; i < parser.getAttributeCount() ; i++) {
156
                                                                        if (parser.getAttributeLocalName(i).equals("target")) {
157
                                                                                target = parser.getAttributeValue(i)
158
                                                                                break
159
                                                                        }
160
                                                                }
161

    
162
                                                                def split = target.split(" ")
163
                                                                links[split[0].substring(4)] = split[1].substring(4)
164
                                                        }
165
                                                }
166
                                        };
167
                        pLinks.process();
168
                }
169
                //println "zones size: "+zones.size()
170
                //println "links size: "+links.size()
171

    
172
                current_img_file = img_file_name
173
        }
174

    
175
        public static void main(String[] args) {
176
                File corpusDirectory = new File("/home/mdecorde/TEMP/testori/FontenatTestAlignement")
177
                File xmlFile = new File(corpusDirectory, "txm/FontenayTest-w/FontenayTest-w.xml")
178
                File img_links_directory = new File(corpusDirectory, "img_links")
179
                File zones_directory = new File(corpusDirectory, "zones")
180

    
181
                File outputFile = new File(corpusDirectory, "txm/FontenayTest-w/FontenayTest-w-coords.xml")
182

    
183
                CoordsProjection cp = new CoordsProjection(xmlFile, img_links_directory, zones_directory, "w");
184
                println cp.process(outputFile)
185
        }
186
}